PrimeNG table validation

家住魔仙堡 提交于 2019-12-13 00:49:40

问题


I've following PrimeNG Table Here is the StackBlitz demo.

https://stackblitz.com/edit/datatablevalidation

Only one column "Value" is editable.

In "Value" column, I want to add some validation based on the column "Type". I am rendering column value type dynamically

<input pInputText [(ngModel)]="rowData[col.field]" type={{rowData.propValueType.toLowerCase()}} class="form-control" />  

My biggest challenge is validating the table on the first load because column Type is a new column feature of the existed product and data already existed in the table, i need to show all validation errors when a table is initiated.

From my sample, you can see that the first row is has type email has an invalid value , same for row all rows


回答1:


You can show to user that some value is incorrect like this:

<ng-template pTemplate="output">
    <p>{{rowData[col.field]}}  </p>
    <p *ngIf="rowData.propValueType.toLowerCase() =='email' 
              && !isCorrectEmail(rowData[col.field])" 
        style="color:red">
          incorrect email
     </p>
</ng-template>

and then in your .ts file:

isCorrectEmail(emailAddress: any) {
    let re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(String(emailAddress).toLowerCase());        
}

For other types you can write other validation methods.



来源:https://stackoverflow.com/questions/57190311/primeng-table-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!