Marc Rasmussen
Marc Rasmussen

Reputation: 20555

Show validation message when rule triggered

so if i have the following validation rule in my Forward model

    public $validate = array(
    'url' =>array(
        'rule' => 'url',
        'message' => 'Please supply a valid Url.'
    )
);

and i want to display the message in a flash how do i achieve that?

I have tried the following:

        $new_forward = $this->request->data;
        $this->Forward->create();
        $this->Session->setFlash($this->Forward->save($new_forward));

Also tried this with no result

$this->Session->setFlash($this->ModelName->validationErrors);

Upvotes: 0

Views: 50

Answers (1)

Dipesh Parmar
Dipesh Parmar

Reputation: 27364

You can do this using below sample code,

$this->Forward->create();
if ($this->Forward->save($this->request->data))
{
    $this->Session->setFlash(__('The Forward has been saved', true),'flash_success');
    $this->redirect(array('controller' => 'forwards','action' => 'index'));
}
else
{
    $this->Session->setFlash(__('The Forward could not be saved. Please, try again.', true),'flash_error');
}

Upvotes: 1

Related Questions