title: Laravel and angularjs form validation
body: <p>I am new to angularjs and im playing around with it.</p>
  
  <p>I'm stuck with one thing, in jQuery it's more easier to retrive the validation error messages json object from laravel, with angular i am able, but i am doing it this way and im sure there is a more effective way</p>
  
  <p>My from</p>
  
  <pre><code>&lt;div class="row"&gt;
      &lt;div class="col-lg-8"&gt;
          &lt;h5&gt;&lt;?php echo  Lang::get('auth.signup') ?&gt;&lt;/h5&gt;
          &lt;div class="page-divider"&gt;&lt;/div&gt;
  
          &lt;form name="myForm" ng-controller="formController" ng-submit="signupPost()" class="form-horizontal" novalidate&gt;
  
              &lt;div class="form-group"&gt;
                  &lt;label for="first_name" class="col-lg-3 control-label"&gt;&lt;?php echo Lang::get('form.first_name') ?&gt;&lt;/label&gt;
                  &lt;div class="col-lg-8"&gt;
                    &lt;input type="text" name="first_name" ng-model="formData.first_name" id="first_name" class="form-control input-small"&gt;
                    &lt;span class="help-block" ng-show="errors['first_name'][0]"&gt;{{ errors['first_name'][0] }}&lt;/span&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;div class="form-group"&gt;
                  &lt;label for="last_name" class="col-lg-3 control-label"&gt;&lt;?php echo Lang::get('form.last_name') ?&gt;&lt;/label&gt;
                  &lt;div class="col-lg-8"&gt;
                    &lt;input type="text" name="last_name" ng-model="formData.last_name" id="last_name" class="form-control input-small"&gt;
                    &lt;span class="help-block" ng-show="errors['last_name'][0]"&gt;{{ errors['last_name'][0] }}&lt;/span&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;div class="form-group"&gt;
                  &lt;label for="username" class="col-lg-3 control-label"&gt;&lt;?php echo Lang::get('form.username') ?&gt;&lt;/label&gt;
                  &lt;div class="col-lg-8"&gt;
                    &lt;input type="text" name="username" ng-model="formData.username" id="username" class="form-control input-small"&gt;
                    &lt;span class="help-block" ng-show="errors['username'][0]"&gt;{{ errors['username'][0] }}&lt;/span&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;input type="submit" value="&lt;?php echo Lang::get('auth.signup') ?&gt;" class="btn btn-primary"&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  
  &lt;/div&gt; 
  </code></pre>
  
  <p>Angular controller</p>
  
  <pre><code>function formController($scope, $http) 
  {
      $scope.formData = {};
  
      $scope.signupPost = function() {
  
          $http.post('signup', $scope.formData).success(function(data){
  
              if(data.msg == "success")
              {
                  $location.path(data.redirect)
              }
              else
              {
                  $scope.errors = data.error_msg;
              }
          });
      }
  }
  </code></pre>
  
  <p>And the json what laravel retunrs if the form validation fails</p>
  
  <pre><code> $messages = $val-&gt;messages();
  
              $data = array(
                  'error_msg' =&gt; array(
                      'first_name'           =&gt; $messages-&gt;get('first_name'),
                      'last_name'            =&gt; $messages-&gt;get('last_name'),
                      'username'             =&gt; $messages-&gt;get('username'),
                      'profession'           =&gt; $messages-&gt;get('profession'),
                      'location'             =&gt; $messages-&gt;get('location'),
                      'email'                =&gt; $messages-&gt;get('email'),
                      'gender'               =&gt; $messages-&gt;get('gender'),
                      'password'             =&gt; $messages-&gt;get('password'),
                      'dob'                  =&gt; $messages-&gt;get('dob'),
                      'confirm_password'     =&gt; $messages-&gt;get('confirm_password'),
                  ));
          }
  
          return Response::json($data);
  </code></pre>
  
  <p>I tried a few variations and currently it works like this in the form, show the form validation error messages if its set, this way <code>errors['first_name'][0]</code> for all fields.</p>
  
  <p>My question is, is there a more effective way doing this? If someone could show me an example would be great</p>
  
  <p>Thank you</p>