angular icon indicating copy to clipboard operation
angular copied to clipboard

How to trigger client validation/submission from a different component NG6

Open Sigkill9 opened this issue 8 years ago • 6 comments

I am only using formio to render. Submissions are handled by a legacy api. Submission needs to be controlled by buttons in a different angular component. How can I trigger (client) validation then form submission from my legacy buttons component that is outside of formio?

Sigkill9 avatar Jul 17 '18 20:07 Sigkill9

@Sigkill9 are you able to figure it out. Can you share your experience?

indingpig avatar Aug 15 '18 08:08 indingpig

Yes! Here is the solution I have currently implemented. Thanks to @travist from form.io for the help!

/* 
  FORM SERVICE 
  I supply an event listener here for components to use
  to make a request to validate/save the form
*/

export class MyFormService {
  @Output() saveRequest = new EventEmitter();
  
  save(data):any{
    /* Call to server to save data. */
  }
}

/* 
  BUTTON COMPONENT 
  uses my form service to emit save event
*/

export  class MyFormButtonComponent {
  public _formio: any;
  constructor(private _myformService: MyFrmService) {
  }

  @HostListener('click', ['$event.target'])
  onClick(btn){
    this._myformService.saveRequest.emit();
  }
}

/*
  FORM COMPONENT
  inject the form service to subscribe to the 
  save request event
*/

export class MyFormComponent {
  constructor(private _myformService: MyFormService){  }
    
  @ViewChild(FormioComponent) formioComponent: FormioComponent;
    ngAfterViewInit() {
      this.formioComponent.ready.then((formio:any) => {

        /* LISTEN FOR SAVE EVENT (SUBMIT BUTTONS TO BE CLICKED) */
        this._myformService.saveRequest.subscribe(data=>{
          
          /* triggers validation */
          formio.executeSubmit()
          .then(submission=>{
            /* FORM IS VALID */
              this._myformService.save(data);
            },
            err=>{
              /*
                FORM IS NOT VALID
                err contains an array of field conf literals and
                their corresponding validation errors
              */
            });
        });

      });
    }

  };

Sigkill9 avatar Aug 15 '18 16:08 Sigkill9

@Sigkill9 Thanks to help, But it don't work. What is the 'FormioComponent' ? Is it import { FormioComponent } from 'angular-formio'?

here is my code `import { Component, OnInit, ViewChild, AfterViewInit } from '@angular/core'; import { FormServiceService } from '../../services/form-service.service'; import { FormioComponent } from 'angular-formio';

@Component({ selector: 'app-my-form', templateUrl: './my-form.component.html', styleUrls: ['./my-form.component.css'] }) export class MyFormComponent implements AfterViewInit {

constructor(private _myformService: FormServiceService) { } @ViewChild(FormioComponent) formioComponent: FormioComponent; ngAfterViewInit() { // Here, this.formioComponent is undefined; this.formioComponent.ready.then((formio: any) => { this._myformService.saveRequest.subscribe(data => { formio.executeSubmit().then(submission => { this._myformService.save(data); }, err => { console.log(err); }); }); }); } }`

indingpig avatar Aug 16 '18 06:08 indingpig

Apologies. yes. import { FormioService, FormioComponent } from "angular-formio";

Sigkill9 avatar Aug 16 '18 14:08 Sigkill9

@Sigkill9 Sorry, it still don't work... The formioComponent is undefined.

indingpig avatar Aug 17 '18 02:08 indingpig

Check your version of angular-formio in package.json

"angular-formio": "^2.1.0"

Sigkill9 avatar Aug 17 '18 20:08 Sigkill9