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

dependsOn for siblings #61

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open

Conversation

Norgul
Copy link

@Norgul Norgul commented Jul 17, 2024

Created functionality to enable dependsOn for siblings.

Disclaimer: I haven't tested the feature extensively in each and every scenario, so if anyone else would benefit from this feature, please try it out and let me know if it fails somewhere.

Copying usage instructions I wrote in the readme:


dependsOn() function is made available for fields within the SimpleRepeatable. To make the feature available, be sure
to use DependsOnSiblings trait on the resource you want to have dependsOn() available for sibling fields.

Key on which the field depends on is constructed out of two parts: {parent attribute}.{child attribute}. This key
will be made available to fetch within the function via $request->get('parent.child').

Example:

SimpleRepeatable::make('Adding', 'parent', [
    // Depending on 1 field
    Text::make('Child'),
    Text::make('Dependent Child')
      ->dependsOn('parent.child', function ($field, NovaRequest $request, FormData $formData) {
          $attribute = $request->get('parent.child');
      }),

    // Depending on multiple fields
    Text::make('Second Child'),
    Text::make('Third Child'),
    Text::make('Really Dependent Child')
      ->dependsOn(['parent.second_child', 'parent.third_child'], function ($field, NovaRequest $request, FormData $formData) {
          $attribute1 = $request->get('parent.second_child');
          $attribute2 = $request->get('parent.third_child');
      }),

])

@Norgul Norgul mentioned this pull request Jul 17, 2024
@laravelwebdev
Copy link

laravelwebdev commented Sep 12, 2024

Can depends on work for sibling depennds on filed outside simple repeatable?

Select::make('Test');
SimpleRepeatable::make('Adding', 'parent', [
Text::make('Child')->dependsOn('test'.......,
]);

@Norgul
Copy link
Author

Norgul commented Sep 12, 2024

@laravelwebdev if I remember correctly while testing, that feature should be available out-of-the-box, but it begs the question of what happens with your repeatable fields. Because if you added 10 fields, and then change Select to something else, all 10 fields will be affected.

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.

2 participants