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

186
Visualizações
how to validate input field which contains object

how can I check that the required input field is not empty, formControl cannot read the object...

  public discountParamsForm = new FormGroup({
     name: new FormControl('', Validators.required),
     type: new FormControl('', Validators.required),
     status: new FormControl('', Validators.required),
     value: new FormControl('', [Validators.required, Validators.pattern(/^([0-9]{1,2}){1}(\.[0-9]{1,2})?$/),
       Validators.min(1), Validators.max(100)]),
     dateInput: new FormControl('', Validators.required),
     categorySelect: new FormControl('', Validators.required)
  });

<input formControlName="dateInput" fullWidth type="text" [(ngModel)]="dateRange"
           nbInput placeholder="{{pickDateUI}}" [nbDatepicker]="datepicker">
    <nb-rangepicker (rangeChange)="onDateChange($event)" format="yyyy-MM-dd" #datepicker></nb-rangepicker>

console.log for value and discountparamsform

UI

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

0

never, never use in the same tag [(ngModel)] and formControlName

Well, use a custom Validator.

discountParamsForm = new FormGroup({
    ...
    dateInput: new FormControl('', this.validatorRange()),
})

validatorRange(){
  return (control:AbstractControl)=>{
     if (!control.value.start)
         return {error:'start date is mandatory'}
     if (!control.value.end)
         return {error:'end date is mandatory'}
  }
}

See that the control is always an object with properties "start" and "end" -so always has value, so in the custom validator we check if this properties has value or not-

about 4 years ago · Juan Pablo Isaza Relatório

0

A couple things here, you are looking at the form object itself. You can see on the image of the console, the entire form is INVALID.

If you want to see if one field in a form you can use the get method.

in your component

this.discountParamsForm.get('name');

this returns a FormControl object, and has values such as valid/invalid and many other use full indicators.

you can add a getter method in your component to check this in your html.

get name() {
  return this.discountParamsForm.get('name');
}
<input [ngClass]="{'my-invalid-class': name.invalid}"

since we have a getter method we dont need to check the form directly and we can easily access the control inside the parent form.

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