Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You're right the getMessages() method was missing in the implementation. For now you can add it to your model while it's officially added to the C class:</p> <pre><code>&lt;?php class Users extends Phalcon\Mvc\Collection { public function getMessages() { return $this-&gt;_errorMessages; } } </code></pre> <p>1- How do I show the error messages that are a result of a validation? Check above</p> <p>2- How do I create custom validators?</p> <p>A validator is a class that inherits from "Phalcon\Mvc\Model\Validator" and implements "Phalcon\Mvc\Model\ValidatorInterface":</p> <pre><code>&lt;?php use Phalcon\Mvc\Model\Validator, Phalcon\Mvc\Model\ValidatorInterface; class HashValidator extends Validator implements ValidatorInterface { public function validate($record) { $fieldName = $this-&gt;getOption('field'); if (!preg_match('/[a-z]+/', $fieldName) { $this-&gt;appendMessage("The hash is not valid", $fieldName, "Hash"); return false; } return true; } } </code></pre> <p>If you don't want to reuse the validator you can simply add a validation rule to the model:</p> <pre><code>&lt;?php use Phalcon\Mvc\Model\Message; class Users extends Phalcon\Mvc\Collection { public function validation() { if (!preg_match('/[a-z]+/', $this-&gt;password) { $this-&gt;_errorMessages[] = new Message("The hash is not valid", "password", "Hash"); return false; } return true; } public function getMessages() { return $this-&gt;_errorMessages; } } </code></pre> <p>3- Is Phalcon\Mvc\Model\Validator\Uniqueness validator NOSQL compatible? This validator is only compatible with SQL models, however, you can create a validator to work with NoSQL collections this way:</p> <pre><code>&lt;?php use Phalcon\Mvc\Model\Validator, Phalcon\Mvc\Model\ValidatorInterface; class UniqueValidator extends Validator implements ValidatorInterface { public function validate($record) { $field = $this-&gt;getOption('field'); if ($record-&gt;count(array("field" =&gt; $record-&gt;readAttribute($field)))) { $this-&gt;appendMessage("The ".$field." must be unique", $fieldName, "Unique"); return false; } return true; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload