Home > Software design >  Laravel country select from droplist not saving
Laravel country select from droplist not saving

Time:09-27

I'm facing a problem while validating the country in laravel

1.View

        <div class="form-group">
                 <label >{{__("Country")}} <span class="required">*</span> </label>
                 <select name="country" class="form-control">
                 <option value="">{{__('-- Select --')}}</option>
                  @foreach(get_country_lists() as $id=>$name)
                  <option @if((old('country',$dataUser->country ?? '')) == $id)selected @endif value="{{$id}}">{{$name}}</option>
                    @endforeach
                    </select>
                    <span class="invalid-feedback error error-country"></span>
                </div>

2.in Controller

       'country'  =>
       'required|max:200',

      'country.required'   => __('Select your country from the list '),
     
        $user = new \App\User();
        $user = $user->fill([
                'first_name'=>$request->input('first_name'),
                'last_name'=>$request->input('last_name'),
                'email'=>$request->input('email'),
                 'country'=>$request->input('country'),
                  ]);

3.in register.js

    var url = form.attr('action');
    $.ajax({
        'url': url,
        'data': {
            'email': form.find('input[name=email]').val(),
            'first_name': form.find('input[name=first_name]').val(),
            'last_name': form.find('input[name=last_name]').val(),
            'country':form.find('input[name=country]').val(),
          },
        'type': 'POST',

by selecting the country, getting the error for validation, and the country is not saved

Thanks

CodePudding user response:

Your saving the country as an id, your error that you validate the country with max:200 if the country id is greater than 200 it will throw a validation exception. I think you must remove the max:200 rule.

CodePudding user response:

Your country form field is a select element but you are trying to catch it's value with input[name=country].

You need to change it as 'country':form.find('select[name=country]').val(), in your register.js file.

  • Related