#### 驗證數據完整性(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 當任何數據操作操作失敗時觸發 |
- Welcome
- 安裝
- XAMPP 下的安裝
- WAMP 下安裝
- Apache 安裝說明
- Nginx 安裝說明
- Cherokee 安裝說明
- 使用 PHP 內置 web 服務器
- Phalcon 開發工具
- Windows 系統下使用 Phalcon 開發工具
- Mac OS X 系統下使用 Phalcon 開發工具
- Linux 系統下使用 Phalcon 開發工具
- 教程 1:讓我們通過例子來學習
- 教程 2:INVO簡介
- 教程 3: 保護INVO
- 教程4: 使用CRUD
- 教程5: 定制INVO
- 教程6: V?kuró
- 教程 7:創建簡單的 REST API
- 依賴注入與服務定位器(Dependency Injection/Service Location)
- MVC 架構(The MVC Architecture)
- 使用控制器(Using Controllers)
- 使用模型(Working with Models)
- 模型關系(Model Relationships)
- 模型事件(Model Events)
- 模型行為(Model Behaviors)
- 模型元數據(Models Metadata)
- 事務管理(Model Transactions)
- 模型驗證(Validating Models)
- Working with Models (Advanced)
- Phalcon 查詢語言(Phalcon Query Language (PHQL))
- 緩存對象關系映射(Caching in the ORM)
- 對象文檔映射 ODM (Object-Document Mapper)
- 使用視圖(Using Views)
- 視圖助手 (Tags)(View Helpers (Tags))