yassilah / laravel-nova-nested-form

This package allows you to include your nested relationships' forms into a parent form.
240 stars 96 forks source link

Add validation for nested form #121

Closed alberto-bottarini closed 3 years ago

alberto-bottarini commented 3 years ago

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));
    }