Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

    }
like image 401
Ace Avatar asked Mar 10 '23 15:03

Ace


2 Answers

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.

like image 50
Mayank Pandeyz Avatar answered Mar 19 '23 08:03

Mayank Pandeyz


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' }}>
like image 24
Szika Otto Avatar answered Mar 19 '23 09:03

Szika Otto