php - Laravel validation: Required only and only one field -


i have got 2 fields namely number , percentage. want user input value in 1 input field. if user inputs values in both number , percentage field, system should throw validation error. there can laravel validation achieve this?

thanks.

you can write custom validator that: http://laravel.com/docs/5.0/validation#custom-validation-rules

it may looks somthing this:

class customvalidator extends illuminate\validation\validator {      public function validateempty($attribute, $value)     {         return ! $this->validaterequired($attribute, $value);     }      public function validateemptyif($attribute, $value, $parameters)     {         $key = $parameters[0];          if ($this->validaterequired($key, $this->getvalue($key))) {             return $this->validateempty($attribute, $value);         }          return true;     } } 

register in service provider:

validator::resolver(function($translator, $data, $rules, $messages, $attributes) {     return new customvalidator($translator, $data, $rules, $messages, $attributes); }); 

use (in form request, example):

class storesomethingrequest extends formrequest {     // ...      public function rules()     {         return [             'percentage' => 'empty_if:number',             'number'     => 'empty_if:percentage',         ];     } } 

update tested in tinker:

validator::make(['foo' => 'foo', 'bar' => 'bar'], ['bar' => 'empty_if:foo'])->fails() => true validator::make(['foo' => '', 'bar' => 'bar'], ['bar' => 'empty_if:foo'])->fails() => false validator::make(['foo' => '', 'bar' => 'bar'], ['foo' => 'empty_if:bar'])->fails() => false 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -