【问题标题】:Add rule and validation value from database从数据库添加规则和验证值
【发布时间】:2020-10-29 01:51:59
【问题描述】:

当用户创建帖子时,我想检查此类别是否属于类型。如果类别属于类型,则存储它,否则给我错误

我的数据库表:

id  categoryname    type
1   cat1          posttype
2   cat2          posttype
3   cat3          usertype
4   cat4          usertype 

现在当用户创建帖子并发送 1 id 然后我会检查它是否属于 posttype 如果它属于 usertype 然后给我错误。

【问题讨论】:

    标签: laravel validation rules


    【解决方案1】:

    假设这是您的 $request 的 dd() 的样子:

    $request = [
        [
            'id' => 1,
            'categoryname' => 'cat1',
            'type' => 'posttype',
        ],
        [
            'id' => 2,
            'categoryname' => 'cat2',
            'type' => 'posttype',
        ],
        [
            'id' => 3,
            'categoryname' => 'cat3',
            'type' => 'usertype',
        ],
        [
            'id' => 4,
            'categoryname' => 'cat4',
            'type' => 'usertype',
        ],
    ];
    

    然后您可以创建一个如下所示的validation 规则:

    $rules = [
        ...
        '*.type' => [ 'required', 'in:posttype' ]
        ...
    ];
    

    然后您需要在表单请求或控制器的验证器中使用该规则。如果您不知道该怎么做,请告诉我,我会更新答案。

    【讨论】:

    • 不,这是我的数据库表,不是对象数组
    • 请将您要验证的请求放入您的问题中。
    猜你喜欢
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    • 1970-01-01
    • 2020-09-24
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多