6

Currently when I want to validate a select box I need to include all the values within the validation field.

public static $rules = array(
    'type' => array('required', 'in:a,b,c,d')
);

Is there a best practise way to do this using an array?

For example: I have a long list of country names and want to include this as the validation list. The hacky way of doing this would be something along the lines of:

public static $rules = array(
    'type' => array('required', 'in:'.implode(',', $countries))
);

Thanks

1
  • Just thinking about this - I could use a custom validation rule and add the array name as the parameter? Commented Jun 21, 2013 at 13:21

2 Answers 2

3

A custom is possible, but a exist-rule can also do the job. More details on http://laravel.com/docs/validation#rule-exists

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. That only helps if we're using data from a database though
well, custom validation it is ;)
0

Create a select menu with an array like so:

$types = [ ''    => 'select a type',
          'one'  => 'type one',
          'a'    => 'type a' ];

And rules for validation like so:

$rules = ['type' => 'required'];

As the first key of $types is an empty string. Will output like this:

 <option value=''>select a type</option>

On validation the required rule will fail validation.

Assuming all options in the select menu are valid options.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.