Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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');
$this->set(compact('suppliers'));

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>
</select>

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 $suppliers array in the controller with something like

$suppliers[0] = '';

and remove the empty option from the FormHelper input?

like image 789
Devin Crossman Avatar asked Sep 11 '13 21:09

Devin Crossman


People also ask

What is the use of allowemptystring() in CakePHP?

CakePHP provides empty value support for different shapes of data: allowEmptyString () Should be used when you want to only accept an empty string. allowEmptyArray () Should be used when you want to accept an array. allowEmptyDate () Should be used when you want to accept an empty string, or an array that is marshalled into a date field.

How do I validate an array in CakePHP?

The validation package in CakePHP provides features to build validators that can validate arbitrary arrays of data with ease. You can find a list of available Validation rules in the API. Validator objects define the rules that apply to a set of fields.

What does the 'url' option do in CakePHP?

This allows CakePHP to emulate proper REST support in web browsers. Using the 'url' option allows you to point the form to a specific action in your current controller or another controller in your application.

How to use belongs to many associations in CakePHP?

The above example shows an expanded example for belongs to many associations, with separate inputs for each entity and join data record. You can also create a multiple select input for belongs to many associations: You can add custom control widgets in CakePHP, and use them like any other control type.


2 Answers

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

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

See http://www.dereuromark.de/2010/06/23/working-with-forms/

like image 135
mark Avatar answered Sep 19 '22 17:09

mark


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

You can also add required to it:

echo $this->Form->input('supplier_id', array('empty'=>'Select', 'required' => true));
like image 24
burunoh Avatar answered Sep 20 '22 17:09

burunoh