Create user in Laravel 5.3 passing data with select

3

I am creating a registration form in Laravel 5.3, where one of the fields is a "group" that searches in a database. The groups are available and displayed by a select . The problem is that I could not save, because I think I'm not receiving the value of select .

This is my select code:

<div class="form-group{{ $errors->has('id_group') ? ' has-error' : '' }}">
    <label for="id_group" class="col-md-4 control-label">Grupos</label>
    <div class="col-md-6">
        <select class="form-control" name="id_group" >
          @foreach($groups as $group)
            <option value="{{$group->id}}">{{$group->name}}</option>
          @endforeach
        </select>
          @if ($errors->has('id_group'))
              <span class="help-block">
                  <strong>{{ $errors->first('id_group') }}</strong>
              </span>
          @endif
    </div>
</div>

This is the function where I validated the form data:

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:testauths',
        'password' => 'required|min:6|confirmed',
        'group_id' => 'required|max:255|confirmed',
    ]);
}

This is my function create where I save the data:

protected function create(array $data)
{
    return Testauth::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'group_id' => $data['group_id'],

    ]);
}

When sending the form, despite having selected the value of the select, the validation method tells me that nothing has been selected.

Making a dd ($ data) of the validate function shows that the "Group" field is sent empty.

    
asked by DVertel 31.10.2016 в 15:34
source

2 answers

1

After exchanging information in the comments, three errors were found and corrected:

  • The attribute name of the field sent did not match the name of the field in the validation (one was group_id and the other was id_group.

  • There was an unnecessary confirmed validation in that field.

  • The query was not fetching the Id, so the attribute value in each <option> was empty and this was reflected in the data of the Request.

  • answered by 31.10.2016 / 20:35
    source
    2

    The option is sending id_group and what your controller expects is group_id

    <div class="form-group{{ $errors->has('group_id') ? ' has-error' : '' }}">
    <label for="id_group" class="col-md-4 control-label">Grupos</label>
    <div class="col-md-6">
        <select class="form-control" name="id_group" >
          @foreach($groups as $group)
            <option value="{{$group->id}}">{{$group->name}}</option>
          @endforeach
        </select>
          @if ($errors->has('id_group'))
              <span class="help-block">
                  <strong>{{ $errors->first('group_id') }}</strong>
              </span>
          @endif
    </div>
    

    Leveraging: I'm fine with using HTML for the select and not blade but how do you update with the value corresponding to the record?

        
    answered by 28.01.2017 в 17:22