6

In my form validation there is part of server validations. So I should get back from the server list with the names of the fields and a string with the error in each of them. My idea was to write a general code knowledge to deal with all fields without knowing them in advance, by accessing them with their name. this is field for example:

<!-- Email -->
<div class="form-group" data-ng-class="{ 'has-error' : step1Form.email.$invalid && (!step1Form.email.$pristine || submitted) }">
     <label>Email</label>
     <input type="email" name="email" class="form-control" data-ng-model="user.email" required data-ng-minlength="5" data-ng-maxlength="60">
     <p data-ng-show="step1Form.email.$error.required && (!step1Form.email.$pristine || submitted)" class="help-block">required!</p>
     <p data-ng-show="step1Form.email.$error.minlength" class="help-block">too short1</p>
     <p data-ng-show="step1Form.email.$error.maxlength" class="help-block">too long!</p>
     <p data-ng-show="step1Form.email.$error.email" class="help-block">invalid email!</p>
     <p data-ng-show="step1Form.email.$error.serverError" class="help-block">{{emailServerError}}</p>
</div>

like you can see, the variable emailServerError is saved for errors that come from the server validations... i have a lot fields in my application and i try to write generic code that will fit for all the fields...

so this is the angular code:

// function to submit the form after all validation has occurred            
$scope.submitForm = function() {

    // check to make sure the form is completely valid
    if ($scope.step1Form.$valid) {
        // now we will go to server side validation
        // AJAX calls.......
        // lets say we got this back:
        var problem = { field: 'email', msg: 'this email is already registered'};

        // now we need to setValidity for email input.
        var errorVariableName = $parse(problem.field + 'ServerError');  // Get the name of the error string variable.
        errorVariableName.assign($scope, problem.msg);  // Assigns a value to it

        console.log($scope.emailServerError); // = 'this email is already registered'

        // HERE THE PROBLEM:
        // now i need to do something like that:
        // $scope.step1Form. + problem.field + .$setValidity('serverError', false);
        // but i dont know how to this that.

        // i think that i need to get this element ($scope.step1Form. + problem.field) in some way by name, and then use setValidity on it. but i dont know how.. 
    }      
};

the question is in the comments inside the code...

Akuka
  • 73
  • 1
  • 2
  • 6
  • 2
    I believe its just `$scope.step1Form.email.$setValidity` – Fresheyeball Feb 21 '14 at 23:57
  • actually you are already using this syntax in your html. What is the problem exactly? – Fresheyeball Feb 22 '14 at 00:02
  • you right, if i want to do it hardcoded so i just need to do $scope.step1Form.email.$setValidity, but my code didnt know the fields name ("email" is dynamic name.. its can be "firstname", "lastname" or whatever...), so i need to get this element ($scope.step1Form. + 'SomeDynamicVariableName') and then do setValidity on it. you get me? – Akuka Feb 22 '14 at 00:07
  • $scope.step1Form['fieldNameAsString'] – Fresheyeball Feb 22 '14 at 08:23

1 Answers1

3

You can try

$scope.step1Form

and then access the right value with

$scope.step1Form["nameOfProblemfield"]
ecoologic
  • 10,202
  • 3
  • 62
  • 66
Dom
  • 658
  • 6
  • 10