0

All in-line validations in HTML side are working OK, but when I use functions in my Controller (functions ingresarNuevoEmpleador and limpiarNuevoEmpleador), always get $invalid=true. $setPristine neither works. Apparently both sides are referencing different objects because controller functions do not modify the state of the html side validators.

<form name="frmEmpleadorDirecto" novalidate>
    <div class="row">
      <div class="col-xs-8 col-sm-8">
       <div class="form-group has-feedback" 
        ng-class="{'has-error': frmEmpleadorDirecto.nempleador.$invalid && (frmEmpleadorDirecto.nempleador.$touched || frmEmpleadorDirecto.$submitted)}">
          <label for="input501">Empleador Directo</label>
          <input type="text" class="form-control text-uppercase" name="nempleador" placeholder="Nombre Empleador" ng-model="objEmpleador_.nombreEmpleador_" ng-minlength="2" ng-maxlength="30" ng-required="true">
          <span class="help-block" ng-if="frmEmpleadorDirecto.nempleador.$error.minlength && frmEmpleadorDirecto.nempleador.$touched">too short</span>
          <span class="help-block" ng-if="frmEmpleadorDirecto.nempleador.$error.maxlength && frmEmpleadorDirecto.nempleador.$touched">too long</span>
          <span class="help-block" ng-if="frmEmpleadorDirecto.nempleador.$error.required && frmEmpleadorDirecto.nempleador.$touched">required</span>
          </div>
        </div>
      </div>
      <div class="text-right">
      <button type="submit" class="btn btn-success btn-mm" ng-disabled="frmEmpleadorDirecto.$pristine || frmEmpleadorDirecto.$invalid" ng-click="ingresarNuevoEmpleador()">Aceptar</button>
      <button type="button" class="btn btn-warning btn-mm" ng-click="limpiarNuevoEmpleador()">Limpiar</button>
    </div>
    </form>

Controller:

angular.module('BlurAdmin.pages.empleador_')
      .controller('CtrlEmpleadorNuevo',CtrlEmpleadorNuevo);

 function CtrlEmpleadorNuevo($scope,$http,$rootScope) {

  $scope.objEmpleador_ = new eva.Empleador_();


  $scope.ingresarNuevoEmpleador = function()
     {
    console.log("Nombre: " + $scope.objEmpleador_.nombreEmpleador_); //OK
        if($scope.objEmpleador_.$valid)  //always invalid
        {

        console.log('valid!');
        }else {
          console.log('invalid!');        }

    }

    $scope.limpiarNuevoEmpleador = function () 
    {

        $scope.objEmpleador_.$setPristine; //no changes
       $scope.objEmpleador_.nombreEmpleador_ = ''; //Works: reset html field

    }
}

Objects:

eva.Empleador_ = function() {
    var self = this;
    self.nombreEmpleador_ = '';
};

1 Answer 1

0

Your form is bound on your $scope and inputs and their validity can be reached following the name attribute.

  $scope.ingresarNuevoEmpleador = function(){
    console.log("Nombre: " + $scope.objEmpleador_.nombreEmpleador_); //OK
        if($scope.frmEmpleadorDirecto.nempleador.$valid) {
        console.log('valid!');
        }else {
          console.log('invalid!');        
        }

    };

Plunker

Sign up to request clarification or add additional context in comments.

2 Comments

angular.js:12798 TypeError: Cannot read property 'nempleador' of undefined at ChildScope.CtrlEmpleadorNuevo.$scope.ingresarNuevoEmpleador(CtrlEmpleadorNuevo.js:17)
Can you please check the plunker. I get no errors and the console displays when the button is pressed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.