模型验证(Validating Models)¶
验证模型数据完整性(Validating Data Integrity)¶
在模型 Phalcon\Mvc\Model 中提供了几个事件(beforeSave、beforeCreate、beforeUpdate)用以数据的验证和过滤。 我们推荐使用专用的事件 validation,它允许我们调用内置的验证类进行验证数据:
<?PHP
use Phalcon\Mvc\Model;
use Phalcon\Validation\Validator\uniqueness;
use Phalcon\Validation\Validator\InclusionIn;
class Robots extends Model
{
public function validation()
{
$validation = new Phalcon\Validation;
$validation->add("type", new InclusionIn(
array(
"DOMain" => array("Mechanical", "Virtual")
)
);
$validation->add("name", new Uniqueness(
array(
"message" => "The robot name must be unique"
)
);
return $this->validate($validation);
}
}
上面的例子使用了内置验证器`InclusionIn` 和 Uniqueness,更多的内置验证器请查看 验证 章节。
除了内置的验证程序,我们可以创建自定义验证器:
<?php
use Phalcon\Validation\Validator;
use Phalcon\Validation\ValidatorInterface;
use Phalcon\ValidationInterface;
class MaxMinValidator extends Validator implements ValidatorInterface
{
public function validate(ValidationInterface $validator, $field, $allowEmpty = FALSE)
{
$min = $this->getOption('min');
$max = $this->getOption('max');
$value = $validator->getValue($field);
if ($min <= $value && $value <= $max) {
$message = new Phalcon\Validation\Message("The field doesn't have the right range of values", $field, "MaxMinValidator", 0);
$validator->appendMessage($message);
return false;
}
return true;
}
}
Adding the validator to a model:
<?php
use Phalcon\Mvc\Model;
class Customers extends Model
{
public function validation()
{
$validation = new Phalcon\Validation;
$validation->add("price", new MaxMinValidator(
array(
"min" => 10,
"max" => 100
)
);
return $this->validate($validation);
}
}
The idea of creating validators is make them reusable between several models. A validator can also be as simple as:
<?php
use Phalcon\Mvc\Model;
use Phalcon\Validation\Message;
class Robots extends Model
{
public function validation()
{
if ($this->type == "Old") {
$message = new Message(
"Sorry, old robots are not allowed anymore",
"type",
"MyType"
);
$this->appendMessage($message);
return false;
}
return true;
}
}
可以通过覆盖 Phalcon\Mvc\Model::getMessages()
方法对默认验证信息就行转换:
<?php
use Phalcon\Mvc\Model;
class Robots extends Model
{
public function getMessages()
{
$messages = array();
foreach (parent::getMessages() as $message) {
switch ($message->getType()) {
case 'InvalidCreateAttempt':
$messages[] = 'The record cannot be created because it already exists';
break;
case 'InvalidUpdateAttempt':
$messages[] = 'The record cannot be updated because it doesn\'t exist';
break;
case 'PresenceOf':
$messages[] = 'The field ' . $message->getField() . ' is mandatory';
break;
}
}
return $messages;
}
}
验证失败事件(Validation Failed Events)¶
Another type of events are available when the data validation process finds any inconsistency:
Operation | Name | Explanation |
---|---|---|
Insert or Update | notSaved | Triggered when the INSERT or UPDATE operation fails for any reason |
Insert, Delete or Update | onValidationFails | Triggered when any data manipulation operation fails |