Reputation: 11151
On web form I have select-option field. It have to submit value 'card' (without quotes), or NULL value.
Now, code looks like this:
$options = array(NULL => 'Invoice', 'card' => 'Payment card');
echo $this->Form->input('payment_method', array('options'=>$options));
How can I define options array, to submit NULL value in database table (and/or 'card', but that works of course), in case that Invoice option is selected?
Thank you in advance!
Upvotes: 4
Views: 5870
Reputation: 28987
NULL is an invalid key for an associative array. Only 'integers' and 'strings' are valid array keys, see the documentation: Arrays
To send an empty value through the form, you can use an empty string. To force inserting a NULL value in the database, handle this inside your model or controller before saving
$options = array('' => 'Invoice', 'card' => 'Payment card');
echo $this->Form->input('payment_method', array('options'=>$options));
Then inside your controller;
if ('' === $this->request->data['MyModelName']['payment_method']) {
this->request->data['MyModelName']['payment_method'] = null;
}
However, why not use a value for the 'invoice' payment method? Like this:
echo $this->Form->input('payment_method', array(
'options' => array(
'' => 'Select your payment method..',
'invoice' => 'Invoice',
'card' => 'Payment card',
)
));
IMO this has some advantages;
Upvotes: 4