【问题标题】:Nested array validation laravel嵌套数组验证 laravel
【发布时间】:2016-03-27 09:15:31
【问题描述】:

我正在构建一个基于 REST 的 API,其中一个 API 有以下请求

{
   "categories_id" :"1",
   "product_name" : "Pen",
   "product_description" : "this is pen",
   "tags" : "pen,write",
   "image_count" : "4",
   "skus": 
      {
          "is_shippable":"n",
          "actual_price":"100.55", 
          "selling_price":"200.45",
          "quantity_type":"bucket",
          "quantity_total":"10",
          "bucket_value":"instock",
          "sort_order":"1"
      }
}

这些是我的验证规则

protected $rules = [
        ValidatorInterface::RULE_CREATE => [
        'users_id' => 'required',
        'user_profiles_id' => 'required',
        'categories_id' => 'required',
        'product_name' => 'required|max:100',
        'product_description' => 'required|max:1000',
        'tags' => 'required',
        'image_count'=>'required|integer',
        'creation_mode'=>'required|integer',
        'skus.is_shippable'=>'in:y,n',
        'skus.actual_price'=>'regex:/^\s*(?=.*[1-9])\d*(?:\.\d{1,2})?\s*$/',
        'skus.selling_price' => 'regex:/^\s*(?=.*[1-9])\d*(?:\.\d{1,2})?\s*$/',
        'skus.quantity_type' => 'sometimes|required|in:finite,infinite,bucket',
        'skus.quantity_total' => 'integer|required_if:skus.quantity_type,finite', 
        'skus.bucket_value'=>'in:instock,soldout,limited|required_if:skus.quantity_type,bucket',
        'skus.sort_order'=> 'required|integer'
        ],
        ValidatorInterface::RULE_UPDATE => [
        ]
    ];

上述请求正在正确地得到验证。但是 skus 里面可以有多个实体,如下所示

{
       "categories_id" :"1",
       "product_name" : "Pen",
       "product_description" : "this is pen",
       "tags" : "pen,write",
       "image_count" : "4",
       "skus": 
          [{
              "is_shippable":"n",
              "actual_price":"100.55", 
              "selling_price":"200.45",
              "quantity_type":"bucket",
              "quantity_total":"10",
              "bucket_value":"instock",
              "sort_order":"1"
          },
          {
              "is_shippable":"n",
              "actual_price":"100.55", 
              "selling_price":"200.45",
              "quantity_type":"bucket",
              "quantity_total":"10",
              "bucket_value":"instock",
              "sort_order":"1"
          }]
    }

如何验证是否有多个嵌套实体?

