莫菲    

laravel 验证例子

8年前发布  · 1099 次阅读
  laravel5 
public function createRule(Request $request)
    {
        $this->data = $request->input();
        $validation = Validator::make($this->data,
            array(
                'A' => 'required|string',
                'B' => 'required|integer|min:0|max:254',//数字范围
                'C' => 'required|date_format:"Y-m-d"',
                'Protocol' => 'required|string|in:TCP,UDP,GRE,ESP,AH,IPIP,ALL',//包含于字符串
                'E' => ['required','regex:/^((comp)|(soen)|(engr)|(elec))\d{3}$/i'],//符合正则规则
                'RemoteIpPrefix' => 'required|integer',
                'Enabled' => 'required|integer',
            )
        );
        if ( $validation->fails() ) {
            $errors = $validation->messages();
        }
        if ( ! empty( $errors ) ) {
            foreach ( $errors->all() as $error ) {
                echo '<div class="error">' . $error . '</div>';
            }
        }
    }