You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
With a custom validation rule, there is no way to add parameters to the $validator->failed() array.
For example, I have a custom rule which warns a user if their email address does not pass DNS, but will pass on a second try.
class ValidEmail extends AbstractRule implements Rule
{
const AUTO = 'auto';
const STRICT = 'strict';
const WEAK = 'weak';
protected $alias = 'valid_email';
protected $strictness = self::AUTO;
public function __construct($strictness = self::AUTO)
{
if (in_array($strictness, [self::STRICT,self::WEAK,self::AUTO])) {
$this->strictness = $strictness;
}
}
public function passes($attribute, $value, $parameters = [], $validator = null)
{
$this->setStrictnessFrom($parameters);
if ($this->isStrict()) {
// ... validate strict
} else {
// .... validate weak
}
}
The rule also has an alias valid_email which can accept a parameter valid_email:strict or valid_email:weak.
If I use the alias as a rule, I can see the parameter in $validator->failed(). However, there is no way to put the parameter into the failed rules array if the rule is instantiated using the class name, unless I extend the validator (which seems like overkill for this use case).
Proposal
Check if a method exists on the rule class to manually pass in the parameters.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Description:
With a custom validation rule, there is no way to add parameters to the
$validator->failed()
array.For example, I have a custom rule which warns a user if their email address does not pass DNS, but will pass on a second try.
The rule also has an alias
valid_email
which can accept a parametervalid_email:strict
orvalid_email:weak
.If I use the alias as a rule, I can see the parameter in
$validator->failed()
. However, there is no way to put the parameter into the failed rules array if the rule is instantiated using the class name, unless I extend the validator (which seems like overkill for this use case).Proposal
Check if a method exists on the rule class to manually pass in the parameters.
In
ValidationRuleParser::parse
:In
Validator::validateUsingCustomRule
:Beta Was this translation helpful? Give feedback.
All reactions