akatche
akatche

Reputation: 48

Jquery Validator and Laravel 4 ajax issue

i´m trying to validate a form in laravel 4 with Jquery Validator, the only thing i can´t perform it´s a remote validation for the email.

I tried with my browser the following

http://example.com/validacion/email/[email protected] 

and i get the result (in json) that i want.

//Routes.php 
Route::get('validacion/email/{email}', 'ValidatorController@getValidacionEmail');

//In my JS the rule of email is
         email: {
            required: true,
            email: true,
            remote: {
                url: "/validacion/email/",
                type: "get",
                data: {
                    email: "[email protected]"
                },
                complete: function(data) {
                    if (data.responseText !== "true") {
                        alert(data.respuesta);
                    }
                }
         }

And when i use Firebug I get this location

http://example.com/validacion/email/?email=akatcheroff%40gmail.com

and a 301 code and after that a 500 and this error

{"error":{"type":"Symfony\Component\HttpKernel\Exception\NotFoundHttpException","message":"","file":"C:\Users\Usuario\Dropbox\Public\sitios\futbol\vendor\laravel\framework\src\Illuminate\Routing\Router.php","line":1429}}

Does anybody knows if there is a way to send my mail parameter in a way that the route recognize it?

Thanks!

Upvotes: 0

Views: 1508

Answers (1)

Matteus Hemström
Matteus Hemström

Reputation: 3845

Problem

The route you have specified validacion/email/{email} will handle routes such as:

(1) http://mysite.com/validacion/email/[email protected] (Just like you tried in Firefox.)

When your ajax runs you end up (just as firebug dumped) with urls such as:

(2) http://mysite.com/validacion/email/[email protected]

Now notice the difference between url 1 and 2. The first one have the email value as a segment of the url. The second have the email value as part of the query string.

The error Laravel throws is saying that the router couldn't find a matching handler for the url.

Solutions

You can solve this by either changing your javascript:

remote: {
    url: "/validacion/email/" + "[email protected]",
    type: "get"
}

Remove the email from the query string and add it to the path.

Or, you can solve it by changing your PHP route:

Route::get('validacion/email', 'ValidatorController@getValidacionEmail');

Then in getValidacionEmail you can get the email from the query string using:

$email = Input::get('email');

Upvotes: 3

Related Questions