1

I want to show the required error message once the from is submitted but not sure how to do it i am trying as shown below

<div class="controls">
        <form  name="formx" >
        <ul class="form small">
               <div class="tag">ID</div>
                <input type="text" class="small"  name="enrolmentId" ng-model="enrolmentDetail.Id" required="" onkeypress='return !(event.charCode == 32 )'>
                <div class="error text-danger" style="position: absolute;margin-left: 120px;" ng-show="formx.enrolmentId.$error.required && (formx.$submitted || formx.enrolmentId.$touched)"><p>Id is required</p></div>
            </li>
            <li>
                <div class="btn" ng-click="saveDetails(enrolmentDetail)" ng-show="formAction=='save'">SAVE</div>
            </li>
        </ul>
            </form>
    </div>

here now the error message is shown once we dirty the textbox but i want to show the error message once the form is submitted and if there is required error from should not be submitted

trying used ng-submit but not sure how to do it

Please help by creating a fiddle or posting a example to do it

1 Answer 1

2

This is an example

html

<body ng-app="myApp">
<div ng-controller="myCtrl as mc">
 <form  class="form" name="myForm" ng-submit="mc.submit(myForm)" novalidate>
  <div class="form-group">
   <label for="username">Username</label>
   <input name="username" class="form-control" type="text" ng-model="mc.username" required/>
    <p class="text-danger" ng-show="myForm.username.$error.required && mc.submitted">Username is required</p>
   </div> 
   <div class="form-group">
   <label for="password">Password</label>
   <input name="password" class="form-control" type="password" ng-model="mc.password" required/>
    <p class="text-danger" ng-show="myForm.password.$error.required && mc.submitted">Password is required</p>
   </div> 
   <button class="btn btn-success">submit</button>
</form>
<p class="text-success" ng-show="mc.sent && mc.submitted">Form sent</p>

js

angular.module("myApp",[])
  .controller("myCtrl", function(){
    var vm = this;
    vm.submit = submit;
    function submit(form){
       vm.submitted = true;
       if(form.$valid && vm.submitted === true){
         //Send data logic
         vm.sent = true;
       }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, Irvin! This works like magic, nice and simple! Wish I could mark this answer as accepted!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.