Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 to use existing validation to AngularJS

Hi I have a problem in laravel 5 existing validation.

In my controller and request.

class PostEmailRequest extends Request{
/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'email' => 'required|max:255',
    ];
}}

It will return the validation of an email as an example. What I want to do with my angularjs is I want to catch validation and fetch it on my front-end form. It will not reload the page. Do you have any idea about this one?

Form from my application will send data through angularjs. $http post url "app/postPage" and if it has an error above my code will $validation->messages() will send to my angularjs.

like image 653
Kuya A Avatar asked Jul 29 '15 12:07

Kuya A


1 Answers

You can do following.

  • Make a POST route for validation lets say myapp.com/validateEmail
  • Send the data which you want to validate to this route using $http request of anuglarJS. Lets say Email
  • Validate the data and return validation object as JSON. You can send JSON response like that. JSON Responses from docs
  • Read this JSON in scope model in angularjs controller. In the success() of following code.

    $http.post('/someUrl', {msg:'hello word!'}).
      success(function(data, status, headers, config) {
    // this callback will be called asynchronously
    // when the response is available
     }).
    error(function(data, status, headers, config) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
    });
    
  • Show appropriate error based on scope in HTML.

like image 102
hhsadiq Avatar answered Oct 14 '22 07:10

hhsadiq