Retrieve error message via json no Angular

8

I'm trying to register using Angularjs, but I want to get the validations that are in my model in rails. But I'm having trouble:

Covenat (model) Rails

validates :name, presence: true 

Covenat Action Create controller

def create
  @covenat = Covenat.new(covenat_params)
  if @covenat.save
    render json: @covenat, status: 200
  else
    render json: { errors: @covenat.errors }, status: 422
  end
end

view /assets/templates/covenats/new.html

<form name="form" ng-submit="submit()" novalidate>
   <div class="form-group" ng-class="errorClass('name')">
     <label class="control-label" for="covenat_name">Nome</label>
     <input class='form-control' type='text' ng-model='covenat.name'     name='covenat_name' id="covenat_name" required/>
     <p class="help-block" ng-show="form.name.$invalid && form.name.$dirty">
        {{ errorMessage('name')}}
     </p>
   </div>          
</form>

covenats_new_controller.js

angular.module('clinica')
  .controller('CovenatsNewCtrl', ['$scope', 'Covenat', '$location', 
function($scope, Covenat, $location){

$scope.submit = function() {
  console.log("submit")

  function success(response) {
    console.log("success", response)
    $location.path("/covenats");
  }

  function failure(response) {
    console.log("failure", response)

    _.each(response.data, function(errors, key) {
    _.each(errors, function(e) {
      $scope.form[key].$dirty = true;
      $scope.form[key].$setValidity(e, false);
    });
   });
 }

  Covenat.save($scope.covenat, success, failure);

};

$scope.errorClass = function(name) {
 var s = $scope.form[name];
 return s.$invalid && s.$dirty ? "error" : "";
};

$scope.errorMessage = function(name) {
 var s = $scope.form[name].$error;
 result = [];
 _.each(s, function(key, value) {
   result.push(value);
 });
 return result.join(", ");
};

}]);

I have the following error in function failure:

  

TypeError: Can not read property '$ invalid' of undefined

in line return s.$invalid && s.$dirty ? "error" : "";

    
asked by anonymous 14.04.2015 / 18:20

2 answers

0

When you call the functions

errorClass('name') , errorMessage('name')

You are passing as parameter the string "name" ie when it arrives at the function.

var s = $scope.form[name]; this is actually turning $scope.form.name and the name property does not exist inside your form, so the s variable is undefined, when you try to access the $ invalid variable it shows the error.

    
11.08.2015 / 23:51
0

The problem is that you are passing the 'name' parameter as if it were the name of the form element you want to access in the errorClass function.

Actually, you should pass the name of the attribute you want to validate the error message in the 'covenat_name' case.

Here's an example:

<form name="form" ng-submit="submit()" novalidate>
   <div class="form-group" ng-class="errorClass('covenat_name')">
     <label class="control-label" for="covenat_name">Nome</label>
     <input class='form-control' type='text' ng-model='covenat.name'     name='covenat_name' id="covenat_name" required/>
     <p class="help-block" ng-show="form.name.$invalid && form.name.$dirty">
        {{ errorMessage('name')}}
     </p>
   </div>          
</form>
    
23.02.2016 / 15:49