Model validation messages

I have the validation in my models, and I'm trying to use my custom error messages, but so far I can get only the standard error messages.

Code from my model:

    public function validation() {

        $this->validate(new PresenceOf(
                array(
                    "field" => "name",
                    "message" => "Type in your name, please!"
                )
        ));

        return $this->validationHasFailed() != true;
    }

In one of the actions:

        if ($this->request->isPost()) {
            if ($supplier->save()) {
              //some code here
            } else {
                foreach ($userModel->getMessages() as $message) {
                    $this->flashSession->error($message);
                }
            }
        }

And I can get as error message is "name is required". How can I retrieve my custom error messages and pass them to the flash service?

It sounds like you database is set up to not allow the name column to be NULL. The automatic validation Phalcon does is finding that problem and generating the error. I guess that stops other validation from happening.

One thing you can do is override the getMessages() function. That will allow you to change the default message to something more pleasant. Here's my implementation:

public function getMessages($filter = NULL){
        $messages = array();
        foreach (parent::getMessages() as $message) {
            switch ($message->getType()) {
                case 'InvalidCreateAttempt':
                    $message->setMessage('The Program cannot be created because it already exists');
                    break;
                case 'InvalidUpdateAttempt':
                    $message->setMessage('The Program cannot be updated because it already exists');
                    break;
                case 'PresenceOf':
                    switch($message->getField()){
                        case 'name':
                            $message->setMessage('Program name must be provided');
                            break;
                        case 'start_date':
                            $message->setMessage('Start date must be provided');
                            break;
                        case 'end_date':
                            $message->setMessage('End date must be provided');
                            break;
                        case 'application_deadline':
                            $message->setMessage('Application deadline must be provided');
                            break;
                        default:
                            $message->setMessage('"'.$message->getField().'" must be provided');
                        break;
                    }
                break;
            }
            $messages[] = $message;
        }
        return $messages;
    }

As a sidenote, I have edited your post to have proper syntax highlighting. Please take a loo so you can do so for yourself in the future.

You are right that the related column is set to NOT NULL and the authomatic validation was triggered. I've found a workaround by calling the validation() method myself, something like:

    if ($this->request->isPost()) {
        if ($supplier->validation() && $supplier->save()) {

        }else{
            foreach($supplier->getMessages() as $message){
                $this->flashSession->error($message);
            }
        }
    }

In this way I can get my custom validation messages, defined in the validation() method.



1.7k

Venelin!

Another solution - in my opinion the simpliest one is to switch off automatic notNullValidations in your model.

To do this, write this in your model:

class Supplier extends \Phalcon\Mvc\Model {

  public function initialize(){

    $this->setup(
        array('notNullValidations'=>false) //switch off
    );
  }
}

This way:

  1. Automatic validation will NOT be performed. And default messages "XXX is required" will NOT be generated.
  2. Your PresenceOf validation which you have defined in your validation() method WILL be performed and your own message WILL be generated.
  3. You don't need your workaround (to type $supplier->validation() && in your if in your controllers).
  4. You don't need to override any method.

More about this: