Github源码:https://github.com/phalcon/cphalcon/tree/master/phalcon/validation/validator
Phalcon 2.0 Model 字段验证:
public function validation() { $this->validate(new PresenceOf(array( "field" => 'code', "message" => '应用名称不能为空', ))); $this->validate(new Regex(array( "field" => 'code', "message" => '应用名称只能包含字母、数字、 中、下划线', 'pattern' => '/^[-A-Za-z0-9_]+$/', ))); $this->validate(new Model\Validator\Uniqueness(array( 'field' => 'code', 'message' => '应用名称已被使用' ))); $this->validate(new PresenceOf(array( "field" => 'name', "message" => '名称不能为空', ))); $this->validate(new PresenceOf(array( "field" => 'mechanismId', "message" => '请选择归属推理系统', ))); return $this->validationHasFailed() != true; }
Phalcon 3.0 Model 字段验证:
use Phalcon\Validation\Validator\PresenceOf; use Phalcon\Validation\Validator\Regex as RegexValidator; use Phalcon\Validation\Validator\Uniqueness;
= ->'mobile', 'password', "message" =>'mobile' => '手机号不能为空', 'password' => '密码不能为空',, ->'mobile', 'password', 'pattern' =>'mobile' => "/^(13[0-9]|14[0-9]|17[0-9]|15[0-9]|18[0-9])\d{8}$/", 'password' => "/^[A-Za-z0-9_]+$/",, 'message' =>'mobile' => '手机号只能包含英文、数字和下划线', 'password' => '密码只能包含英文、数字和下划线' ->'mobile', 'model' => 'User', 'message' => ':field must be unique' ->validate(