任何数据验证:
<?php
use Phalcon\Validation\Validator\PresenceOf,
Phalcon\Validation\Validator\Email;
$validation = new Phalcon\Validation();
$validation->add('name', new PresenceOf(array(
'message' => 'The name is required'
)));
$validation->add('email', new PresenceOf(array(
'message' => 'The e-mail is required'
)));
$validation->add('email', new Email(array(
'message' => 'The e-mail is not valid'
)));
$messages = $validation->validate($_POST);
if (count($messages)) {
foreach ($messages as $message) {
echo $message, '<br>';
}
}
Run Code Online (Sandbox Code Playgroud)
http://docs.phalconphp.com/en/1.2.6/reference/validation.html
如果您正在使用模型:
<?php
use Phalcon\Mvc\Model\Validator\InclusionIn,
Phalcon\Mvc\Model\Validator\Uniqueness;
class Robots extends \Phalcon\Mvc\Model
{
public function validation()
{
$this->validate(new InclusionIn(
array(
"field" => "type",
"domain" => array("Mechanical", "Virtual")
)
));
$this->validate(new Uniqueness(
array(
"field" => "name",
"message" => "The robot name must be unique"
)
));
return $this->validationHasFailed() != true;
}
}
Run Code Online (Sandbox Code Playgroud)
http://docs.phalconphp.com/en/1.2.6/reference/models.html#validating-data-integrity
模型也有事件,所以你可以在这些函数中添加你需要的任何逻辑:
http://docs.phalconphp.com/en/1.2.6/reference/models.html#events-and-events-manager
| 归档时间: |
|
| 查看次数: |
1709 次 |
| 最近记录: |