Skip to content

dependsOn not working as expected for fields with the same attribute name #4648

@stefblokdijk

Description

@stefblokdijk
  • Laravel Version: 9.22.1
  • Nova Version: 4.12.13
  • PHP Version: 8.1

Description:

When you have 2 different fields with the same name, the dependsOn is experiencing the following issues

  • Field shows at the same time with the same label, help text etc (Experienced when using Number and Text together)
  • Required validation rule is ignored even if they are set on 1 of the fields (Experienced with Number and Text together, and with Number and a custom field together)

Detailed steps to reproduce the issue on a fresh Nova installation:

Select::make('Field Type')
    ->options([
        'text' => 'Text',
        'number' => 'Number',
    ])
    ->onlyOnForms()
    ->displayUsingLabels(),

Number::make('Number', 'field_value')
    ->help('Number test')
    ->onlyOnForms()
    ->dependsOn(
        ['field_type'],
        function (Number $field, NovaRequest $request, FormData $formData) {
            if ($formData->field_type == 'number') {
                $field
                    ->show()
                    ->rules('required');
            } else {
                $field
                    ->hide();
            }
        }
    ),

Text::make('Text', 'field_value')
    ->help('Text test')
    ->onlyOnForms()
    ->dependsOn(
        ['field_type'],
        function (Text $field, NovaRequest $request, FormData $formData) {
            if ($formData->field_type == 'text') {
                $field
                    ->show()
                    ->rules('required');
            } else {
                $field
                    ->hide();
            }
        }
    ),

Metadata

Metadata

Assignees

Labels

bugVerified bug by the Nova team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions