php - Laravel validation OR -


i have validation requires url or route there not both.

    $this->validate($request, [         'name'  =>  'required|max:255',         'url'   =>  'required_without_all:route|url',         'route' =>  'required_without_all:url|route',         'parent_items'=>  'sometimes|required|integer'     ]); 

i have tried using required_without , required_without_all both past validation , not sure why.

route rule in route field

i think easiest way creation own validation rule. looks like.

validator::extend('empty_if', function($attribute, $value, $parameters, illuminate\validation\validator $validator) {      $fields = $validator->getdata(); //data passed validator      foreach($parameters $param) {         $excludevalue = array_get($fields, $param, false);          if($excludevalue) { //if exclude value present validation not passed             return false;         }     }      return true; }); 

and use

    $this->validate($request, [     'name'  =>  'required|max:255',     'url'   =>  'empty_if:route|url',     'route' =>  'empty_if:url|route',     'parent_items'=>  'sometimes|required|integer' ]); 

p.s. don't forget register in provider.

edit

add custom message

1) add message 2) add replacer

validator::replacer('empty_if', function($message, $attribute, $rule, $parameters){     $replace = [$attribute, $parameters[0]];     //message is: field :attribute cannot filled if :other filled     return  str_replace([':attribute', ':other'], $replace, $message); }); 

Comments

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -