Laravel自定义验证

时间:2012-12-09 12:09:58

标签: laravel laravel-3

---自定义验证类(application / libraries / validator.php)---

 class Validator extends Laravel\Validator {
    public function validate_passwdU($attribute, $value, $parameters){
        $r_uppercase    = '/[A-Z]/';  //Uppercase
        $default_min = 1;
        if(is_numeric($parameters[0])>=$default_min){
        $default_min = $parameters[0];
        }
        return (preg_match_all($r_uppercase,$value, $matches)>=$default_min);       
    }
}

--- application / language / en / validation.php ---

    "passwdU" => "The :attribute must be at least :size uppercase characters."

---控制器---

    $rules = array(
        'passwd' => 'required|min:8|passwdU:2'
    );

    $validation = Validator::make($input, $rules);

    if ($validation->fails())
    {
        return Redirect::to('URL')->with_errors($validation);
    }

---输入---

passwd = 11111111

---查看---

<pre>
<?php print_r($errors); ?>
</pre>

---输出---

Laravel\Messages Object
(
    [messages] => Array
    (
        [passwd] => Array
            (
                [0] => validation.passwdU
            )

    )
    [format] => :message
)

为什么我没有收到我在语言文件中定义的消息?

1 个答案:

答案 0 :(得分:0)

将消息方法传递给视图

return Redirect::to('login')
    ->withErrors($validator->messages())

并使用get方法

打印它
{{ $errors->get('email') }}

为我工作:

The email field is required.