0

I am new to AngularJS and I have a simple test form as shown below. Without AngularJS validation, value in the input field is retained if the validation at server fails and the form is returned to the user. If I add AngularJS validation but the form is submitted when its invalid, validation fails at the server end and the form is returned to the user, but the input field is empty. I can see AngularJS is setting ng-empty for this field.

I was browsing through and I could not find similar situation addressed anywhere yet. I was thinking may be I should add those form values to cookie and then refill if there is a validation error. Is this the only approach or is there a much simpler way to do this?

<s:form name="form" action="/actionname" novalidate="novalidate" >  
        <div > 
            <label>Name</label>
            <s:textfield type="text"  name="user.name" size="25" ng-model="name" ng-required="true" cssClass="formfield" />                             
            <span class="customErrorMessage" ng-show="form['user.name'].$touched && form['user.name'].$error.required">Please enter your name.</span>                                   

        </div>
        <div>
            <s:submit type="button"  cssClass="submitButton">Take me in!</s:submit>
        </div>
    </s:form>
Roman C
  • 49,761
  • 33
  • 66
  • 176
Pradeep
  • 1
  • 1
  • 3
  • You should use ajax to submit a form – Roman C Nov 20 '16 at 12:01
  • Thanks @RomanC something like this you mean? [link](http://tutsnare.com/post-form-data-using-angularjs/) My actual form will have almost 25 fields, is this still the simplest way? – Pradeep Nov 20 '16 at 12:15
  • Why not? Angular is using http service to make a post request, but you can use xhr, it's up to you. – Roman C Nov 20 '16 at 12:58
  • Its just that this requires more code to be written in my case... let me check @RomanC. Thank you for your help. – Pradeep Nov 20 '16 at 14:05
  • You can post the code here you have tried, or if you have a question then clarify your question. – Roman C Nov 20 '16 at 14:15
  • Will try writing some code, but I have one question before starting though. How should the validation errors be returned from the server? Can it be JSON or is it Java Map with field name and error message as a pair? – Pradeep Nov 20 '16 at 14:44
  • http://stackoverflow.com/a/26845204/573032, or http://stackoverflow.com/a/24969874/573032 – Roman C Nov 20 '16 at 17:23

0 Answers0