Ace Ace - 18 days ago 6
PHP Question

Laravel 5.2 Adding a terms and condition in register

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'
]);

}

Answer

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.

Comments