Php Laravel/嵌套验证

Php Laravel/嵌套验证,php,laravel,validation,laravel-validation,Php,Laravel,Validation,Laravel Validation,我正在为一个基本的页面生成器系统构建一个组件驱动的API,在进行验证时遇到了一个障碍 首先,我想解释一下用例 如果我们有一个组件,例如在/components/ProfileCard.Vue中的Vue中 导出默认值{ 道具:{ 名称:String, 年龄:数字,, 阿凡达:字符串 } } 我正在backend components.php配置中创建一个组件: 你的方法似乎把一个简单的问题复杂化了。我永远不会在验证规则中进行验证。而是执行依赖于组件的规则,并在表单请求中相应地调整它。您可以轻松地执

我正在为一个基本的页面生成器系统构建一个组件驱动的API,在进行验证时遇到了一个障碍

首先,我想解释一下用例

如果我们有一个组件,例如在/components/ProfileCard.Vue中的Vue中

导出默认值{ 道具:{ 名称:String, 年龄:数字,, 阿凡达:字符串 } } 我正在backend components.php配置中创建一个组件:


你的方法似乎把一个简单的问题复杂化了。我永远不会在验证规则中进行验证。而是执行依赖于组件的规则,并在表单请求中相应地调整它。您可以轻松地执行这样的嵌套规则

[
    'profile.name' => 'string',
]
执行表单请求中的其余逻辑。策略是基于请求输入和您的配置文件(基于您已经尝试过的内容)制定规则

public function rules()
{
    // i do not know how you determine this key
    $componentKey = 'profile';

    $rules = [
        ...,
        $componentKey => [
            'required',
        ]
    ];

    $inputComponent= $this->input('profile')['component'];
    $components = config('components');

    // based on your data this seems wrong, but basically fetch the correct config entry
    $component = $components[$inputComponent];

    foreach ($component['rules'] as $key => $value) {
            $rules[$componentKey  . '.' . $key] => $value;
    }

    return $rules;
}

您的代码中有一些部分我无法理解数据的含义,我不知道您是如何获得组件密钥配置文件的,并且基于配置和组件字段的代码似乎是错误的,应该使用where条件执行循环。我认为这可以让您朝着正确的方向前进,此解决方案将解决您的邮件问题,并且更加简单。

您可以发布您的规则吗?他们在哪一类等等。我已经更新了这个问题,包括我取得的一些进展和关于这个问题的更多信息。当你添加$this->validator->errors->getMessages时,你会得到什么;或者这与您问题中的最后一个代码片段完全相同?我知道使用点语法嵌套验证的能力,我试图实现的是在页面上动态生成内容,例如页面生成器,它将具有不同的组件类型,例如列表组件或英雄组件,需要验证的数据不同,具体取决于所选的数据。这里的ProfileComponent用于示例目的。你的回答确实解决了这个问题,尽管我希望在某种程度上避免创建一个混乱的规则方法。看来这是唯一能做到这一点的方法,所以我很高兴把你的答案标记为正确!我不认为这会比你要走的路更混乱,在验证中验证。在每个组件模型中,您可以生成生成规则的方法,并且您将正确地检查内部结构,我可以理解为什么我尝试的嵌套验证方式没有实现,它为错误的发生留下了太多的地方,并且使得使用验证消息数据变得更加困难!
[
    'profile.name' => 'string',
]
public function rules()
{
    // i do not know how you determine this key
    $componentKey = 'profile';

    $rules = [
        ...,
        $componentKey => [
            'required',
        ]
    ];

    $inputComponent= $this->input('profile')['component'];
    $components = config('components');

    // based on your data this seems wrong, but basically fetch the correct config entry
    $component = $components[$inputComponent];

    foreach ($component['rules'] as $key => $value) {
            $rules[$componentKey  . '.' . $key] => $value;
    }

    return $rules;
}