当前位置 : 主页 > 网络编程 > PHP >

laravel Validator实例

来源:互联网 收集:自由互联 发布时间:2021-06-30
1. [代码] [PHP]代码 public function getIndex() { $rules = array( 'email' = 'required|email', 'name' = 'required|between:1,20', 'password' = 'required|min:8', ); $message = array( "required" = ":attribute 不能为空", "between" = ":att

1. [代码][PHP]代码    

public function getIndex()
    {
        $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' => '用户密码',
        );

        $validator = Validator::make(
            Input::all(), 
            $rules,
            $message,
            $attributes
        );
        if ($validator->fails()) {
            $warnings = $validator->messages();
            $show_warning = $warnings->first();
            return Response::Json(compact('show_warning'));
        }
        return Response::Json("ok");
    }




@if($errors->any())
        <ul class="alert alert-danger">
            @foreach($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
@endif
网友评论