JeffreyWay / Laravel-Model-Validation

This Laravel package auto-validates a model on save.
https://packagist.org/packages/way/database
275 stars 54 forks source link

show the error messages in ajax #13

Open sureshamk opened 10 years ago

sureshamk commented 10 years ago

i used your validation library . its nice and helped a lot to me . but now i am getting one issue . i need to show the error messages only as a response . how can i achieve this ?

my code is

$dog = new dog(Input::all());
        if (Request::ajax()) {
            if (!$dog->save()) {
                if ($dog->hasErrors()) {
                    return Response::json(array(
                        'success' => false,
                        'errors' => $dog->getErrors(),
                                      ), 200); 
                }
            }

        }
molovo commented 9 years ago

This isn't a bug. You can include the errors in an Ajax response by calling $dog->getErrors()->toArray(). This is necessary as the messages attribute of the object returned by getErrors() is protected