Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I had to solve the same problem. Here's what I did:</p> <p>I created a custom class that extends the default <code>Laravel\Validator</code> class. I wanted to be able to tell the validator when I'm dealing with multiple values (like in your case). In my implementation this could be done by appending '_array' to every validation rule for a certain field name. What my class does is to check if the rule name has this suffix and if it does the value parameter (which is an array in this case) is broken down to its contained items and passed to the default validation functions in Laravel.</p> <pre><code>&lt;?php class Validator extends Laravel\Validator { public function __call($method, $parameters) { if (substr($method, -6) === '_array') { $method = substr($method, 0, -6); $values = $parameters[1]; $success = true; foreach ($values as $value) { $parameters[1] = $value; $success &amp;= call_user_func_array(array($this, $method), $parameters); } return $success; } else { return parent::__call($method, $parameters); } } protected function getMessage($attribute, $rule) { if (substr($rule, -6) === '_array') { $rule = substr($rule, 0, -6); } return parent::getMessage($attribute, $rule); } } </code></pre> <p>As stated in the posts above you will have to make the following change so that your custom <code>Validator</code> class can be found by the Autoloader:</p> <blockquote> <p>Then you need to remove the following line from application/config/application.php file:</p> <p>'Validator' => 'Laravel\Validator'</p> </blockquote> <p>When this is done you'll be able to use all of Laravel's validation rules with the '_array' suffix. Here's an example:</p> <pre><code>public static $rules = array( 'post_categories'=&gt;'required_array|alpha_array' ); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload