zoukankan      html  css  js  c++  java
  • laravel validate学习笔记

    Laravel 自带一个简单、方便的 Validation 类用于验证数据以及获取错误消息。

    http://www.cnblogs.com/yjf512/p/4324159.html

    在model里面定义

    public function checkValidate($data){
    $rules = array(
    'email' => 'required|email',
    'name' => 'required|between:1,20',
    'password' => 'required|min:8',
    );
    $message = array(
    "required" => ":attribute 不能为空",
    "between" => ":attribute 长度必须在 :min 和 :max 之间"
    );

    $attributes = array(
    "email" => '电子邮件',
    'name' => '用户名',
    'password' => '用户密码',
    );

    $validate = Validator::make($data,$rules,$message,$attributes);
    return $validate;
    }

    在controller 里面 调用

    public function test_Validator(){
    $data = Input::all();
    $user = new User();
    $validate = $user->checkValidate($data);

    if($validate->fails()){
    $warnings = $validate->messages();
    $show_warning = $warnings->first();
    print_r($warnings);
    print_r($show_warning);
    }else{
    echo 'aaa';
    }
    }


    Validator的验证扩展
    验证规则扩展
    你一定会遇到比如要验证是否手机号合法的问题,laravel的Validator没有提供手机号验证的规则,因为每个国家的手机号规则是不统一的。

    但是这个使用想使用Validator做验证怎么办呢?

    Validator::extend('mobile', function($attribute, $value, $parameters)
    {
    return preg_match('/^0?(13[0-9]|15[012356789]|18[0-9]|14[57])[0-9]{8}$/', $value);
    });
    然后在rule中就可以使用mobile做为规则了。

    这种扩展验证规则放到哪里呢?

    我建议是在filters同级目录下增加一个validator.php,然后再start/global.php中

    require app_path().'/validator.php';
    输出信息统一提示
    上面的那个例子中,attribute都需要在使用validator的时候自己定义,比较麻烦,有没有更好的方法能统一设置呢?

    validator的提示设置是按照语言来进行设置的。

    语言设置是config目录下的app.php里面的locale界定的。默认为en。

    而en对应的错误提示都在lang目录下的en目录里面的validation.php中设置。

    你可以看一眼lang/en/validation.php 就明白对应的英文提示信息怎么出来的了。

    要设置中文提示:

    修改config/app.php里面的locale,设置为ch
    创建 lang/ch/validation.php
    修改validation.php文件,将里面的提示信息修改为中文,注意提示信息中的:attribute在显示的时候会被替换成对应属性名称,attributes是设置属性的中文名字的。
    从这里也就看出来了laravel对跨语言的支持是什么样子的。

    lang下面的文件夹除了validation.php之外还有分页:pagination.php,提示:reminders.php文件。

    laravel自带rule规则:

    accepted
    字段值为 yes, on, 或是 1 时,验证才会通过。这在确认"服务条款"是否同意时很有用。

    active_url
    字段值通过 PHP 函数 checkdnsrr 来验证是否为一个有效的网址。

    after:date
    验证字段是否是在指定日期之后。这个日期将会使用 PHP strtotime 函数验证。

    alpha
    字段仅全数为字母字串时通过验证。

    alpha_dash
    字段值仅允许字母、数字、破折号(-)以及底线(_)

    alpha_num
    字段值仅允许字母、数字

    array
    字段值仅允许为数组

    before:date
    验证字段是否是在指定日期之前。这个日期将会使用 PHP strtotime 函数验证。

    between:min,max
    字段值需介于指定的 min 和 max 值之间。字串、数值或是文件都是用同样的方式来进行验证。

    confirmed
    字段值需与对应的字段值 foo_confirmation 相同。例如,如果验证的字段是 password ,那对应的字段 password_confirmation 就必须存在且与 password 字段相符。

    date
    字段值通过 PHP strtotime 函数验证是否为一个合法的日期。

    date_format:format
    字段值通过 PHP date_parse_from_format 函数验证符合 format 制定格式的日期是否为合法日期。

    different:field
    字段值需与指定的字段 field 值不同。

    digits:value
    字段值需为数字且长度需为 value。

    digits_between:min,max
    字段值需为数字,且长度需介于 min 与 max 之间。

    boolean
    字段必须可以转换成布尔值,可接受的值为 true, false, 1, 0, "1", "0"。

    email
    字段值需符合 email 格式。

    exists:table,column
    字段值需与存在于数据库 table 中的 column 字段值其一相同。
    Exists 规则的基本使用方法

    'state' => 'exists:states'
    指定一个自定义的字段名称

    'state' => 'exists:states,abbreviation'
    您可以指定更多条件且那些条件将会被新增至 "where" 查询里:

    'email' => 'exists:staff,email,account_id,1'
    /* 这个验证规则为 email 需存在于 staff 这个数据库表中 email 字段中且 account_id=1 */
    通过NULL搭配"where"的缩写写法去检查数据库的是否为NULL

    'email' => 'exists:staff,email,deleted_at,NULL'
    image
    文件必需为图片(jpeg, png, bmp, gif 或 svg)

    in:foo,bar,...
    字段值需符合事先给予的清单的其中一个值

    integer
    字段值需为一个整数值

    ip
    字段值需符合 IP 位址格式。

    max:value
    字段值需小于等于 value。字串、数字和文件则是判断 size 大小。

    mimes:foo,bar,...
    文件的 MIME 类需在给定清单中的列表中才能通过验证。
    MIME规则基本用法

    'photo' => 'mimes:jpeg,bmp,png'
    min:value
    字段值需大于等于 value。字串、数字和文件则是判断 size 大小。

    not_in:foo,bar,...
    字段值不得为给定清单中其一。

    numeric
    字段值需为数字。

    regex:pattern
    字段值需符合给定的正规表示式。

    注意: 当使用regex模式时,您必须使用数组来取代"|"作为分隔,尤其是当正规表示式中含有"|"字串。

    required
    字段值为必填。

    required_if:field,value
    字段值在 field 字段值为 value 时为必填。

    required_with:foo,bar,...
    字段值 仅在 任一指定字段有值情况下为必填。

    required_with_all:foo,bar,...
    字段值 仅在 所有指定字段皆有值情况下为必填。

    required_without:foo,bar,...
    字段值 仅在 任一指定字段没有值情况下为必填。

    required_without_all:foo,bar,...
    字段值 仅在 所有指定字段皆没有值情况下为必填。

    same:field
    字段值需与指定字段 field 等值。

    size:value
    字段值的尺寸需符合给定 value 值。对于字串来说,value 为需符合的字串长度。对于数字来说,value 为需符合的整数值。对于文件来说,value 为需符合的文件大小(单位 kb)。

    timezone
    字段值通过 PHP timezone_identifiers_list 函数来验证是否为有效的时区。

    unique:table,column,except,idColumn
    字段值在给定的数据库中需为唯一值。如果 column(字段) 选项没有指定,将会使用字段名称。

    Occasionally, you may need to set a custom connection for database queries made by the Validator. As seen above, setting unique:users as a validation rule will use the default database connection to query the database. To override this, do the following:

    $verifier = App::make('validation.presence');

    $verifier->setConnection('connectionName');

    $validator = Validator::make($input, [
    'name' => 'required',
    'password' => 'required|min:8',
    'email' => 'required|email|unique:users',
    ]);

    $validator->setPresenceVerifier($verifier);
    唯一(Unique)规则的基本用法

    'email' => 'unique:users'
    指定一个自定义的字段名称

    'email' => 'unique:users,email_address'
    强制唯一规则忽略指定的 ID

    'email' => 'unique:users,email_address,10'
    增加额外的 Where 条件

    您也可以指定更多的条件式到 "where" 查询语句中:

    'email' => 'unique:users,email_address,NULL,id,account_id,1'
    上述规则为只有 account_id 为 1 的数据列会做唯一规则的验证。

    url
    字段值需符合 URL 的格式。

    注意: 此函数会使用 PHP filter_var 方法验证。

  • 相关阅读:
    cscope的使用
    关于函数指针
    linux内核源码目录(转)
    lcc之内存分配
    符号管理之符号表
    监听UITextFiled文本发生改变
    Debugging Tools for Windows__from WDK7
    WinDBG__独立安装文件
    20160215
    QT Creator 代码自动补全
  • 原文地址:https://www.cnblogs.com/hjcan/p/11377586.html
Copyright © 2011-2022 走看看