开发者

How do I use CakePHP's model validation messages instead of the controller's setFlash messages?

Description

I have a simple form on every page of my site - a single input and a submit button. The point is just for the user to enter his/her email address and hit submit.

It works if the data is an email address - but if it's not a valid email address, it submits the form, the page reloads (or whatever), and the flash message comes up instead of my model's more specific "not a valid email" error.

Question:

So, how do I use the model's validation message instead of the controller's generic one?

The form:

echo $this->Form->create('Email', array('class'=>'form1', 'url'=>'/emails/add', 'inputDefaults'=>array('label'=>false))); 
echo $this->Form->input('Email.email');
echo $this->Form->end('SIGN-UP');

The email controller "add" function (or method?):

function add() {
    if (!empty($this->data)) {
        $this->Email->create();
        if ($this->Email->save($this->data)) {
            $this->Session->setFlash(__('The email address has been saved', true));
            $this->redirect($this->referer());
        } else {
            $this->Session->setFlash(__('The email address could not be saved. Please, try again.', true));
            $this->set('error', 'custom error here');
            $this->redirect($this->referer());
        }
    }
}

And the email model:

class Email extends AppModel {
var $name = 'Email';

var $validate = array(
    'email'     => array(
        'is_valid'  => array( //named whatever we want
            'rule'      => 'notEmpty',
              开发者_运维技巧  'rule'      => array('email', true),
                'message'   => 'Please supply a valid email address.',
                'last'      => true //causes it to not check the next rule if this one fails
        ),
        'is_unique'     => array( //named whatever we want
            'rule'          => 'isUnique',
            'message'       => 'That email address is already in our database.'
        )
    ),
);
}


Dave

CakePHP does this for you, you have set the validation correctly, the error is the REDIRECT if your save fails. This loses the post data and hence the validation messages

Simply

if ($this->Email->save($this->data)) {
  $this->Session->setFlash(__('The email address has been saved', true));
  $this->redirect($this->referer());
} else {
   $this->Session->setFlash(__('There were problems saving, please see the messages below.', true));                    
}


Normally, you would use the $form helper's error options. See the section of the manual on this.

However, if you want to populate the flash message with the validation message, you can use the model method invalidFields(). Here is an example:

if (!$this->User->save($this->data)) {
    $validationErrors = $this->User->invalidFields();
    $this->Session->setFlash($validationErrors['email']); // named key of the rule
    $this->redirect('/somewhere');
}

What this does is to get the messages written in model for the validation rules that failed. While invalidFields() returns an array, you can manipulate the values within it to produce a better error message, like for example, concatenating the different error messages.

Btw, I noticed something bad in your code above. You are naming your model class as 'Email' and there is a core CakePHP class already named Email (which is the famous Email component), so I suggest you name the whole MVC for that differently.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