It's your ajax request that needs to validate a response and show an alert.
$.ajax({
type:"POST",
url:"/application/workpermit",
data:dataString,
success:function(data){
$('#tabs-12 .workpermit').html(data);
var json = JSON.parse(data);
if (typeof json.errors != 'undefined')
console.error(json.errors);
else
alert('success');
}
});
Your validator could then look like
if ($validator->fails()) {
return json_encode(['errors' => ['whatever error', 'maybe another..?']]);
}
Form not submitting through ajax, php form submit is working so after validation check it redirect, and when it redirect ajax again call and give the fresh form
Sorry. I don't understand how this is related to ajax then?
If you want to show errors, you can do something like this in your base controller (Controller.php)
if ($request->hasSession()) {
if ($request->session()->has('errors'))
dd($request->session()->get('errors')->all());
if ($request->session()->has('success'))
dd($request->session()->get('success'));
}
<?php namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesCommands;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
abstract class Controller extends BaseController {
use DispatchesCommands, ValidatesRequests;
if ($request->hasSession()) {
if ($request->session()->has('errors'))
dd($request->session()->get('errors')->all());
if ($request->session()->has('success'))
dd($request->session()->get('success'));
}
}
Giving me error FatalErrorException in Controller.php line 10: syntax error, unexpected 'if' (T_IF), expecting function (T_FUNCTION)
Can you explain more please
Read the error message. You have an if statement in the class, and not in a method. Read up on PHP syntax to fix that error.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community