think\Validate::rule PHP Method

rule() public method

添加字段验证规则
public rule ( string | array $name, mixed $rule = '' ) : Validate
$name string | array 字段名称或者规则数组
$rule mixed 验证规则
return Validate
    public function rule($name, $rule = '')
    {
        if (is_array($name)) {
            $this->rule = array_merge($this->rule, $name);
        } else {
            $this->rule[$name] = $rule;
        }
        return $this;
    }

Usage Example

Exemplo n.º 1
0
 public function testRule()
 {
     $rule = ['name' => 'require|alphaNum|max:25|expire:2016-1-1,2026-1-1', 'account' => 'requireIf:name,thinkphp|alphaDash|min:4|length:4,30', 'age' => 'number|between:1,120', 'email' => 'requireWith:name|email', 'host' => 'activeUrl', 'url' => 'url', 'ip' => 'ip', 'score' => 'float|gt:60|notBetween:90,100|notIn:70,80|lt:100|elt:100|egt:60', 'status' => 'integer|in:0,1,2', 'begin_time' => 'after:2016-3-18', 'end_time' => 'before:2016-10-01', 'info' => 'require|array', 'info.name' => 'require|length:8|alpha|same:thinkphp', 'value' => 'same:100', 'bool' => 'boolean'];
     $data = ['name' => 'thinkphp', 'account' => 'liuchen', 'age' => 10, 'email' => '*****@*****.**', 'host' => 'thinkphp.cn', 'url' => 'http://thinkphp.cn/topic', 'ip' => '114.34.54.5', 'score' => '89.15', 'status' => 1, 'begin_time' => '2016-3-20', 'end_time' => '2016-5-1', 'info' => [1, 2, 3, 'name' => 'thinkphp'], 'zip' => '200000', 'date' => '16-3-8', 'ok' => 'yes', 'value' => 100, 'bool' => 'true'];
     $validate = new Validate($rule);
     $validate->rule('zip', '/^\\d{6}$/');
     $validate->rule(['ok' => 'require|accepted', 'date' => 'date|dateFormat:y-m-d']);
     $result = $validate->batch()->check($data);
     $this->assertEquals(true, $result);
 }
All Usage Examples Of think\Validate::rule