You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is an issue that happens when you want to link a model to 0 records.
HTML doesn't seem to provide any way to send an empty array in form data.
So in order to save 0 values in a MtM relation using this behavior, you'd have to use a special value that represents an empty array. This can be done with unselect attribute when creating a list of inputs.
So when data is passed from controller to this behavior during $model->load(), if you selected 0 elements in the form inputs that represent our many-to-many relation, then in the attribute you'll have that special value (empty string '' by default) instead of an array. So in your attribute for the many-to-many relation, you'll have that value instead of an array. This may have a number of obvious consequences. For me it complicates validation, as you have to keep in mind (and in documentation for your project) that there may be a special value instead of empty array.
What I suggest:
Have a configuration parameter for the behavior to choose the value that represents an empty array, '' by default
Automatically translate that value into an array when accessing the many-to-many relationship attribute after loading form data into the model, so instead of this:
var_export($house->dweller_ids); // ''
you'd get this:
var_export($house->dweller_ids); // []
The text was updated successfully, but these errors were encountered:
Checkbox by default should check 2 elements. One of it was hidden and have a default value 0.
So it can save relation with false checked checkbox.
Empty string what you get it some problem with your HTML or form. By default for checkbox value is 0 or 1.
I can not tell you how to solve your problem. You must build an HTML element so that an array with IDs is sent to the backend. I'm sure there are many ways to do this.
There is an issue that happens when you want to link a model to 0 records.
HTML doesn't seem to provide any way to send an empty array in form data.
So in order to save 0 values in a MtM relation using this behavior, you'd have to use a special value that represents an empty array. This can be done with
unselect
attribute when creating a list of inputs.So when data is passed from controller to this behavior during
$model->load()
, if you selected 0 elements in the form inputs that represent our many-to-many relation, then in the attribute you'll have that special value (empty string''
by default) instead of an array. So in your attribute for the many-to-many relation, you'll have that value instead of an array. This may have a number of obvious consequences. For me it complicates validation, as you have to keep in mind (and in documentation for your project) that there may be a special value instead of empty array.What I suggest:
''
by defaultyou'd get this:
The text was updated successfully, but these errors were encountered: