Php 基于其他用户输入的Laravel验证输入

Php 基于其他用户输入的Laravel验证输入,php,validation,laravel-4,Php,Validation,Laravel 4,我有一个提交类别的表格。(新增,编辑) 根 电脑类 ... 父_id中的每个id都是数据库中的类别id,除了0,root 我想进行一个验证,该验证显示:如果父id不是0,请在category表中检查父id是否为有效id。(0是根,而不是数据库中的id) 如何使用laravel验证规则做到这一点 $input = Input::only('dragon', 'parent_id', 'carrot'); $rules = [ // your rules ]; if ( ! empt

我有一个提交类别的表格。(新增,编辑)


根
电脑类
...
父_id中的每个id都是数据库中的类别id,除了0,root

我想进行一个验证,该验证显示:如果父id不是0,请在category表中检查父id是否为有效id。(0是根,而不是数据库中的id)

如何使用laravel验证规则做到这一点

$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

我以前有时没用过。但请在下面试试。目前无法测试自己

/* This is untested code  */

$validation->sometimes('parent_id', 'exists:categories', function($input)
{
    return $input->parent_id > 0;
});

/* This is untested code.*/

手动输入:

这没有帮助。存在基于其他输入的条件。
/* This is untested code  */

$validation->sometimes('parent_id', 'exists:categories', function($input)
{
    return $input->parent_id > 0;
});

/* This is untested code.*/