Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] setValue() in Validator #46716

Merged
merged 5 commits into from
Apr 11, 2023

Conversation

joelharkes
Copy link
Contributor

Followup of: #46162

Why

For package development, i would like to make it possible for rules to override data to make the validation 'type-safe'.
There is now no possible way to efficiently update a value inline. i can call setData() but it would completly reload the rules itself as well cause in big slowdown.

Alternatively I could sub-class Validator and add it myself, but this would greatly risk my package no longer being compatible with other packages AND people their own code repository.

Usage:

abstract class BaseTypeSafeRule implements ValidationRule, DataAwareRule, ValidatorAwareRule
{
    private array $data;
    private Validator $validator;

    public function setData(array $data)
    {
        $this->data = $data;
    }

    public function setValidator(Validator $validator)
    {
        $this->validator = $validator;
    }

    protected function modifyValue(string $attribute, mixed $value): void
    {
       // with this PR:
       $this->validator->setValue($attribute, $value);

        // previously: very slow!
        Arr::set($this->data, $attribute, $value);
        $this->validator->setData($this->data);
    }

    protected function translate(string $key, array $replacements = []): string|array
    {
        return $this->validator->getTranslator()->get($key, $replacements);
    }
}

@taylorotwell
Copy link
Member

Please fix the docblock to match the rest of the framework.

@taylorotwell taylorotwell marked this pull request as draft April 10, 2023 14:01
@joelharkes joelharkes marked this pull request as ready for review April 11, 2023 07:20
@joelharkes
Copy link
Contributor Author

@taylorotwell I fixed to docs according to styleCI.
I don't see it anywhere but is there a linter for this that i can just run to do it for me?

@joelharkes joelharkes changed the title [10.x] Set value in Validator [10.x] setValue() in Validator Apr 11, 2023
@bert-w
Copy link
Contributor

bert-w commented Apr 11, 2023

@joelharkes Remove the parameter descriptions, they are not used anywhere else. I think that should be it. There is no styleci for it atm.

@taylorotwell taylorotwell merged commit b60b19a into laravel:10.x Apr 11, 2023
@mreduar
Copy link

mreduar commented Apr 12, 2023

I had been wanting to do this for a while. Thank you @joelharkes!

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

Successfully merging this pull request may close these issues.

None yet

4 participants