Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

Angular/typescript how to make 2 out of 4 input fields required

design decisions sub systems waarom frameworks library's

hello! im am quite new to angular and have encountered a problem which I cant seem to solve in a proper way, meaning I could make a really long and big if statement but then I wouldnt learn anything from it. The problem is that I have a big form which can be devided in 4 sections every section has 5 input fields. in order to go to the next web page I want to require that you have completely filled in 2 or more sections.

I tried the following

tabel11: new FormGroup({
        tabel11A: this.tabel11A,
        tabel11B: this.tabel11B,
        tabel11C: this.tabel11C,
        tabel11D: this.tabel11D,
        tabel11E: this.tabel11E
      })
if(this.inputTabelForm.get('tabel9').valid){
      i+= 1;
    }
    if(this.inputTabelForm.get('tabel10').valid){
      i+= 1;
    }
    if(this.inputTabelForm.get('tabel11').valid){
      i+= 1;
    }
    if(i >= 2){
      return true;
    }

it seems that when nothing is filled in so when all fields are NULL they are still perceived as valid which is making it a lot more complicated to me. If someone knows how you are supposed to solve this it would be great!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When checking this.inputTabelFrom.get('tabelx') you are checking the whole subtable as an object and not all specific instances of the subtable. If you want to check whether all the inputs in one of the subforms are filled in, you will have to check all inputs of that subform. For more information on form validation with Angular, please check this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...