Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel ajax form validation with input field

Tags:

laravel

In Laravel 5.1 Project

I'm getting this Ajax Response As Error

{
      "success":false,
      "errors":{
      "drugs_power":["The drugs power field is required."]
      }
}

i need a error validation like this image enter image description here i can make it by laravel validator by this code(return From Controller),

$this->validate($request, [
            'drugs_name' => 'required|unique:drugs',
            'drugs_group_name' => 'required',
            'drugs_power' => 'required'
        ]);

Show In blade.php by this code

<div class="form-group{{ $errors->has('drugs_power') ? ' has-error' : '' }}">
   <label for="userName">Power</label>
       <div>
          <input type="text" class="form-control" name="drugs_power"
                                               value="{{old('drugs_power')}}">
        @if ($errors->has('drugs_power'))
          <span class="help-block">
         <strong>{{ $errors->first('drugs_power') }}</strong>
              </span>
           @endif
     </div>


</div>

How can i make ajax+laravel validation just like that??

like image 224
K M Rifat ul alom Avatar asked Nov 08 '22 17:11

K M Rifat ul alom


1 Answers

Passing in a 3rd parameter to validate allows you to specify a custom message, as seen here. You can use "dot" notation to specify the error message to a specific field.

$this->validate($request, [
    'drugs_name' => 'required|unique:drugs',
    'drugs_group_name' => 'required',
    'drugs_power' => 'required'
], [
    'drugs_power.required' => 'Your custom message here.'
]);
like image 182
camelCase Avatar answered Nov 15 '22 11:11

camelCase