Form model binding and array of checkboxes issue
Hi.
When trying to use an array of checkboxes (like name="permissions[]) with form model binding feature, the checkboxes' state were never reflecting the model data, like issued on #5078 and #2290.
In my case, I created a Role model, which has permissions as the related model (a belongsToMany relation).
The way I found to solve this was the following:
1. Make a call to the related model before inserting the checkboxes.
The related model is only added to the main model when it's called (see Illuminate\Database\Eloquent\Model::__get()). So, if it's not called, it's not present on Illuminate\Database\Eloquent\Model::$relations array.
The object_get() helper (called by Illuminate\Html\FormBuilder::getModelValueAttribute()) calls isset($model->relatedModel), which returns false if the Illuminate\Database\Eloquent\Model::$relations['relatedModel'] is not set (see the model's __isset() magic method).
Adding a line like
$model->relatedModel;
before inserting the checkboxes adds the related model to $relations array, returning true on the isset() call.
Maybe this should be checked at the model's __isset() method.
2. Add a function to process a collection of related models.
The Illuminate\Html\FormBuilder::getCheckboxCheckedState() checks against single values, not agains collections of related models.
Adding the code from @borfast's commit solved this part.
In this code, I just changed
if ($relation->id == $value) return true;
line to
if ($relation->getKey() == $value) return true;
so it works even if the primary key is other than id.
I didn't checked all this against an empty model (in case of a creation form), but I believe it works.
Yes, works like a charm. Empty models work, too. Wish that commit became part of the core.
I've run into this issue recently and I'm somewhat new to Laravel. I've read this several times, but I'm not sure I completely understand where I need to make the changes to fix this. Can you clarify the solution a bit for a n00b?
I think the main question is did you add the code from @borfast's commit hacking the core or did you extend Illuminate\Html\FormBuilder and override the getCheckboxCheckedState() function and add in his checkRelationship function?
Ok, a little Googling and I found this: http://stackoverflow.com/questions/22884764/laravel-extend-form-class. Would this be the preferred way to extend the Form class without breaking the Laravel core?
Hi phylaxis!
I made a small package to avoid having to repeat the implementation of this solution in each new project I create. Maybe it can be useful to you. You can find it at https://github.com/RodrigoBalest/Html.
Hope it helps!
Unfortunately my proposed change creates a hard dependency on Illuminate\Database\Eloquent\Collection within the FormBuilder class, which, I guess, is why it wasn't accepted. I agree that it's not the perfect solution but it would have been nice to at least get a reply from the maintainers, even if only to be told to go eat shit. Instead, all Taylor did was close the PR without even saying a word. Nice.
I contacted Graham directly and the closing of the thread was just housekeeping. They are not pursuing this because the HTML component isn't fully maintained anymore and has been removed in from the core in Laravel 5.
Well, my PR was from Sep 14 2013, over a year ago. Laravel 4.1 had just been released and 4.2 wasn't even on the horizon yet, let alone 5. It's just plain bad community management, I think.
I would understand silently closing an issue that was a year old and that no one else had commented on, but this is from August and it's a problem that several people have complained about and offered solutions for. Ignoring all those people is... well, nevermind, let's not waste our energies on that matter.