你說得對,實現中缺少getMessages()方法。現在,你可以把它添加到你的模型,而它的正式加入到C級:
<?php
class Users extends Phalcon\Mvc\Collection
{
public function getMessages()
{
return $this->_errorMessages;
}
}
1 - 我如何證明是一個確認的結果的錯誤信息?以上檢查
2-如何創建自定義驗證器?
驗證器是由「費爾康\的mvc \型號\驗證」繼承並實現「爾康\的mvc \型號\ ValidatorInterface」一類:
<?php
use Phalcon\Mvc\Model\Validator,
Phalcon\Mvc\Model\ValidatorInterface;
class HashValidator extends Validator implements ValidatorInterface
{
public function validate($record)
{
$fieldName = $this->getOption('field');
if (!preg_match('/[a-z]+/', $fieldName) {
$this->appendMessage("The hash is not valid", $fieldName, "Hash");
return false;
}
return true;
}
}
如果你不想重複使用驗證器你可以簡單地添加驗證規則到模型:
<?php
use Phalcon\Mvc\Model\Message;
class Users extends Phalcon\Mvc\Collection
{
public function validation()
{
if (!preg_match('/[a-z]+/', $this->password) {
$this->_errorMessages[] = new Message("The hash is not valid", "password", "Hash");
return false;
}
return true;
}
public function getMessages()
{
return $this->_errorMessages;
}
}
3-是爾康\的mvc \型號\驗證\唯一性驗證NOSQL兼容? 這個驗證只與SQL型號兼容,但是,你可以創建一個驗證與NoSQL的集合,以這種方式工作:
<?php
use Phalcon\Mvc\Model\Validator,
Phalcon\Mvc\Model\ValidatorInterface;
class UniqueValidator extends Validator implements ValidatorInterface
{
public function validate($record)
{
$field = $this->getOption('field');
if ($record->count(array("field" => $record->readAttribute($field)))) {
$this->appendMessage("The ".$field." must be unique", $fieldName, "Unique");
return false;
}
return true;
}
}
謝謝你的詳細答案。 – lobostome