Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate select box input in Laravel4

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

like image 410
eski009 Avatar asked Jun 21 '13 12:06

eski009


People also ask

What is the method used to configure validation rules in form request?

Laravel Form Request class comes with two default methods auth() and rules() . You can perform any authorization logic in auth() method whether the current user is allowed to request or not. And in rules() method you can write all your validation rule.

What is the method used for specifying custom messages for validator errors in form request?

After checking if the request failed to pass validation, you may use the withErrors method to flash the error messages to the session. When using this method, the $errors variable will automatically be shared with your views after redirection, allowing you to easily display them back to the user.


1 Answers

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

like image 62
Rob Gordijn Avatar answered Nov 03 '22 23:11

Rob Gordijn