3

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.

2 Answers 2

5

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

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

Comments

0
$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

1 Comment

this does not help. exists with conditions based on other inputs.

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.