Skip to content

Add validation for nested form #121

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

Merged
merged 2 commits into from
Jan 15, 2021

Conversation

alberto-bottarini
Copy link
Collaborator

This PR add the possibility to add custom validation rules to NestedForm instance.

I usually need a sort of "AvoidDuplicates" rule. Take this screenshot:

Screenshot 2020-12-17 at 18 40 23

With this PR developers can create rules that validate the whole field, having access to all the nested properties.

Example has been implemented in this way:

//in Resource.php
NestedForm::make('Addresses', 'addresses', Address::class)
                ->rules(new AvoidDuplicates)
//in Rules/AvoidDuplicates.php
    public function passes($attribute, $value)
    {
        //value is a multi-dimension array with all the nested-form properties
        $cities = array_column($value, 'city');
        return count($cities) == count(array_unique($cities));
    }

@alberto-bottarini alberto-bottarini merged commit d67f6e1 into yassilah:master Jan 15, 2021
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.

1 participant