ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
#### 验证数据完整性(Validating Data Integrity) Phalcon\Mvc\Model provides several events to validate data and implement business rules. The special “validation” event allows us to call built-in validators over the record. Phalcon exposes a few built-in validators that can be used at this stage of validation. Phalcon\Mvc\Model 对于验证数据和实现业务规则,提供了多个事件。特殊的“验证”事件允许我们在记录上调用内置的验证器。在这个验证阶段,有一些内置的验证器可以使用。 The following example shows how to use it: 下面的例子展示了如何使用它: ~~~ <?php namespace Store\Toys; use Phalcon\Mvc\Model; use Phalcon\Validation; use Phalcon\Validation\Validator\Uniqueness; use Phalcon\Validation\Validator\InclusionIn; class Robots extends Model { public function validation() { $validator = new Validation(); $validator->add( "type", new InclusionIn( [ "domain" => [ "Mechanical", "Virtual", ] ] ) ); $validator->add( "name", new Uniqueness( [ "message" => "The robot name must be unique", ] ) ); return $this->validate($validator); } } ~~~ The above example performs a validation using the built-in validator “InclusionIn”. It checks the value of the field “type” in a domain list. If the value is not included in the method then the validator will fail and return false. 上面的例子使用内置的验证器“InclusionIn”来执行验证。它检查域列表中的字段“type”的值。如果该值没有包含在该方法中,那么验证器将失败并返回false。 * * * * * > For more information on validators, see the Validation documentation. >要获得更多关于验证器的信息,请参阅验证文档。 * * * * * The idea of creating validators is make them reusable between several models. A validator can also be as simple as: 创建验证器的想法使它们可以在多个模型之间进行重用。验证器也可以这么简单: ~~~ <?php namespace Store\Toys; use Phalcon\Mvc\Model; use Phalcon\Mvc\Model\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; } } ~~~ 验证信息(Validation Messages) Phalcon\Mvc\Model has a messaging subsystem that provides a flexible way to output or store the validation messages generated during the insert/update processes. Phalcon\Mvc\Model 有一个消息子系统,它提供了一种灵活的方式来输出或存储在插入/更新过程中生成的验证消息。 Each message is an instance of Phalcon\Mvc\Model\Message and the set of messages generated can be retrieved with the `getMessages()` method. Each message provides extended information like the field name that generated the message or the message type: 每个消息都是一个关于 Phalcon\Mvc\Model\Message 的实例,生成的消息集可以用getMessages()来检索的方法。每个消息都提供了扩展信息,比如生成消息或消息类型的字段名: ~~~ <?php if ($robot->save() === false) { $messages = $robot->getMessages(); foreach ($messages as $message) { echo "Message: ", $message->getMessage(); echo "Field: ", $message->getField(); echo "Type: ", $message->getType(); } } ~~~ Phalcon\Mvc\Model can generate the following types of validation messages: 可以生成以下类型的验证消息: | Type 类型 | Description 描述 | | --- | --- | | PresenceOf | Generated when a field with a non-null attribute on the database is trying to insert/update a null value 当数据库中具有非null属性的字段试图插入/更新null值时生成| | ConstraintViolation | Generated when a field part of a virtual foreign key is trying to insert/update a value that doesn’t exist in the referenced model 当一个虚拟外键的字段部分试图插入/更新引用模型中不存在的值时生成 | | InvalidValue | Generated when a validator failed because of an invalid value 由于无效值而导致验证器失败 | | InvalidCreateAttempt | Produced when a record is attempted to be created but it already exists 当一个记录试图被创建时产生,但它已经存在 | | InvalidUpdateAttempt | Produced when a record is attempted to be updated but it doesn’t exist | The `getMessages()` method can be overridden in a model to replace/translate the default messages generated automatically by the ORM: `getMessages()` 可以在模型中覆盖方法来替换/转换由ORM自动生成的默认消息: ~~~ <?php namespace Store\Toys; use Phalcon\Mvc\Model; class Robots extends Model { public function getMessages() { $messages = []; 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 当任何数据操作操作失败时触发 |