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

[QUESTION] - How validation rules works? #533

Open
sacerro opened this issue Jan 16, 2025 · 0 comments
Open

[QUESTION] - How validation rules works? #533

sacerro opened this issue Jan 16, 2025 · 0 comments

Comments

@sacerro
Copy link

sacerro commented Jan 16, 2025

I'm trying to validate the entire group of fields because the validation condition for the field "Related field" depends on the option selected by the user in "Whatever" field.

This is the extract Nova Resource definition of the field inside "fields" function.

            Flexible::make('My group of fields', 'group_id')
                    ->addLayout(
                        'Group Name',
                        'group_name_id',
                        [
                            Select::make('Whatever', 'whatever')
                                  ->options(['array', 'of', 'options'])
                                  ->rules('required'),

                            Text::make('Related field', 'related_field')
                                ->rules('required'),

                            Number::make('Other unrelated field', 'other_unrelated_field')
                                  ->min(0)
                                  ->max(100)
                                  ->rules('required', 'numeric', 'min:0', 'max:100'),
                        ]
                    )->rules(['array', new MyCustomRule()])

And here is MyCustomRule class:

class MyCustomRule implements Rule
{
    public function passes($attribute, $value): bool
    {
        // Validation login. Returns false if error. Adds every error to an array: [ 0 => [ 'field_id' => 'Error message' ], 1 => [ 'field_id' => 'Error message' ], ..., n => [ 'field_id' => 'Error message' ] ]
    }
    public function message()
    {
        $messages = [];
        foreach ($this->errors as $layoutIndex => $fieldErrors) {
            foreach ($fieldErrors as $field => $error) {
                $messages[sprintf('groud_id.%d.attributes.%s', $layoutIndex, $field)] = $error;
            }
        }

        return $messages;
    }
}

I'm trying to return the errors as:
group_id.0.attributes.field_id: ERROR MESSAGE
group_id.2.attributes.other_unrelated_field: OTHER ERROR MESSAGE

Because I want to display the error below the field with errors. If I dump the $message variable the array structure seems to be as defined, but the response of the request is something like:

 {"message":"The given data was invalid.","errors":{"group_id":["ERROR MESSAGE"]}}

I'm not sure how to display errors using Flexible. Can anybody help me? I'm trying to find it in the documentation but I cannot (I'm using Laravel 8 with al older version of Nova v.3 [and I cannot upgrade]).

The error is displayed under the entire Flexible block and not below input field with errors.

image

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

No branches or pull requests

1 participant