activeListbox with no selection POSTs nothing

Hello, All

This seems like such a basic problem that I feel that I must be missing something. In my application, Managers are related m-to-m to Departments. A Manager has a private member $_deptsInput that is used with getDepts() and setDepts() to maintain an array() of depts in the Manager model. When the getter is called, if $_deptsInput is not an array, it is looked up in the database. $_deptsInput is used with an activeListbox to select Departments on the Managers form.

Everything works fine except when a Manager with Departments has all of them removed on the Manager form. The activeListbox submits nothing for the depts attribute. So when the getter is called, it sees that the value has never been set (even though the POST has set other attributes – and would set depts if one or more were selected). The getter looks depts up in the database, supplying the old array of depts, while the rest of the Manager model has the POSTed values in the model. This is only an issue when depts are entirely removed.

Any ideas what the correct solution is?

Thanks,

Greg

Sounds like a browser issue, and it might even be specified behavior. Anyway, you could derive that it’s empty by checking whether the page is called using a POST request. If the data from your listbox isn’t available, you can assume it was empty when the form was submitted.

For example, you could do something like this before assigning the Model attributes:


if(Yii::app()->request->isPostRequest && !isset($_POST['deptsInput'])) { $_POST['deptsInput'] = array(); }

Thanks, Sander

That worked perfectly. I knew there had to be an easy solution. :)

Greg