0

I have used reactive forms in my angular 2 project. I'm trying to do button validation but facing bit confuse. Here my form contains several text fields and buttons. In my form there are two way's to give input. i.e through input textfields and upload spread sheet button.

If we try to give input from text fields, my upload button should disable and If remove data from textfields upload button should enable. How to achieve this functionality.

Sample example: https://stackblitz.com/edit/angular-vnqqcx

3
  • stackblitz would be great. Commented Feb 4, 2019 at 13:45
  • you should better use !SignupForm.valid to disable your submit button Commented Feb 4, 2019 at 14:47
  • @MehdiBenmoha Yeah I've tried already which you suggested, but I don't think so this will work for this particular scenario. Commented Feb 4, 2019 at 17:14

2 Answers 2

1

You can subscribe to

empty=true;
this.SignupForm.controls.userData.valueChanges.subscribe(res=>{
      this.empty=!res.username && !res.email
    })

Or create a customValidator and check is the group es valid

  this.SignupForm = new FormGroup({
      'userData': new FormGroup({
        'username': new FormControl(null),
        'email': new FormControl(null),
      }, this.userDataValidator())
    });
  userDataValidator() {
    return (group: FormGroup) => {
      return (!group.value.username && !group.value.email) ? null : 
         { error: 'filled' }
    }
  }

or use a getter

  get isEmpty()
  {
    return !this.SignupForm || !this.SignupForm.controls.userData
           || (!this.SignupForm.controls.userData.value.username &&
               !this.SignupForm.controls.userData.value.email)
  }

see stackblitz

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for answer, Yes this is working fine but In my case there are nearly fifty text fields in my form. So is this is correct approach for that scenario to check every field empty or not??
In this case, I'll put at first in the form a radio button to allow choose to the user upload the data or fill the data manually, but it's only an idea
Ohh Okay..Any how thanks for your ans and your response.
0

what you want do do is to allow user to submit the form it it is valid. That is what valid/invalid properties of Form are for. So instead

<button class="btn btn-primary" type="submit" [disabled]="SignupForm.dirty" >Submit</button>  

use

    <button class="btn btn-primary" type="submit" [disabled]="SignupForm.invalid" >Submit</button> 

It works well with data validation.

https://stackblitz.com/edit/angular-cdfj8z?file=src/app/app.component.html

1 Comment

Thanks for your answer, but this is not I am expecting. My scenario is different, I don't want to submit a form. Test fields and button purpose is different here. If we use use textfields then button not required so need to disable. If we remove data then button should enable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.