【问题讨论】:

    标签: php validation laravel laravel-5


    【解决方案1】:

    你使用的是什么版本的 Laravel?如果您使用的是Laravel 5.2,或者您不介意更新到它,那么有一个开箱即用的解决方案。

    数组验证

    在 Laravel 5.2 中验证数组表单输入字段要容易得多。为了 例如,验证给定数组输入字段中的每封电子邮件是 独一无二,您可以执行以下操作:

    $validator = Validator::make($request->all(), [
        'person.*.email' => 'email|unique:users'
    ]);
    

    同样,您可以在指定验证时使用 * 字符 语言文件中的消息,使使用单一语言变得轻而易举 基于数组的字段的验证消息:

    'custom' => [
        'person.*.email' => [
            'unique' => 'Each person must have a unique e-mail address',
        ]
    ],
    

    来自Laravel news的另一个例子:

    假设您有一个包含如下输入字段数组的表单:

    <p>
    <input type="text" name="person[1][id]">
    <input type="text" name="person[1][name]">
    </p>
    <p>
    <input type="text" name="person[2][id]">
    <input type="text" name="person[2][name]">
    </p>
    

    在 Laravel 5.1 中添加验证规则需要循环和 单独添加规则。而不是必须做所有的事情 被“Laravelized”成这样:

    $v = Validator::make($request->all(), [
      'person.*.id' => 'exists:users.id',
      'person.*.name' => 'required:string',
    ]);
    

    因此,如果您不想使用 Laravel 5.2,则必须手动进行,如果您确实更新到 Laravel 5.2,则可以使用新的数组验证,它会是这样的:

    protected $rules = [
            ValidatorInterface::RULE_CREATE => [
            'users_id' => 'required',
            'user_profiles_id' => 'required',
            'categories_id' => 'required',
            'product_name' => 'required|max:100',
            'product_description' => 'required|max:1000',
            'tags' => 'required',
            'image_count'=>'required|integer',
            'creation_mode'=>'required|integer',
            'skus.*.is_shippable'=>'in:y,n',
            'skus.*.actual_price'=>'regex:/^\s*(?=.*[1-9])\d*(?:\.\d{1,2})?\s*$/',
            'skus.*.selling_price' => 'regex:/^\s*(?=.*[1-9])\d*(?:\.\d{1,2})?\s*$/',
            'skus.*.quantity_type' => 'sometimes|required|in:finite,infinite,bucket',
            'skus.*.quantity_total' => 'integer|required_if:skus.quantity_type,finite', 
            'skus.*.bucket_value'=>'in:instock,soldout,limited|required_if:skus.quantity_type,bucket',
            'skus.*.sort_order'=> 'required|integer'
            ],
            ValidatorInterface::RULE_UPDATE => [
            ]
        ];
    

    编辑

    Ihmo 添加此额外验证逻辑的最佳方法是扩展 Validator 类创建您的 CustomValidator 类,这可能有点矫枉过正,但当 Laravel 5.2 发布时您可以删除您的 CustomValidator 并继续使用 Laravel 的 5.2 Validator,而无需对您的代码进行任何更改。

    怎么样?首先我们在app/ 下创建一个文件夹,我决定将这个文件夹命名为Validator,你可以随意命名,只要记住更新以下类的命名空间即可。接下来我们将在这个文件夹中创建 3 个 .php 文件 CustomValidator.php、CustomValidatorServiceProvider.php 和 Factory.php。

    CustomValidator.php

    <?php
    
    namespace App\Validator;
    
    use Illuminate\Support\Arr;
    use Illuminate\Support\Str;
    use Illuminate\Validation\Validator;
    use Symfony\Component\Translation\TranslatorInterface;
    
    class CustomValidator extends Validator
    {
        /**
         * Create a new Validator instance.
         *
         * @param  \Symfony\Component\Translation\TranslatorInterface  $translator
         * @param  array  $data
         * @param  array  $rules
         * @param  array  $messages
         * @param  array  $customAttributes
         * @return void
         */
        public function __construct(TranslatorInterface $translator, array $data, array $rules, array $messages = [], array $customAttributes = [])
        {
            $this->translator = $translator;
            $this->customMessages = $messages;
            $this->data = $this->parseData($data);
            $this->customAttributes = $customAttributes;
    
            // Explode the rules first so that the implicit ->each calls are made...
            $rules = $this->explodeRules($rules);
    
            $this->rules = array_merge((array) $this->rules, $rules);
        }
    
        /**
         * Explode the rules into an array of rules.
         *
         * @param  string|array  $rules
         * @return array
         */
        protected function explodeRules($rules)
        {
            foreach ($rules as $key => $rule) {
                if (Str::contains($key, '*')) {
                    $this->each($key, $rule);
                    unset($rules[$key]);
                } else {
                    $rules[$key] = (is_string($rule)) ? explode('|', $rule) : $rule;
                }
            }
            return $rules;
        }
    
    
        /**
         * Define a set of rules that apply to each element in an array attribute.
         *
         * @param  string  $attribute
         * @param  string|array  $rules
         * @return void
         *
         * @throws \InvalidArgumentException
         */
        public function each($attribute, $rules)
        {
            $data = Arr::dot($this->data);
            foreach ($data as $key => $value) {
                if (Str::startsWith($key, $attribute) || Str::is($attribute, $key)) {
                    foreach ((array) $rules as $ruleKey => $ruleValue) {
                        if (! is_string($ruleKey) || Str::endsWith($key, $ruleKey)) {
                            $this->mergeRules($key, $ruleValue);
                        }
                    }
                }
            }
        }
    
    
    
        /**
         * Get the inline message for a rule if it exists.
         *
         * @param  string  $attribute
         * @param  string  $lowerRule
         * @param  array   $source
         * @return string|null
         */
        protected function getInlineMessage($attribute, $lowerRule, $source = null)
        {
            $source = $source ?: $this->customMessages;
            $keys = ["{$attribute}.{$lowerRule}", $lowerRule];
            // First we will check for a custom message for an attribute specific rule
            // message for the fields, then we will check for a general custom line
            // that is not attribute specific. If we find either we'll return it.
            foreach ($keys as $key) {
                foreach (array_keys($source) as $sourceKey) {
                    if (Str::is($sourceKey, $key)) {
                        return $source[$sourceKey];
                    }
                }
            }
        }
    
        /**
         * Get the custom error message from translator.
         *
         * @param  string  $customKey
         * @return string
         */
        protected function getCustomMessageFromTranslator($customKey)
        {
            $shortKey = str_replace('validation.custom.', '', $customKey);
            $customMessages = Arr::dot(
                (array) $this->translator->trans('validation.custom')
            );
            foreach ($customMessages as $key => $message) {
                if ($key === $shortKey || (Str::contains($key, ['*']) && Str::is($key, $shortKey))) {
                    return $message;
                }
            }
            return $customKey;
        }
    }
    

    此自定义验证器包含在 Laravel 5.2 上所做的所有更改,您可以在 here 中查看它们

    现在我们有了一个新的 CustomValidator 类,我们必须找到一种使用它的方法,为此我们必须扩展 ValidatorServiceProvider 和 Validator factory。 p>

    CustomValidatorServiceProvider.php

    <?php
    
    namespace App\Validator;
    
    
    class CustomValidatorServiceProvider extends \Illuminate\Validation\ValidationServiceProvider
    {
        /**
         * Register the validation factory.
         *
         * @return void
         */
        protected function registerValidationFactory()
        {
            $this->app->singleton('validator', function ($app) {
                $validator = new Factory($app['translator'], $app);
    
                // The validation presence verifier is responsible for determining the existence
                // of values in a given data collection, typically a relational database or
                // other persistent data stores. And it is used to check for uniqueness.
                if (isset($app['validation.presence'])) {
                    $validator->setPresenceVerifier($app['validation.presence']);
                }
    
                return $validator;
            });
        }
    }
    

    Factory.php

    <?php
    
    namespace App\Validator;
    
    use App\Validator\CustomValidator as Validator;
    
    class Factory extends \Illuminate\Validation\Factory
    {
        /**
         * Resolve a new Validator instance.
         *
         * @param  array  $data
         * @param  array  $rules
         * @param  array  $messages
         * @param  array  $customAttributes
         * @return App\Test\CustomValidator
         */
        protected function resolve(array $data, array $rules, array $messages, array $customAttributes)
        {
            if (is_null($this->resolver)) {
                return new Validator($this->translator, $data, $rules, $messages, $customAttributes);
            }
    
            return call_user_func($this->resolver, $this->translator, $data, $rules, $messages, $customAttributes);
        }
    }
    

    现在我们已经扩展了验证以支持嵌套语法sku.*.id

    我们只需要将 Validator 替换为 CustomValidator,最后一步是更改文件 config/app.php 并在 ServiceProviders 数组中查找 ValidatorServiceProvider ,只需注释该行并添加我们的扩展服务提供商,如下所示:

    ....
    // Illuminate\Validation\ValidationServiceProvider::class,
    App\Validator\CustomValidatorServiceProvider::class,
    ....
    

    我们将其注释掉的原因是,每当您将 Laravel 5.1 更新到 5.2 时,您只想取消注释,从列表中删除我们的 CustomValidatorServiceProvider,然后删除我们的 app/Validator 文件夹,因为我们不再需要它了.

    【讨论】:

    • 太棒了!谢谢。我不知道 5.2 有这个功能。我正在使用 5.1 如果我更新它会中断吗?
    • @Ajeesh 你应该注意一些事情,Laravel 5.2 处于 beta 阶段,但你可以在这里查看迁移指南here 你可以做的是创建你的验证逻辑,当 Laravel 5.2 稳定版发布,您更新到了这种验证嵌套数组的新方法。
    • 我会这样做的。因此,与其更新到测试版,不如在稳定版发布后更新。如果我必须为此编写自定义逻辑,你可以给我看一个例子并更新你的答案吗?
    • 感谢您的详尽回答!
    • @Ajeesh CustomValidator 缺少一个函数,我已经编辑了答案,查看函数 getInlineMessage 并将其添加到您的 CustomValidator
    猜你喜欢
    • 2019-04-15
    • 2016-08-21
    • 2013-08-01
    • 2019-07-25
    • 2014-05-18
    • 1970-01-01
    • 2023-01-03
    • 1970-01-01
    • 2014-05-18
    相关资源
    最近更新 更多