angularangular-materialngx-formly

ngx-formly: How to trigger blur event in formly for a custom control like dateTimePicker?


Blur event is not triggering for custom control in angular formly. How to bind blur event for a custom component ? I have created custom control for dateTimePicker and using Material UI with angular version 9, formly version 5.5

export class AppComponent {
  form = new FormGroup({});
  model: any = {};
  options: FormlyFormOptions = {};
  fields: FormlyFieldConfig[] = [
    {
      key: "DateTimePicker",
      type: "dateTime",
      templateOptions: {
        label: "Accept terms",
        description: "In order to proceed, please accept terms",
        pattern: "true",
        required: true,
        blur: () => {
          console.log("blur called");
        }
      },
      validation: {
        messages: {
          pattern: "Please accept the terms"
        }
      }
    }
  ];
}

Solution

  • formlyAttributes directive handle the blur event so make sure its defined in your custom type as mentioned in our doc https://formly.dev/guide/custom-formly-field

    <input type="input"
     [formControl]="formControl"
     [formlyAttributes]="field"
    />
    

    In case it doesn't work or your component use a different output name for blur event, you must declare it:

    <input type="input"
     (blur)="to.blur ? this.to.blur(this.field, $event): ''"
    />