【发布时间】:2021-11-23 11:25:20
【问题描述】:
我正在使用Laravel Framework 8.62.0,并且我有以下验证规则:
$rules=[
'username' => 'required|min:3|max:30|alpha_dash',
'email' => "required|email|unique:users,email",
'password' => 'required|min:6|confirmed',
];
$error_messages=[
'username.required'=>'The username-field is required.',
'username.min'=>'Your username must be longer than 3 characters.',
'username.max'=>'Please shorten your username.',
'username.alpha_dash'=>'Please use letters from A-Z and a-z, dashes (-) or underscores(_).',
'email.unique'=>'Your email is already used.',
'email.email'=>'Please add a correct email-address.',
'email.required'=>'The email-field is required.',
'password.required'=>'The password-field is required.',
'password.min'=>'Your password must have at least 6 characters.',
'password.confirmed'=>'Your entered passwords do not match.',
];
$validator = validator($request->all(), $rules, $error_messages);
我的表单如下所示:
<form id="Register" class="card-body" tabindex="500" action="{{ url('register') }}" method="POST">
<!-- CSRF Token -->
<input type="hidden" name="_token" value="{{ csrf_token() }}" />
<div class="name">
<input type="text" name="username" value="{!! old('username') !!}">
<label>Username</label>
<li>
{!! $errors->first('username.required', '<ul><span style="color: red;" class="help-block">:message</span></ul>') !!}
{!! $errors->first('username.min', '<ul><span style="color: red;" class="help-block">:message</span></ul>') !!}
{!! $errors->first('username.max', '<ul><span style="color: red;" class="help-block">:message</span></ul>') !!}
{!! $errors->first('username.alpha_dash', '<ul><span style="color: red;" class="help-block">:message</span></ul>') !!}
</li>
</div>
<div class="mail">
<input type="email" name="email" value="{{ old('email') }}">
<label>Mail</label>
{!! $errors->first('email.unique', '<span style="color: red;" class="help-block">:message</span>') !!}
{!! $errors->first('email.required', '<span style="color: red;" class="help-block">:message</span>') !!}
{!! $errors->first('email.email', '<span style="color: red;" class="help-block">:message</span>') !!}
</div>
<div class="passwd">
<input type="password" name="password" value="{{ old('password') }}">
<label>Password</label>
{!! $errors->first('password.min', '<span style="color: red;" class="help-block">:message</span>') !!}
{!! $errors->first('password.confirmed', '<span style="color: red;" class="help-block">:message</span>') !!}
{!! $errors->first('password.required', '<span style="color: red;" class="help-block">:message</span>') !!}
</div>
<div class="passwd">
<input type="password" name="password_confirmation" value="{{ old('password_confirmation') }}">
<label>Confirm Password</label>
{!! $errors->first('password.confirmed', '<span style="color: red;" class="help-block">:message</span>') !!}
</div>
<div class="submit">
<!-- <a class="btn ripple btn-primary btn-block" href="#">Register</a> -->
<input type="submit" class="btn ripple btn-primary btn-block" value="Register">
</div>
<p class="text-dark mb-0">Already have an account?<a href="{{ url('login') }}" class="text-primary ml-1">Sign In</a></p>
</form>
当我输入一个包含 3 个字符且没有 - 的用户名并且我的密码长度不匹配时,我会返回所有验证错误:
但是,我只想找回真正适用的错误。
任何建议我做错了什么?
感谢您的回复!
【问题讨论】:
标签: laravel validation