Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

laravel display validation errors ajax

// php
public function store(Request $request)
{
  	$validator = Validator::make( $request->all(),[
            'first_name' => 'string',
            'last_name' => 'string',
            'phone_number' => ['string', 'max:14'],
            'email' => 'email'
      ]);
        
      if($validator->fails())
        {
            $response = "<div class='alert alert-danger'><ul>";
            foreach($validator->errors()->all() as $error)
            {
              $response .= "<li> {$error}</li>";  
            }
            $response .= "</ul></div>";
            return response()->json([
                'message' => $response
            ], 201);
        }
  
  // no errors
  //continue with your logic
}

// javascript (jQuery)
$.ajax({
  url: 'your_backend_url',
  type: 'POST/PUT',
  data: request_data, // maybe $(this).serializeArray()
  beforeSend: () => {
    // any stuff that you would like to do before sending the request,maybe showing a loader
  },
  success: (response, responseText, xhr) => {
     if(xhr.status === 201)
      {
        // put errors in some element above or below the form
        return $(".errors").html(response.message)
      }
    // clear previous errors
    $(".errors").html("");
    // success stuff here
    }
});
Comment

laravel return validation errors ajax

public function testAjax(Request $request)
  {
    $name = $request->input('name');
    $validator = Validator::make($request->all(), ['name' => 'required']);

    if ($validator->fails()){
        $errors = $validator->errors();
        echo $errors;
    }
    else{
      echo "welcome ". $name;
    }

  }
Comment

laravel return validation errors

@if($errors->any())
   @foreach ($errors->all() as $error)
      <div>{{ $error }}</div>
  @endforeach
@endif
Comment

laravel display validation errors ajax


        $validator = Validator::make($request->all(), [
            'barCodeNo' => 'required',
            'propertyNo'  => 'required',
        ]);

        if ($validator->fails()) {

            $response['code']   = 400;
            $response['errors'] = $validator->errors()->toArray();

            return response()->json(['response' => $response],400);
        }
Comment

PREVIOUS NEXT
Code Example
Javascript :: js array return only certain positions 
Javascript :: jquery cdn in react 
Javascript :: express uncaughtException 
Javascript :: jquery checkvalidity 
Javascript :: loop through all dom elements javascript 
Javascript :: bigger or equal javascript 
Javascript :: styling element using jquery 
Javascript :: javascript clear input string 
Javascript :: access selected option in jquery 
Javascript :: convert number to word crore/lakhs 
Javascript :: generate html with javascript 
Javascript :: chartjs lineTension 
Javascript :: check template shopify 
Javascript :: iterate over map in javascript 
Javascript :: ajax code 
Javascript :: how to make pages from list in nodejs 
Javascript :: react fetch custom hook 
Javascript :: sort object array javascript 
Javascript :: jquery telephone input mask 
Javascript :: lodash empty string 
Javascript :: add multiple event listeners 
Javascript :: Date gethours js 
Javascript :: reset function javascript 
Javascript :: syntax for srcset in react 
Javascript :: index.js:3 Uncaught ReferenceError: $ is not defined at index.js:3 
Javascript :: typeorm findone subquery 
Javascript :: add active class and remove active class by click 
Javascript :: loop through javascript object 
Javascript :: javascript intl.numberformat reais 
Javascript :: javascript array move element 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =