6
내 양식 유효성 검사에 서버 유효성 검사의 일부가 있습니다. 그래서 나는 서버 목록에서 필드의 이름과 각각의 오류가있는 문자열을 얻어야합니다. 제 아이디어는 모든 필드를 사전에 알지 못하고 이름으로 액세스하여 모든 필드를 다루는 일반적인 코드 지식을 작성하는 것이 었습니다. 이 예를 들어 필드 : 당신이 볼 수있는 것처럼이름으로 AngularJS 요소를 얻는 방법은 무엇입니까?
<!-- 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>
는 변수 emailServerError는 내가 내 응용 프로그램에서 많은 필드가 나는 일반적인 작성하려고 ... 서버 검증에서 오는 오류를 저장됩니다
// 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..
}
};
문제는 ...
코드 내부의 의견에 : 모든 필드에 맞는 코드 ...은 그래서 이것은 각 코드
내가 믿는 (가) 오른쪽 값에 액세스은 단지'$ scope.step1Form.email. 실제로 당신이 이미이 구문을 사용하는 $ setValidity' – Fresheyeball
당신의 html. 문제는 정확히 무엇입니까? – Fresheyeball
당신이 바로, 만약 내가 그것을 할 하드 코드 된 그래서 난 그냥 $ scope.step1Form.email 할 필요가있다. $ setValidity,하지만 내 코드 didnt 필드 이름을 알고 ("이메일"동적 이름입니다 .. 그 "firstname"수 있습니다 , "성"또는 뭐든간에 ...), 그래서이 요소 ($ scope.step1Form. + 'SomeDynamicVariableName')를 가져 와서 setValidity를 수행해야합니다. 너 나 잡아? – user3339306