I am using respect built in validation from this source in my php application "https://respect-validation.readthedocs.io/en/1.1/rules/Each/". I am passing data via ajax call to php application code like
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.
$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. It displays error message for the above items despite these items are correct according to the validation rule v::alpha(). Also it throws validation message 4 times instead of three times. 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 !!!
You can achieve like this, please try.
->key('itemsArray', v::arrayVal()->each( v::key('itemName', v::alpha()))