I want to add a terms and condition in the validation form of my register but it is not working. Can some one help me with this.
View
<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'
]);
}
Change the following line:
<input type="checkbox" class="form-control" name="terms" value="{{ old('terms') }}" /> Agree with the terms and conditions
to
<input type="checkbox" class="form-control" name="terms" value="1" /> Agree with the terms and conditions
because we maintain the old value for textbox, textarea etc in case if the entered old value do not pass the validation, but in your case checkbox require a static value i.e. 1 or any value. If it is not selected than show the error, but its value remains the same.
Checkbox validations are harder because they are not sent in request unchecked. Laravel has the accepted validation rule for your case (the value of the checkbox needs to be yes, on, true or 1. Also, to have the old input back on error, just use:
<input type="checkbox" name="terms" value="true" {{ !old('terms') ?: 'checked' }}>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With