Closed paxperscientiam closed 1 year ago
If I got this right, you can use a combination of setName
and setTemplate
on individual validators. Then you can use $e->getId
to figure out which field triggered the exception error.
If you want to reuse the original templates, there's an example with $pointsRule
below:
<?php
declare(strict_types=1);
require 'vendor/autoload.php';
use Respect\Validation\Rules as r;
use Respect\Validation\Exceptions\IntValException;
$pointsRule = (new r\IntVal())
->setName('Points')
->setTemplate(IntValException::STANDARD);
$planPointsRule = (new r\Number())
->setName('Points')
->setTemplate('Plan Points must be a number');
$rules = new r\AllOf(
(new r\Key("plan-selection", $pointsRule))
->setName('Plan Selection')
->setTemplate('You need to select a plan'),
(new r\Key("plan-points-goal", $planPointsRule, false))
->setName('Goal')
->setTemplate('You need to select a goal')
);
$data = [];
try {
$rules->check($data);
} catch (Respect\Validation\Exceptions\ValidationException $e) {
// "Plan Selection"
var_dump($e->getId());
// "You need to select a plan"
var_dump($e->getMessage());
}
$data = [
'plan-selection' => 'notint'
];
try {
$rules->check($data);
} catch (Respect\Validation\Exceptions\ValidationException $e) {
//"Plan Selection"
var_dump($e->getId());
//"Plan Selection must be an integer number"
var_dump($e->getMessage());
}
$data = [
'plan-selection' => 123,
'plan-points-goal' => 'dnjsndfn NOT A NUMBER'
];
try {
$rules->check($data);
} catch (Respect\Validation\Exceptions\ValidationException $e) {
//"Goal"
var_dump($e->getId());
//"Goal must be a number"
var_dump($e->getMessage());
}
As you can see, it's possible to target and identify individual validator failures. This should also work with assert
and getMessages
to get them all at once.
You can also do ->setName
and ->setTemplate
in the AllOf
rule, which is the same as new V
in your snippet. In fact, any validator supports it.
There might be some tweaking and experimentation needed to get your use case right.
If this isn't enough, I'll need you to elaborate more. Please feel free to reopen this if you can't make it work for your case.
Thanks for the awesome library!
So, here's what I'm trying to accomplish with use-case.
Goal: Given a multirule validator, with a mix of optional and required inputs, determine how to test a single arbitrary input.
Use case: presenting inline error text at
level immediately after an input field has changed. In such a scenario, the goal is not to validate the entire form, but just a single input field.The below code is my attempt at this
As you can see, this approach for validating a single input is cumbersome. I wonder, is it possible to setup a validator -- with a mix of required and optional fields -- that will validate a single input without worrying about omitted required inputs?
I hope that makes sense. Happy to clarify if it doesn't :)