Search code examples
cakephp-2.5cakephp-2.x

How to prevent an empty form submission into CakePHP 2.5.2


I am new to CakePHP, how can i use javascript to prevent a form being submitted empty, i mean with all fields empty ?

The user just hit the submit button

I am using CakePHP 2.5.2


Solution

  • You don't need Javascript for such minor blank validation stuff. Just go through the validation section.

    Check out this example below:

    Model: User.php has the following validation code for email field.

    class User extends AppModel {
      public $validate = array(
       'email' => array(
         'required' => true,
         'allowEmpty' => false,
         'message' => 'Email cannot be empty.'
         )
      );
    

    Setting required to true, and allowEmpty to false does the job for you. Also, the "message" field acts as the icing on the cake which indicates the error message to be shown when the validation fails.

    Peace! xD