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

Merging \Phalcon\Forms\Form::isValid or \Phalcon\Mvc\Model::validation messages

We have two ways of validation of user input data in phalcon.

I trying to merge my model validation with form validation in this way.

$result = $site->save();

if (!$result) {
                $messages = $site->getMessages();

                /** @var \Phalcon\Mvc\Model\Message $msg */
                foreach ($messages as $msg) {
                    /** @var \Phalcon\Forms\Element $field */
                    $field = $form->get($msg->getField());
                    $field->appendMessage(new \Phalcon\Validation\Message($msg->getMessage(), $msg->getField()));

                    var_dump($form->getMessagesFor($msg->getField()));
                    var_dump($field->getMessages());
                    exit;
                }

                // ....
}

In result i have:

object(Phalcon\Validation\Message\Group)[113]
  protected '_position' => null
  protected '_messages' => null

object(Phalcon\Validation\Message\Group)[110]
  protected '_position' => null
  protected '_messages' => 
    array (size=1)
      0 => 
        object(Phalcon\Validation\Message)[111]
          protected '_type' => null
          protected '_message' => string 'The URL must be unique' (length=22)
          protected '_field' => string 'fsurl' (length=5)

Why? What is incorrect in my code or it is a bug?

P.S. Phalcon version is 1.1.0.



98.9k

Can you try with 1.2.0?



2.5k

ok, i'll try, but in general - is this correct way for validation? How usually you do? Check only in model or only in forms?

Nobody do it like me?



98.9k

I think your idea is good, I remember fix something related to add messages to a form without an implicit validation by the form itself in 1.2.0



2.5k

Still have the same result on 1.2.0:

object(Phalcon\Validation\Message\Group)[111]
  protected '_position' => null
  protected '_messages' => null

object(Phalcon\Validation\Message\Group)[112]
  protected '_position' => null
  protected '_messages' => 
    array (size=1)
      0 => 
        object(Phalcon\Validation\Message)[113]
          protected '_type' => null
          protected '_message' => string 'The URL must be unique' (length=22)
          protected '_field' => string 'fsurl' (length=5)