lavarel 验证表单,验证器带需要参数的话怎么写? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
yakczh
V2EX    PHP

lavarel 验证表单,验证器带需要参数的话怎么写?

  •  
  •   yakczh Mar 18, 2014 3315 views
    This topic created in 4426 days ago, the information mentioned may be changed or developed.
    $v = Validator::make($data, array(
    'email' => 'required|email',
    'salary' => 'required|numeric',
    ));

    看了一个文档,一个字段多个验证规则是用|分开的,如果是数值范围 比如 3000-5000这种自定义的参数,怎么设置验证表单的rules
    7 replies    1970-01-01 08:00:00 +08:00
    dongbeta
        1
    dongbeta  
       Mar 18, 2014
    可以写两个:

    'salary' => 'required|numeric|min:3000|max:5000',
    yakczh
        2
    yakczh  
    OP
       Mar 18, 2014
    @dongbeta 再问个问题, artisan 能把代码里的print_r信息显示在控制台上吗?
    Route::post('register', array('before' => 'csrf', function()
    {
    $rules = array(
    'username' => array('required', 'unique:users,username'),
    'email' => array('required', 'email', 'unique:users,email'),
    'password' => array('required', 'min:7')
    );
    $data=Input::all();

    print_r($data);
    $validation = Validator::make( $data, $rules);

    if ($validation->fails())
    {
    $messages = $validator->messages();

    print_r($messages);

    return Redirect::to('/add')->withErrors($validator)->withInput();

    }else {

    User::create($data);

    }

    模板文件是
    <h2>Register!</h2>

    @if( $errors->count() > 0 ) <p>The following errors have occurred:</p>
    <ul id="form-errors">
    {{ $errors->first('email', '<li>:message</li&g;') }}
    {{ $errors->first('username', '<li>:message</li>') }}
    {{ $errors->first('password', '<li>:message</li>') }}
    </ul> @endif

    <hr/>

    {{ Form::open(['register', 'post']) }}

    <br/>{{ Form::label('username', 'Username') . Form::text('username', Input::old('username')) }}
    <br>{{ Form::label('email', 'E-mail') . Form::text('email', Input::old('email')) }}
    <br>{{ Form::label('password', 'Password') . Form::password('password') }}

    <br>{{ Form::submit('Register!') }}

    {{ Form::token() . Form::close() }}


    递交表单老是重定向到 表单页面,显示
    The following errors have occurred:

    但是下面什么也没有,怎么在服务端 查看post提交的数据和出错信息?
    dongbeta
        3
    dongbeta  
       Mar 18, 2014
    没看明白。以上这段程序的 php 不应该在一个文件中吧。
    yakczh
        4
    yakczh  
    OP
       Mar 18, 2014
    @dongbeta 一个是后台接受递交参数的 一个是前台模板显示表单的
    Yuansir
        5
    Yuansir  
       Mar 18, 2014
    'salary' => 'required|numeric|between:3000,5000,
    yakczh
        6
    yakczh  
    OP
       Mar 18, 2014
    @Yuansir 如果是验证敏感词和非法注入之类的处理呢?
    Yuansir
        7
    Yuansir  
       Mar 18, 2014
    @yakczh github上有类似 xssinput 的包,你也自己写过滤逻辑对Input处理
    About     Help     Advertise     Blog     API     FAQ     Solana     807 Online   Highest 6679       Select Languae
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 30ms UTC 20:59 PVG 04:59 LAX 13:59 JFK 16:59
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86