Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

115
Visualizações
Inactive tab validation in ngbNav

I have a form with two tabs and a save button (outside the tab). The first tab has textbox with required validation. Somehow the "required" validation does not fire when user has opened second tab (i.e. first tab is inactive) and click on save button.

HTML Code:

<form #frm="ngForm" (ngSubmit)="onSave(frm)" >
    <button type="submit">Save</button>
    <ul ngbNav #nav="ngbNav" class="nav-tabs">
        <li [ngbNavItem]="1">
            <a ngbNavLink>Details</a>
            <ng-template ngbNavContent>
                <input type="number" class="form-control numeric" id="inputNumber" name="inputNumber"
                        [(ngModel)]="inputNumber" required />
    
            </ng-template>
        </li>
        <li [ngbNavItem]="1">
            <a ngbNavLink>Charts</a>
            <ng-template ngbNavContent>
            </ng-template>
        </li>
    </ul>
</form>

TS Code:

onSave(form) {
    if (!form.valid) { // Valid returns true when the user has opened the second tab and click on save
      return
    };
}

How can I fix this?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

ngbNav provide feature to keep hidden dom elements in case these are from non-active tabs. In your case, as your element is in another tab, you can keep that in DOM, which can easily be validated.

Need to use [destroyOnHide]="false" on ul (where ngbNav is added).

More Info

about 4 years ago · Juan Pablo Isaza Relatório

0

Using [destroyOnHide]="false" would be a quick fix. In a long run, it is worth considering reactive forms.

It is flexible and handles any complex scenarios. It is more component code and less HTML markup which is easier to cover with unit tests.

It takes time to master though. A few hours of training or reading the docs would be required.

about 4 years ago · Juan Pablo Isaza Relatório

0

If the tab is inactive then it's removed from DOM. So the field is not exist and it can't be validated. I would suggest you to have separate field for errors and based on that field show or hide error messages.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda