Reputation: 5262
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.
Upvotes: 3
Views: 2332
Reputation: 350
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
Upvotes: 5
Reputation: 2116
$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
http://laravel.com/docs/validation#rule-exists
Upvotes: 0