I have this code:
echo $form->dropDownList($model,
'defaultPrinterId',
CHtml::listData(Printer::model()->findAll(), 'id', 'name'),
array('prompt' => '-- None--'));
Which gives me a dropdown list like this:
<select id="LabelType_defaultPrinterId" name="LabelType[defaultPrinterId]">
<option value="">-- None --</option>
</select>
However, when the form posts, it adds a value to my table where the defaultPrinterId is 0. Instead of that, how would I make it null, since it’s a nullable field?
In your controller, after you load the attributes from
$_POSTdo a$model->defaultPrinterId = $model->defaultPrinterId ? $model->defaultPrinterId : null;This changes
0tonullotherwise leaves it unmodified.