我想在注册表的验证表单中添加条款和条件,但它不起作用。有谁能帮我一下吗?
视图<div class="form-group{{ $errors->has('terms') ? ' has-error' : '' }}">
<label>
<input type="checkbox" class="form-control" name="terms" value="{{ old('terms') }}" /> Agree with the terms and conditions
</label>
<div class="col-md-4">
@if ($errors->has('terms'))
<span class="help-block">
<strong>{{ $errors->first('terms') }}</strong>
</span>
@endif
</div>
</div>
AuthController
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'company' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'telephone' => 'required|max:255',
'password' => 'required|min:6|confirmed',
'g-recaptcha-response' => 'required|captcha',
'terms' => 'required'
]);
}
更改以下行:
<input type="checkbox" class="form-control" name="terms" value="{{ old('terms') }}" /> Agree with the terms and conditions
<input type="checkbox" class="form-control" name="terms" value="1" /> Agree with the terms and conditions
,因为我们为textbox, textarea等保持旧值,以防输入的旧值不通过验证,但在你的情况下,复选框需要一个静态值,即1或任何值。如果未选中,则显示错误,但其值保持不变
复选框验证更困难,因为它们不是在未检查的情况下发送的。Laravel有可接受的验证规则(复选框的值需要是yes, on, true或1)。此外,要使旧的输入返回错误,只需使用:
<input type="checkbox" name="terms" value="true" {{ !old('terms') ?: 'checked' }}>