I have a form for submitting categories. ( new , edit )
<form ... >
<input type="text" name="name" />
<select name="parent_id" >
<option value="0" >Root</option>
<option value="1" >Computer</option>
<option value="..." >...</option>
</select>
</form>
each one of ids in parent_id is a category id in database except for 0, root;
I want to have a validation which says: check parent_id to be a valid id in category table if it is not 0. ( 0 is root and not an id in database )
How can I do that with laravel validation rules.
I've not used Sometimes before. But please try below. Not currently able to test myself.
/* This is untested code */
$validation->sometimes('parent_id', 'exists:categories', function($input)
{
return $input->parent_id > 0;
});
/* This is untested code.*/
Manual Entry: http://laravel.com/docs/validation#conditionally-adding-rules
$input = Input::only('dragon', 'parent_id', 'carrot');
$rules = [
// your rules
];
if ( ! empty($input['parent_id']))
{
$rules['parent_id'] = 'exists:categories';
}
// the normal validation stuff here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.