0

I have form fields that are validated using required. The problem is, that the error is displayed immediately when the form is rendered. I want it only to be displayed after the user submit.

My question is similar to this one.

but in my case, I have more than one buttom in the form, so I don't understand how to implement the ng-submit or a solution to my problem.

This is my Plunker of what I want to do.

HTML form:

  <form name="myForm">
    <table>
      <tr>
        <td>
          <input type="text" ng-model="oldName">
        </td>
        <td>
          <button ng-click="copyName()"> Copy >> </button>
        </td>
        <td>
          <input type="text" name="newName" ng-model="newName" required>
        </td>
      </tr>
       <tr>
        <td>
          <input type="text" ng-model="oldEmail">
        </td>
        <td>
          <button ng-click="copyEmail()"> Copy >> </button>
        </td>
        <td>
          <input type="email" name="newEmail" ng-model="newEmail">
        </td>
      </tr>
    </table>

    <br>
    <button ng-click="Submit()">Submit </button>

  </form>
3
  • We are going to need to see some code of what you have already tried. It's too hard to help without seeing what you already have. Commented Mar 30, 2017 at 15:11
  • The plunkr link is helpful, but the relevant code should be included directly in the post, as well. Commented Mar 30, 2017 at 15:47
  • yes, the html is only a preview of the plunker, the plunker has the code. The div that has the ng-show and says "invalid email" I need to show that only when click in the submit button. Commented Mar 30, 2017 at 22:59

1 Answer 1

1

You can simply put $scope.myForm.$setSubmitted() in your $scope.submit function which will set your form submitted. Now, in your error message's ng-show, you can have something like this:

<div ng-show="myForm.$submitted && myForm.newEmail.$invalid">
  <strong>invalid email </strong>
</div>

working plunker

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

7 Comments

Thanks for that. Sorry I didn't understand well your solution, but the error summary, so the div shows Invalida email get triggered inmediatelly when while writting a wronge email without wait for the submit click.
@MarcosF8 do you have API call to make on submit?
no, I don't. When submit it will save the info in the Db doing and http call to my controller, so it goes to the MVC controller.
I am thinking in change the Copy buttons for links so I have only one submit buttons and try to use the Ng-submit.
@MarcosF8 so in success callback of http call, you can just have $scope.myForm.$setPristine() which will reset your form controller, and myForm.$submitted will no longer be true until you submit again.
|

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.