1

How to display the validation message which is defined in CakePHP model.When I submit the form , CakePHP model validation works.But validation message for input field is not displayed. It shows HTML 5 validation Message. I want to display CakePHP model validation message. I am using CakePHP 2.5.3

Following is my code block:

Controller:

$this->User->set($this->request->data);
    if ($this->User->validates()) {
        if ($this->User->save($this->request->data)) {
            $Email->send();
            $this->Session->setFlash(__('The user has been created'));
            if ($this->Auth->login()) {

                $this->Session->setFlash(__('Welcome, ' . $this->Auth->user('username')));
                $this->redirect($this->Auth->redirectUrl());
            }
        } else {
            $this->Session->setFlash(__('The user could not be created. Please, try again.'));
        }
    }

Model:

public $validate = array(
    'username' => array(
        'nonEmpty' => array(
            'rule' => array('notEmpty'),
            'message' => 'A username is required',
            'allowEmpty' => false
        ),
        'unique' => array(
            'rule' => array('isUniqueUsername'),
            'message' => 'This username is already in use'
        ),
        'alphaNumericDashUnderscore' => array(
            'rule' => array('alphaNumericDashUnderscore'),
            'message' => 'Username can only be letters, numbers and underscores'
        ),
    )

View:

<div id="divfirstname" class="input text required">
                    <label for="UserFirstname">First Name <span style="color:#FF0000">*</span></label>
                     <?php echo $this->Form->text('firstname');?>
                </div>

                <div id="divlastname"  class="input text required">
                    <label for="UserLastname">Last Name <span style="color:#FF0000">*</span></label>
                    <?php echo $this->Form->text('lastname');?>
                </div>
                <div class="input text required">
                    <label for="UserUsername">Username <span style="color:#FF0000">*</span></label>
                    <?php echo $this->Form->text('username');?>
                </div>
  • possible duplicate of [Disable validation of HTML5 form elements](http://stackoverflow.com/questions/3090369/disable-validation-of-html5-form-elements) – floriank Feb 20 '15 at 12:12
  • Please consider using the search function or Google next time for this kind of trivial problems. See the linked answer in post above. – floriank Feb 20 '15 at 12:13

2 Answers2

0

You don't need to wrap the validation-block around your save call.

So this is enough:

    if ($this->User->save($this->request->data)) {
        $Email->send();
        $this->Session->setFlash(__('The user has been created'));
        if ($this->Auth->login()) {

            $this->Session->setFlash(__('Welcome, ' . $this->Auth->user('username')));
            $this->redirect($this->Auth->redirectUrl());
        }
    } else {
        $this->Session->setFlash(__('The user could not be created. Please, try again.'));
    }

CakePHP handles automatically the validation when Model->save() is called and displays the error under your input fields. So if $this->save fails because of a validation error, cakephp adds <div class="error-message">Message Text from your model validation array</div> under the right input field.

q0re
  • 1,401
  • 19
  • 32
0

You are left with 2 options :

  1. Either dont use validation rule in cakePHP which validates on Client side.

  2. On DOM ready , call $("#formId").removeAttr('novalidate'); Like

    $(document).ready(function(){ $("#formId").removeAttr('novalidate'); });

Pratik Joshi
  • 11,485
  • 7
  • 41
  • 73