Aug 2, 2014

Ketan Patel

Difference between Required and notEmpty in CakePHP


Difference between required and notEmpty in CakePHP



In cakephp,various validations rules are used to validate your forms like required, min length, maxlength etc.Such validation rules makes form handling much easier. But the thing we are going to discuss here is the difference between required and nonEmpty() rules. The use of both is quite easy to understand, but the interesting thing is difference between them.


If you don't know how to use them or define in the model file, then below are examples of each.


public $validate = array(
    'fieldName1' => array(
        'rule'       => 'ruleName',
        'required'   => true,
        'message'    => 'Your Error Message'
    ),

'fieldName2' => array(
        'rule'       => 'ruleName',
        'allowEmpty' => false,
        'message'    => 'Your Error Message'
    )

);

If we have one field called 'title' and we have written the below validation rule,

'title' =>(
          'rule' => 'notEmpty',
          'message'=>'Please Fill this Field'
        )


Above code will just check that the “title” field must not be left blank, BUT only if its present..!!


Say for example, form has two fields, title and body.On inspecting the form using firebug it will looks like,

<form action="/posts/add" id="PostAddForm" method="post" accept-charset="utf-8">
<div style="display:none;">
<input type="hidden" name="_method" value="POST">
</div>
<div class="input text">
<label for="PostTitle">Title</label>
<input name="data[Post][title]" maxlength="50" type="text" id="PostTitle">
</div>
<div class="input textarea">
<label for="PostBody">Body</label>
<textarea name="data[Post][body]" rows="3" cols="30" id="PostBody"></textarea>
</div>
<div class="submit">
<input type="submit" value="Save Post">
</div>
</form>

Now if you remove the “title” field from the form and submit the form then it will successfully submited..!!   What happened with the model rule nonEmpty ??

Reason is nonEmpty simply check the Present Data validation, on the other end by using required=>true you are telling cakephp that on submission of the form this field must be there in the data array. If you remove the field having the required=>true validation and submit the form then it will throw the validation error.

We have discussed about the difference between the required and nonEmpty, but there is one similar term called 'allowEmpty'.The difference between required and allowEmpty can be confusing.


allowEmpty


- allowEmpty will check for the numeric and nonempty value
- If set to false, the field value must be nonempty,
- "nonempty" is defined as !empty($value) ||is_numeric($value). The numeric check is so that CakePHP does the right thing when $value is zero.

  'title' => array(
  'allowEmpty' =>true,
  'rule' => 'alphaNumeric',
  'required'=>true
  ),


If you write above code then it will allow empty field,but if you have input the data that doesn't match with other rule(s) (here alphaNumeric) then it will throw error.


Note : 'required' and 'allowEmpty'can not be used without defining rule.It will throw an error for the validation handler.

Example, 
You can not write like this.
'title' =>array(
'required'=>true
)
or


'title' =>array(
'allowEmpty' =>false,
)

you have to write like this,

'title' =>array(
'rule' => 'alphaNumeric',
'required'=>true
)
or

'title' =>array(

'rule' => 'alphaNumeric',
'allowEmpty' =>false
)

Ketan Patel

About Ketan Patel -

I have developed a wide range of websites using CorePHP, Opencart, CakePHP and CodeIgniter including sites for startup companies and small businesses. Apart from my blogging life, I like to read Novels, Listening music and Net surfing.

Subscribe to this Blog via Email :