ValidationPrimeNG input components indicate validation errors automatically when their value is marked as invalid.

Validation - 图1

View on GitHub

  • validationdemo.ts
  • validationdemo.html

    validationdemo.ts

  1. import {Component,OnInit} from '@angular/core';
  2. import {Validators,FormControl,FormGroup,FormBuilder} from '@angular/forms';
  3. import {SelectItem} from '../../../components/common/api';
  4. import {MessageService} from 'primeng/api';
  5. @Component({
  6. templateUrl: 'showcase/demo/validation/validationdemo.html',
  7. providers: [MessageService]
  8. })
  9. export class ValidationDemo implements OnInit {
  10. userform: FormGroup;
  11. submitted: boolean;
  12. genders: SelectItem[];
  13. description: string;
  14. constructor(private fb: FormBuilder, private messageService: MessageService) {}
  15. ngOnInit() {
  16. this.userform = this.fb.group({
  17. 'firstname': new FormControl('', Validators.required),
  18. 'lastname': new FormControl('', Validators.required),
  19. 'password': new FormControl('', Validators.compose([Validators.required, Validators.minLength(6)])),
  20. 'description': new FormControl(''),
  21. 'gender': new FormControl('', Validators.required)
  22. });
  23. this.genders = [];
  24. this.genders.push({label:'Select Gender', value:''});
  25. this.genders.push({label:'Male', value:'Male'});
  26. this.genders.push({label:'Female', value:'Female'});
  27. }
  28. onSubmit(value: string) {
  29. this.submitted = true;
  30. this.messageService.add({severity:'info', summary:'Success', detail:'Form Submitted'});
  31. }
  32. get diagnostic() { return JSON.stringify(this.userform.value); }
  33. }
  34.  

validationdemo.html

  1. <p-toast [style]="{marginTop: '80px'}"></p-toast>
  2. <form [formGroup]="userform" (ngSubmit)="onSubmit(userform.value)">
  3. <p-panel header="Validate">
  4. <div class="ui-grid ui-grid-responsive ui-grid-pad ui-fluid" style="margin: 10px 0px">
  5. <div class="ui-grid-row">
  6. <div class="ui-grid-col-2">
  7. First Name *:
  8. </div>
  9. <div class="ui-grid-col-6">
  10. <input pInputText type="text" formControlName="firstname" placeholder="Required"/>
  11. </div>
  12. <div class="ui-grid-col-4">
  13. <p-message severity="error" text="Firstname is required" *ngIf="!userform.controls['firstname'].valid&&userform.controls['firstname'].dirty"></p-message>
  14. </div>
  15. </div>
  16. <div class="ui-grid-row">
  17. <div class="ui-grid-col-2">
  18. Last Name *:
  19. </div>
  20. <div class="ui-grid-col-6">
  21. <input pInputText type="text" formControlName="lastname" placeholder="Required"/>
  22. </div>
  23. <div class="ui-grid-col-4">
  24. <p-message severity="error" text="Lastname is required" *ngIf="!userform.controls['lastname'].valid&&userform.controls['lastname'].dirty"></p-message>
  25. </div>
  26. </div>
  27. <div class="ui-grid-row">
  28. <div class="ui-grid-col-2">
  29. Password *:
  30. </div>
  31. <div class="ui-grid-col-6">
  32. <input pInputText type="password" formControlName="password" placeholder="Required - Min(6)"/>
  33. </div>
  34. <div class="ui-grid-col-4">
  35. <p-message severity="error" [text]="userform.controls['password'].errors['required'] ? 'Password is required' : userform.controls['password'].errors['minlength'] ? 'Must be longer than 6 characters' : ''"
  36. *ngIf="!userform.controls['password'].valid&&userform.controls['password'].dirty"></p-message>
  37. </div>
  38. </div>
  39. <div class="ui-grid-row">
  40. <div class="ui-grid-col-2">
  41. Description:
  42. </div>
  43. <div class="ui-grid-col-6">
  44. <textarea pInputTextarea type="text" formControlName="description"></textarea>
  45. </div>
  46. <div class="ui-grid-col-4"></div>
  47. </div>
  48. <div class="ui-grid-row">
  49. <div class="ui-grid-col-2">
  50. Gender *:
  51. </div>
  52. <div class="ui-grid-col-6">
  53. <p-dropdown [options]="genders" formControlName="gender"></p-dropdown>
  54. </div>
  55. <div class="ui-grid-col-4">
  56. <p-message severity="error" text="Gender is required" *ngIf="!userform.controls['gender'].valid&&userform.controls['gender'].dirty"></p-message>
  57. </div>
  58. </div>
  59. <div class="ui-grid-row">
  60. <div class="ui-grid-col-2"></div>
  61. <div class="ui-grid-col-6">
  62. <button pButton type="submit" label="Submit" [disabled]="!userform.valid"></button>
  63. </div>
  64. <div class="ui-grid-col-4"></div>
  65. </div>
  66. <div style="text-align:center;margin-top:20px" *ngIf="submitted">
  67. Form Submitted
  68. <br>
  69. {{diagnostic}}
  70. </div>
  71. </div>
  72. </p-panel>
  73. </form>
  74.