Skip to content

Validator extensions and DataAware #43982

Answered by Tofandel
Tofandel asked this question in General
Discussion options

You must be logged in to vote

Seems the last parameter of the function is the validator, so I was able to do this instead

    public function validate($attribute, $value, $parameters, Validator $validator)
    {
        $this->parameters = $parameters;

        if ($this instanceof ValidatorAwareRule) {
            $this->setValidator($validator);
        }
        if ($this instanceof DataAwareRule) {
            $this->setData($validator->getData());
        }

        $res = $this->passes($attribute, $value);
        if (!$res) {
            $validator->fallbackMessages[Str::snake(class_basename($this))] = $this->message();
        }
        return $res;
    }

And

$validator->extend(class_basename($rule), $rule);

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by Tofandel
Comment options

You must be logged in to vote
1 reply
@Sophist-UK
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants