This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Hi, I'm stuck in a issue. I have a form where user has a multi select field. In case if the user does not select any options while submitting the form for the first time ( during create ) it works fine, but while editing the form ,the user who chose an option wants the select field to left empty since its a multi select it is possible to remove the option but cant able to empty the name array that holds the data of previously selected option. In case if I go with simple select then it becomes mandatory to select any one of the options or else the browser choose the first option as default. I know its possible to set a option with value as empty and store in Db. But I want to dynamically get the array before it is saved/updated and set it to null when empty. Thing is the array is not at all being empty when no option was chosen.
Last updated
It all depends on where your form is (frontend / backend ) and how it populates it's options.
Read: https://octobercms.com/docs/backend/forms#field-dropdown
and tells us a little more about how your options are populated.
Show some code.
Im asking for the back end.
Model Method
public function getSampleOptions()
{
return [
'a'=>'Product1',
'b'=>'Product2',
'c'=>'Product3',
'd'=>'Product4',
'e'=>'Product5'
];
}
Last updated
How about using beforeSave() method in your model to process the array? See: http://octobercms.com/index.php/forum/post/process-data-before-save?page=1
I have tried a lot. Thing is the beforeSave() function works as expected but the array still don't gets empty even though I have removed all my options from the multi Select field
How about:
public function beforeSave()
{
if ($this->sample ==[] ) $this->sample = null;
// or
if (!count($this->sample) ) $this->sample = null;
}
If not then what have you tried?
Last updated
Ive even tried this but, what I mean is when I removed all the select options from the field and if I submit the form, the field still holds the option data.
Example: If the field has 3 options a,b,c and if I empty the field , the array still holds the options a,b,c. But If I removed the options a and b, the field works fine by holding option c. Similar cases for traversing all options. Thing is The field wants at least one option to be selected or else it by default selects the options that were already chosen during previous update/create.
Last updated
1-8 of 8