简体   繁体   English

尊重验证无法验证数组项目

[英]Respect validation not able to validate array items

I am using respect built in validation from this source in my php application " https://respect-validation.readthedocs.io/en/1.1/rules/Each/ ". 我在php应用程序“ https://respect-validation.readthedocs.io/en/1.1/rules/Each/ ”中使用从此源建立的内置验证。 I am passing data via ajax call to php application code like 我通过ajax调用将数据传递给php应用程序代码,例如

print_r($itemsArray);

Array
(
  [0] => Array
    (
        [itemName] => apple
    )

 [1] => Array
    (
        [itemName] => banana
    )

 [2] => Array
    (
        [itemName] => cherry
    )
)

I have applied validation to the below fields in php code side like. 我已经将验证应用于以下类似PHP代码的字段。

   $fullName = $_REQUEST['fullName'];
   $email = $_REQUEST['email'];
   $phoneNumber = $_REQUEST['phoneNumber'];
   $age = $_REQUEST['age'];
   $itemsArray = $_REQUEST['itemsArray'];

   try 
   {
      v::key('fullName', v::notEmpty()->setTemplate("Full Name: Required field"))
       ->key('fullName', v::alpha()->setTemplate("Full Name: Alphabets only"))
       ....
       ....
       ->key('itemsArray', v::arrayVal()->each(v::alpha())->setTemplate("Item must contain Alphabets"))

         ->assert(['fullName' => $fullName, 'email' => $email, 'phoneNumber' => $phoneNumber, 'age' => $age, 'itemsArray' => $itemsArray ]);    
   }
    catch(NestedValidationException $e) 
   {
      $errorMessage = $e->getMessages();
   }

All the validation which are applied for fields like fullname, email, phonenumber and age are working perfectly. 适用于全名,电子邮件,电话号码和年龄等字段的所有验证均正常运行。 The only issue is with itemsArray key (validation rule) applied to this array. 唯一的问题是将itemArray键(验证规则)应用于此数组。 It displays error message for the above items despite these items are correct according to the validation rule v::alpha(). 尽管根据验证规则v :: alpha()这些项目正确,但仍会显示上述项目的错误消息。 Also it throws validation message 4 times instead of three times. 此外,它还会引发验证消息4次而不是3次。 The below is the output for the three items apple, banana and cherry. 以下是苹果,香蕉和樱桃这三个项目的输出。

 Array
 (
    [0] => Item must contain Alphabets
    [1] => Item must contain Alphabets
    [2] => Item must contain Alphabets
    [3] => Item must contain Alphabets
 )

I am not able to track the issues. 我无法跟踪问题。 Please help !!! 请帮忙 !!!

您可以这样实现,请尝试。

->key('itemsArray', v::arrayVal()->each( v::key('itemName', v::alpha()))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM