1

I am trying to adapt the default registration form in Laravel to my database custom users table; I have a checkbox that isn't returning values, and even though it's selected it doesn't return a value. The validator alerts me that the checkbox field is required after submitting even though I selected it.

This is the checkbox:

<!--checkbox-->
<div class="form-group row">
    <label for="usertype" class="col-md-4 col-form-label text-md-right">Type Utilisateur</label>
    <div class="col-md-6">
        <input type="checkbox" name="check[]" value="normal"/> Normal
        <input type="checkbox" name="check[]" value="admin"/> Admin
        <input type="checkbox" name="check[]" value="super"/> Super
        @if ($errors->has('usertype'))
            <span class="help-block">
                <strong>{{ $errors->first('usertype') }}</strong>
            </span>
        @endif
    </div>
</div> 

Edit: Validator

protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:6', 'confirmed'],
            'telephone' => ['required', 'numeric'],
            'usertype' => ['required', 'string'],
        ]);
    }

Note: I removed usertype from Validation but the registration wouldn't go on the register page would refresh and no error appears or any alerts

2
  • Can you please post your validation code here? Commented Feb 16, 2019 at 23:11
  • @1000Nettles Added in the question Commented Feb 16, 2019 at 23:37

2 Answers 2

2

Within your input elements, instead of name="check[]" change it to name="usertype[]".

Sign up to request clarification or add additional context in comments.

5 Comments

Okay i did that and it says "The usertype must be a string."
I removed the 'string' condition from the validator for usertype and it's back to just refreshing the register page whenever i validate.
show full code of that function. your problem isnt only the validation part. Also why do you use string as part of validation for your checkbox
What i understand with string rule you are trying to make sure that the options that would be checked is only between normal admin and user?
I'm glad I helped :)
0

You are currently submitting the value of the checkbox as check to Laravel, not name:

name="check[]"

You can adjust the validator to the following:

protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:6', 'confirmed'],
            'telephone' => ['required', 'numeric'],
            'check' => ['accepted'],
        ]);
    }

For more information, check this out: https://laravel.com/docs/5.2/validation#rule-accepted. You should be using accepted for validating checkboxes if you want it them to be required.

After the validation is complete in your controller action, ensure that you're not just returning the validator. The validator will throw an exception if there's an issue, so you can just place any code you want run after the validation call.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.