Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating paginated forms in Angular JS (1.x)

I have a form in angular using tables with input field. Users can add and delete rows. Each cell can be of input type text,number date etc. If the table is too big the form becomes slow. One way to solve this is to paginate the table.

Unfortunately paginating the table is a problem because I have custom validations on the input fields and the form should not submit if any of the fields in any page are not valid. For e.g. The user may fill the first page and submits the form without filling the second page. Angular must throw an error for the fields in the second page. Currently I am using angular implementation of form directive to manage errors. Angular forms only show the fields in the current page and not in the second page.

Take a look at this plunkr . The form is of the below format.

 <form name="tableForm" novalidate>
      {{tableForm.$valid}}
      <button ng-click="previousPage()">Previous</button>
      <button ng-click="nextPage()">Next</button>
    <table>
      <thead>
        <tr>
          <td>Text</td>
          <td>Date</td>
          <td>Textarea</td>
        </tr>
      </thead>
      <tbody>
        <tr ng-repeat="item in TableData | limitTo :5:offset">
          <td>
            <input name="Text_{{$index+offset}}" type="text" ng-model="item.Text" ng-required="true" />
            <div ng-messages="tableForm.Text_{{$index+offset}}.$error"  ng-messages-include="error-messages" class="errorMsg">
                        <div ng-message="required" class="error_text">This field is required</div>
                    </div>
          </td>
          <td>
            <input name="Date{{$index+offset}}" type="date"  ng-model="item.Date"  />
          </td>
          <td>
            <textarea name="Textarea_{{$index+offset}}"  ng-model="item.Textarea"  ></textarea>
          </td>
        </tr>
      </tbody>
    </table>
    </form>

In the table, the text field has a required validation. In the first page, the form is valid as all the text fields are filled. But in the second page, one text field is not filled. So the form should actually be invalid. But it becomes invalid only if I go to the next page.

How do I solve this problem ?

like image 653
Syed Suhail Ahmed Avatar asked Sep 05 '16 10:09

Syed Suhail Ahmed


People also ask

What is used for validating AngularJS forms?

Form Validation AngularJS also holds information about whether they have been touched, or modified, or not. You can use standard HTML5 attributes to validate input, or you can make your own validation functions. Client-side validation cannot alone secure user input. Server side validation is also necessary.

How is validation done in AngularJS?

AngularJS performs form validation on the client side. AngularJS monitors the state of the form and input fields (input, text-area, select), and notify the user about the current state. AngularJS also holds information about whether the input fields have been touched, modified, or not.

What is $setValidity in AngularJS?

The $setValidity() function is a built-in AngularJS function that is used to assign a key/value combination that can be used to check the validity of a specific model value. The key in this case is “unique” and the value is either true or false.

How do I refresh a form in AngularJS?

1 Answer. Show activity on this post. $('#form_id'). trigger("reset");


1 Answers

As @Stepan Kasyanenko said, if you remove the element from DOM (limitTo does this) validation will not work. If you put the elements in DOM but hide them, then the error messages that are shown will not be very useful as the inputs are not shown to user.

So, you can set a form for each page. The submit button will go to next page if validation for the current page is OK. The submit button on last page will save the form to server. If you would like to save all submitted information, then you can save only the current page on submits to server.

like image 68
yogurt Avatar answered Oct 09 '22 23:10

yogurt