Sunday 15 August 2010

CakePHP how to submit NULL value? -


On the web form, I have the Selection-Option field. I assume the value 'card' (without the quotation marks), or the zero value deposit Have to do.

Now, the code looks like this:

  $ options = array = (= NULL = & gt; 'invoice', 'card' = & gt; '); Echo $ the-> Form- & gt; Input ('payment_messity', array ('option' => = option));   

How do I define option AR to submit zero values ​​in database table (and / or 'card' but definitely works) if that invoice Option is selected?

Thank you in advance!

The tap is an invalid key for an associative array, only 'integer' and 'string' valid array keys See the documentation:

To send an empty value through the form, you can use an empty string. To force a null value to be entered in the database, handle it inside your model or controller before saving it

  $ options = array ('' => Invoice ',' 'Card' = & gt; 'payment card'); Echo $ the-> Form- & gt; Input ('payment_messity', array ('option' => = option));   

Then inside your controller;

  If ('' === $ this- & gt; Request- & gt; Data ['MyModelName'] ['Payment_method']) {This-> Request-> Data ['MyModelName'] ['payment_math'] = null; }   

alternative approach

However, why not use value for 'invoicing' payment method? Like this:

  $ echo-> gt; Form- & gt; Input ('payment_method', array ('option' = & gt; array ('' = '' 'select your payment method', 'invoice' = & gt; 'invoice', 'card' = & gt; Card ',)));   

In IMO, there are some benefits;

  • It is more transparent, e.g. When looking inside the database, it is clear that the payment method is 'invoice' by using a 'special' value (zero), without the knowledge of your application, people will have to browse through the source code in order to work internally < / Li>
  • It is possible to check that usage has chosen payment method; As if the value is empty, it may be that you have forgotten to select the payment method, in which case you can mark the field 'invalid' through a verification rule.

No comments:

Post a Comment