2

I'm trying to apply an AngularJs validation in my project. So the following code was written:

 <form name="recipeTitle">
   <input ng-if="edit" required type="text" ng-model="recipe.name" name="name"/>
 </form>
 <span>valid = {ng recipeTitle.name.$valid ng}</span>
 <span>errors = {ng recipeTitle.name.$error ng}</span>

If no value was entered in input, that returns:

valid = false
errors = {"required":true}

When any value appears in input field, validation returns:

valid = false 
errors = {}

And I have no idea, how to make a $valid property become true. Appreciate any help, thanks.

6
  • 2
    If you will provide a plunker link for example it would be much easier to help you. Commented Jun 6, 2015 at 13:27
  • Plunker. But plunker code with minimum necessary changes works as expected) Commented Jun 6, 2015 at 13:32
  • 1
    If nothing else works, you could manually set valid to true via JavaScript if 'errors' is empty and false otherwise, every time the input is changed Commented Jun 6, 2015 at 13:34
  • @Kamil code in plunker works perfectly. Commented Jun 6, 2015 at 13:37
  • @Rebornix yep, it does. And I have no idea what i have done wrong in my app. Commented Jun 6, 2015 at 13:38

1 Answer 1

3

I had the same issue. It works if you use the $scope:

<span>valid = {ng $scope.recipeTitle.name.$valid ng}</span>
<span>errors = {ng $scope.recipeTitle.name.$error ng}</span>

instead of

<span>valid = {ng recipeTitle.name.$valid ng}</span>
<span>errors = {ng recipeTitle.name.$error ng}</span>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer. I can not check it right now, couse we've already made a workaround, but I'l check it in order to help smb.)

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.