Support the ongoing development of Laravel.io →
Views Forms Validation
Last updated 1 year ago.
0

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..?']]);
        }
Last updated 8 years ago.
0

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

0

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'));
        }
0

<?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

Last updated 8 years ago.
0

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.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Harrrrry harrrrry Joined 2 Nov 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.