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

accept closure attributes (eg wrapper.class) #698

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

rudiedirkx
Copy link
Collaborator

@rudiedirkx rudiedirkx commented Oct 15, 2022

This change accepts attributes to be closures (resolved by Laravel's value()). That's not useful for most fields, because all context is present where the field is defined. But it is useful for collection fields, because you define the same attributes for all the items/child forms. This way you can dynamically add different attributes for different items:

class ParentForm {
	function buildForm() {
		$this->add('items', 'collection', [
			'type' => 'form',
			'prototype' => false,
			'empty_model' => fn() => new ChildModel,
			'options' => [
				'class' => ChildForm::class,
				'wrapper' => [
					// current
					'class' => 'non-dynamic class',
					// new, optional syntax
					'class' => function(\Kris\LaravelFormBuilder\Fields\FormField $field) {
						$model = $field?->getForm()?->getModel();
						return 'use $model->id to add highlight class';
					},
				],
			],
		]);
	}
}

I use this to highlight some collection items if they contain problems.

$field is the collection item field. In this case a ChildFormType because type = form. It could be a InputType, which doesn't have a getForm() and doesn't really know anything useful. It's most useful for child forms.

@Eloar
Copy link
Contributor

Eloar commented Apr 12, 2023

Why it is not yet merged? It seems to be neat one to be introduced

@rudiedirkx
Copy link
Collaborator Author

Because I didn't need it after all, and change is scary.

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.

None yet

2 participants