Ângelo Rigo
Ângelo Rigo

Reputation: 2165

Cakephp 2 validation message on beforesave model callback

I am using CakePhp 2.5 and in a beforeSave model Callback i do return false if some information (MyIndex) is not provided.

How can i display the error message ?

I try :

$this->validationRuleErrors = 'You need to choose MyIndex'; 

But did not see any error message.

if( in_array( 'MyIndex', array_keys( $this->data) ) == FALSE )
{
            $this->validationRuleErrors = 'You need to choose MyIndex';
            debug($this->validationErrors);
            return false;
}

Upvotes: 0

Views: 98

Answers (1)

bill
bill

Reputation: 1656

The validationErrors property set in your beforeSave can be accessed from your controller.

Example controller:

try {
    $this->Model->save($data);
    if (!empty($this->Model->validationErrors)) {
        // just echo $this->Model->validationErrors if you don't want to use an exception
        throw new Exception($this->Model->validationErrors);
    }
} catch (Exception $e) {
    $this->data = [
        'success' => false,
        'message' => $e->getMessage()
    ]
}

Upvotes: 1

Related Questions