模型验证(Validating Models)

验证模型数据完整性(Validating Data Integrity)

在模型 Phalcon\Mvc\Model 中提供了几个事件(beforeSavebeforeCreatebeforeUpdate)用以数据的验证和过滤。我们推荐使用专用的事件 validation,它允许我们调用内置的验证类进行验证数据:

  1. <?php
  2.  
  3. use Phalcon\Mvc\Model;
  4. use Phalcon\Validation\Validator\Uniqueness;
  5. use Phalcon\Validation\Validator\InclusionIn;
  6.  
  7. class Robots extends Model
  8. {
  9. public function validation()
  10. {
  11. $validation = new Phalcon\Validation;
  12.  
  13. $validation->add("type", new InclusionIn(
  14. array(
  15. "domain" => array("Mechanical", "Virtual")
  16. )
  17. );
  18.  
  19. $validation->add("name", new Uniqueness(
  20. array(
  21. "message" => "The robot name must be unique"
  22. )
  23. );
  24.  
  25. return $this->validate($validation);
  26. }
  27. }

上面的例子使用了内置验证器InclusionInUniqueness,更多的内置验证器请查看 验证 章节。

除了内置的验证程序,我们可以创建自定义验证器:

  1. <?php
  2.  
  3. use Phalcon\Validation\Validator;
  4. use Phalcon\Validation\ValidatorInterface;
  5. use Phalcon\ValidationInterface;
  6.  
  7. class MaxMinValidator extends Validator implements ValidatorInterface
  8. {
  9. public function validate(ValidationInterface $validator, $field, $allowEmpty = FALSE)
  10. {
  11. $min = $this->getOption('min');
  12. $max = $this->getOption('max');
  13.  
  14. $value = $validator->getValue($field);
  15.  
  16. if ($min <= $value && $value <= $max) {
  17. $message = new Phalcon\Validation\Message("The field doesn't have the right range of values", $field, "MaxMinValidator", 0);
  18. $validator->appendMessage($message);
  19. return false;
  20. }
  21.  
  22. return true;
  23. }
  24. }

Adding the validator to a model:

  1. <?php
  2.  
  3. use Phalcon\Mvc\Model;
  4.  
  5. class Customers extends Model
  6. {
  7. public function validation()
  8. {
  9. $validation = new Phalcon\Validation;
  10.  
  11. $validation->add("price", new MaxMinValidator(
  12. array(
  13. "min" => 10,
  14. "max" => 100
  15. )
  16. );
  17.  
  18. return $this->validate($validation);
  19. }
  20. }

The idea of creating validators is make them reusable between several models. A validator can also be as simple as:

  1. <?php
  2.  
  3. use Phalcon\Mvc\Model;
  4. use Phalcon\Validation\Message;
  5.  
  6. class Robots extends Model
  7. {
  8. public function validation()
  9. {
  10. if ($this->type == "Old") {
  11. $message = new Message(
  12. "Sorry, old robots are not allowed anymore",
  13. "type",
  14. "MyType"
  15. );
  16.  
  17. $this->appendMessage($message);
  18.  
  19. return false;
  20. }
  21.  
  22. return true;
  23. }
  24. }

可以通过覆盖 Phalcon\Mvc\Model::getMessages() 方法对默认验证信息就行转换:

  1. <?php
  2.  
  3. use Phalcon\Mvc\Model;
  4.  
  5. class Robots extends Model
  6. {
  7. public function getMessages()
  8. {
  9. $messages = array();
  10. foreach (parent::getMessages() as $message) {
  11. switch ($message->getType()) {
  12. case 'InvalidCreateAttempt':
  13. $messages[] = 'The record cannot be created because it already exists';
  14. break;
  15. case 'InvalidUpdateAttempt':
  16. $messages[] = 'The record cannot be updated because it doesn\'t exist';
  17. break;
  18. case 'PresenceOf':
  19. $messages[] = 'The field ' . $message->getField() . ' is mandatory';
  20. break;
  21. }
  22. }
  23.  
  24. return $messages;
  25. }
  26. }

验证失败事件(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

原文: http://www.myleftstudio.com/reference/models-validation.html