Search
 
SCRIPT & CODE EXAMPLE
 

PHP

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 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
Php :: get first 200 characters string php 
Php :: how to hide get parameters in url php 
Php :: laravel with trashed specific 
Php :: add a new column to existing table in a migration 
Php :: Latest 5 records - Laravel 
Php :: Remove product (item) from WooCommerce checkout page using AJAX 
Php :: strtoupper php 
Php :: Call to undefined function mysql_connect() 
Php :: pathtophp in ubuntu lampp in moodle 
Php :: php run python script 
Php :: maximum execution time of 60 seconds exceeded laravel 8 
Php :: forelse laravel 
Php :: laravel child relation order by desc 
Php :: how to increase request timeout in laravel 
Php :: count files in folder php 
Php :: isset blade laravel 
Php :: larvel check two password 
Php :: php regex string start 
Php :: php load specific post id on language 
Php :: remove % sign from string php 
Php :: php trim array to certain length 
Php :: php datetime to timestamp 
Php :: wordpress get page slug 
Php :: convert stdclass object to array php 
Php :: php server 
Php :: foreach loop in php 
Php :: read csv php 
Php :: php timestamp 
Php :: laravel websockets onclose 
Php :: php index of last element in array 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =