gpt4 book ai didi

Laravel 自定义验证

转载 作者:行者123 更新时间:2023-12-02 09:14:22 25 4
gpt4 key购买 nike

--- 自定义验证类 (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."

--- Controller ---

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

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

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

--- 输入 ---

密码=11111111

--- 查看 ---

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

--- 输出 ---

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

)
[format] => :message
)

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

最佳答案

向 View 传递消息方法

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

并使用 get 方法打印

{{ $errors->get('email') }}

为我工作:

The email field is required.

关于Laravel 自定义验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13787266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com