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

display of multiple selected values / binding eloquent model values to multi-choice values #579

Open
profitGeeks opened this issue Jan 14, 2020 · 1 comment

Comments

@profitGeeks
Copy link

profitGeeks commented Jan 14, 2020

Hi Kris,

I have found that when adding a multi-checkbox 'choice' to a Form class it fails to bind when there are multiple values. Only plain string data parses out correctly.

for example, I have in a Kris\LaravelFormBuilder\Form extension class:

$this->add('history', 'choice', [
	'choices' => [
		'heroin' => 'Heroin',
		'alcohol' => 'Alcohol',
		'cocaine' => 'Cocaine / Crack',
	],
	'expanded' => true,
	'multiple' => true
])

This results in a string ["heroin","alcohol", "cocaine"] (a simple json string) saved to the database when all boxes are checked.

However, any multiple selected fields do not properly bind/display when the data from the Eloquent Model is read back into the form using the 'mode' option.

To get around this problem, I had to resort to pulling any multi-select items out into the form controller as an addendum using "addAfter" so I could hard code the 'selected' option processing the string with json_decode.

$form->addAfter('drug_types', 'history', 'choice', [
	'choices' => [
		'heroin' => 'Heroin',
		'alcohol' => 'Alcohol',
		'cocaine' => 'Cocaine / Crack',
	],
	'selected' => json_decode($model->history),
	'expanded' => true,
	'multiple' => true
]);

Should the parsing of multi-selected value work "automatically" as well in the binding process? Is there something I missed in saving the data?

Thanks!

@rudiedirkx
Copy link
Collaborator

I don't use type 'choice'. @kristijanhusak ?

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

2 participants