Devin Crossman Devin Crossman - 4 months ago 7
PHP Question

cakePHP value for empty option

is there a way to pass a value for the empty option in a select dropdown generated by the FormHelper?

I'm creating an input like this:

echo $this->Form->input('supplier_id', array('empty'=>true));

with values supplied automatically from the controller like this

$suppliers = $this->Product->Supplier->find('list');

and the select box is created like this:

<select name="data[Product][supplier_id]" class="form-control" id="ProductSupplierId">
<option value=""></option>
<option value="1">Lolë Montreal</option>
<option value="2">Spiritual Gangster</option>
<option value="3">Havaianas</option>

but I would like the first option (the empty one) to have a value of 0 instead of '' is it possible? or should I instead modify the
array in the controller with something like

$suppliers[0] = '';

and remove the empty option from the FormHelper input?


Using the verbose array syntax you can chose any value for empty:

echo $this->Form->input('supplier_id', array('empty' => array(0 => '')));