We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Working with Models: Validation Messages - Application exits before getMessages

Following the example given here I have written:

        if($this->request->isPost() == true) {

            $new_record= new NewRecord();
            $new_record->name = $this->request->getPost("Name");
            $new_record->label = ucwords($new_record->name);

            if ($new_record->create() == false) {
                    echo "Umh, We can't store a New Record right now: \n";
                    foreach ($new_record->getMessages() as $message) {
                        echo $message, "\n";
                    }
                } else {
                    $this->flashSession->success("News Record created");
                    return $this->response->redirect('admin/news/newrecord');
                }
        }

In the database I have a unique constraint on the 'name' field in table 'NewRecord'.

The problem I am having is that when I create a duplicate record coming up against the unique constraint the application is exiting and ouputting:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'Interview' for key 'news_group_label_UNIQUE'

This happens before getting to the first echo statement in the if.

Is this happening because I have not mapped the constraint in the model? ( I haven't)

In phalcon do I have to explicitly map the database constraints in the model?

Is the style to make all constraints in the model and leave the database alone?



33.8k
Accepted
answer

https://docs.phalcon.io/es/latest/reference/models.html#validating-data-integrity

I would add a validation method to your model. Then, create a private variable that sees if the original name has been changed; if then, add to the field an Uniqueness validator (otherwise, when you update the record and leave the original name, the validator will trigger up, making error).

Try that.



47.7k
edited Jan '15

This works fine in my NewRecord model:

public function validation()
{
    $this->validate(new Uniqueness(
        array(
            "field"   => "name",
            "message" => "The New Record name must be unique."
            )
    ));

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

So the database rules do not appear to be automatically mapped to the model and this type of functionality is not built in.

There appears to be a caveat that database rules must be declared at the Model level to be handled as messages.



33.8k

Remember to add the validator only if you modify the original field value. If you're editing the record, and edit all fields except that one, the validator will trigger up, because the value already exists (which is he itself):

public function validation()
{
    if ($this->fieldHasChanged)
    {
      $this->validate(new Uniqueness(
          array(
              "field"   => "name",
              "message" => "The New Record name must be unique."
              )
      ));
    }

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

And about the DB rules like UNIQUE, yeah, they aren't mapped automatically; however, when you insert/update the record, the app will throw an exception if you break a rule (obviously) (they will be writed in the server's error.log).