Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate table row data with Angular?

I have table with ng-repeat for table rows. I want to make inline editing and validation of data from single row, but it is impossible to use form or ng-form inside table. For inline editing I use inputs with ng-show inside td. So, I have two questions:

  1. Is it correct approach for inline editing? (using hidden inputs inside td).

  2. How can I validate data from row?

Update: I want for table row something like "form submitting" and add error class for table cells with wrong data.

I'm new to angular.

like image 584
uladzimir Avatar asked Feb 07 '14 09:02

uladzimir


People also ask

What is validator in angular?

A validator in Angular is a function which returns null if a control is valid or an error object if it's invalid. For model-driven forms we create custom validation functions and pass them into the FormControl constructor.

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.


1 Answers

NG-Form works on elements that are not a HTML form. So, you should be able to use the built ng-form validations inside a table. It seems to track the forms properly per row for me.

https://docs.angularjs.org/api/ng/directive/form

 <tr ng-repeat="market in markets | orderBy:'name'" ng-form name="myForm">
   <td>{{market.id}}</td>
   <td ng-class="{'has-error': !myForm.minimum.$valid}">
     <input type="number" name="minimum" min="0" max="10000" ng-model="market.minimum" />
   </td>
   <td ng-class="{'has-error': !myForm.cash.$valid}">
    <input type="number" ng-model="market.cash" min="0" name="cash" />
  </td>
  <td>
    <input type="submit" ng-disabled="!myForm.$valid" ng-click="save(market)"/>
  </td>
</tr>
like image 73
Dustin Avatar answered Oct 25 '22 15:10

Dustin