0

I have an app with few configurations so we decided to make a wizard that will guide the user through the necessary steps I though it would be simple since i already have the components ready but when inserting them to the wizard component, well they render but the dynamic validations we have are no longer there... To make it clearer we've implemented a client side validation where form fields turn from red to green using ngClass. On the component itself its working neatly but when i weave the component into the wizard (parent) then i lose this functionality

ill show the relevant parts of the code Business config component

import { Component, OnInit, AfterViewInit } from '@angular/core';
import { FormGroup, FormControl, Validators, FormBuilder } from '@angular/forms';
import { Router } from '@angular/router';

@Component({
  selector: 'app-business-setting',
  templateUrl: './business-setting.component.html',
  styleUrls: ['./business-setting.component.css']
})
export class BusinessSettingComponent implements OnInit, AfterViewInit {

  form: FormGroup;

  // if true we will not display the submit buttom
  isWizard;

  constructor(private formBuilder: FormBuilder, private router: Router) {
    if (this.router.url.includes('wizard')) {
      this.isWizard = true;
    }
   }

  ngOnInit() {
    this.form = this.formBuilder.group({
      companyName: [null, [Validators.required, Validators.minLength(3)]],
      companyEmail: [null, [Validators.required, Validators.email]],
      logoImage: [null, []],
      websiteUrl: [null, []],
      phoneNumber: [null, [Validators.required]],
      mobileNumber: [null, []],
      address: [null, [Validators.required]],
      city: [null, [Validators.required]],
      country: [null, [Validators.required]],
      branches: [null, []]

    });

  }
  ngAfterViewInit() {
    // start the tags input
    (<any>$('input[data-role=tagsinput], select[multiple][data-role=tagsinput]')).tagsinput();
  }

  saveForm() {
    console.log(this.form.value);
    console.log($('#branches').val());
  }
}

and the template

<form [formGroup]="form" >
    <div class="row">
        <div class="col-md-6">
            <div class="form-group" 
              [ngClass]= "{'has-danger': !form.get('companyName').valid && form.get('companyName').touched, 
                           'has-success': form.get('companyName').valid}">
                <label> Company Name : <span class="danger">*</span> </label>
                <input type="text" class="form-control required"
                [ngClass]= "{'form-control-danger': !form.get('companyName').valid && form.get('companyName').touched, 
                'form-control-success': form.get('companyName').valid}"
                formControlName="companyName"> </div>
        </div>
        <div class="col-md-6">
            <div class="form-group" [ngClass]= "{'has-danger': !form.get('companyEmail').valid && form.get('companyEmail').touched, 
            'has-success': form.get('companyEmail').valid}">
                <label> Company Email Address : <span class="danger">*</span> </label>
                <input type="email" class="form-control required" [ngClass]= "{'form-control-danger': !form.get('companyEmail').valid && form.get('companyEmail').touched, 
                'form-control-success': form.get('companyEmail').valid}"
                formControlName="companyEmail"> </div>
        </div>
    </div>

and now the parent (wizard) component again showing the relevant template part

  <form action="#" class="tab-wizard wizard-circle">
    <!-- Step 1 -->
     <h6>Store setting</h6>
        <section>
           <app-business-setting></app-business-setting>
        </section>
    </form>

I hope my question is clear as i don't understand the problem and why im noticing the validation part not working i dont know if its related to this https://github.com/angular/angular/issues/9281

UPDATE****

its not im using a third party plugin http://www.jquery-steps.com/ inside my parent controller (the wizard) im activating it like this

ngOnInit() {
    console.log('wizard after view init');
    (<any>$('.tab-wizard')).steps({
        headerTag: 'h6'
        , bodyTag: 'section'
        , transitionEffect: 'fade'
        , titleTemplate: '<span class="step">#index#</span> #title#'
        , labels: {
            finish: 'Submit'
        }
        , onFinished: function (event, currentIndex) {
           console.log('finished wizard');

        }
    });
}

this is causing the validation to stop working though i dont understand why

yurzui
  • 205,937
  • 32
  • 433
  • 399
naoru
  • 2,149
  • 5
  • 34
  • 58
  • First comment would be that you shouldn't have nested `
    ` elements; I'd remove the outer form element from your parent component. If the problem persists after that, can you let us know whether you see any errors in the dev console while `BusinessSettingComponent` is nested, that you don't see when it's on its own?
    – Jack Koppa Oct 06 '17 at 23:00
  • @JackKoppa i changed the form tag in the parent to a div, validation still doesnt work and console is crystal clear.... no errors whatsoever – naoru Oct 06 '17 at 23:07
  • @JackKoppa i updated the question, found out what is breaking it though i don't know why.. – naoru Oct 06 '17 at 23:27
  • Hmmm... I can take a look at the plugin. I created a couple quick examples on Stack Blitz, to show that (without the Router or the jQuery), nesting the form should not cause an issue. So here's [the basic form](https://stackblitz.com/edit/angular-simple-reactive-validation), and here's [the basic form nested](https://stackblitz.com/edit/angular-simple-nested-reactive-validation) inside an extra parent. – Jack Koppa Oct 06 '17 at 23:32
  • I'll take a quick look at the plugin, but in the meantime, see if you're able to fork either of those examples and reproduce the issue you're seeing. Presumably, it's a problem with the implementation of the plugin. – Jack Koppa Oct 06 '17 at 23:32

0 Answers0