1

I am trying to apply required field validation for text-box which is inside data table template.

Required field validation message pop up properly but as there is no form tag i was not able to check form.valid in component.

Please find below code:

 <data-table  id="user-grid"(reload)="reloadItems($event) [items]="userData">

     <data-table-column [header]="'UserName'">
          <template #dataTableCell let-item="item">
              <span> 
                <input type="text" [(ngModel)]="item.UserName" class="form-control" required #UserName="ngModel" name="UserName"/>

                <span class="text-danger" *ngIf="(UserName.errors != null && UserName.errors.required && (UserName.dirty))">
                    Please enter user name.
                </span>
              </span>                 
          </template>
     </data-table-column>
     <data-table-column [header]="'Action'" >
        <template #dataTableCell let-item="item">                                       
           <a title="Save" (click)="save(item)" class="btn green btn-sm">                                           
           </a>                                        
         </template>
     </data-table-column>

   </data-table>

Any help will be appreciable.

2
  • You can apply form validation attributes to the data-table tag too. You do not need explicit from element. Commented Jan 2, 2017 at 7:25
  • @serkandemirel0420 can you please be more specific by showing few lines of code? i am not able to understand Commented Jan 2, 2017 at 7:33

1 Answer 1

1

Put the datatable element inside the form tag having ngForm. It would fire the form validations. Please see below:

<form #testform="ngForm">
     <data-table>
     </data-table>
</form> 
Sign up to request clarification or add additional context in comments.

Comments

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.