1

i have tried with many ways:

Validation for select field angular 2

How to apply required validation to dropdown in angular 2

This help me to add validation in dropdown. but i have a issue that validation run properly but when i click on submit button it submit the form if dropdown value is valid or not. i do not want to submit my form if the value of dropdown is "select".

this is my HTML code:

<form name="form" (ngSubmit)="f.form.valid && SaveSymbol()" #f="ngForm" novalidate>
       <div class="form-group" [ngClass]="{ 'has-error': f.submitted && !DisplayGroupID.valid && !manageSymbolViewModel.DisplayGroupID }"> 
          <label for="DisplayGroupID">Display Group</label>
          <select class="form-control" name="manageSymbolViewModel.DisplayGroupID" #DisplayGroupID id="manageSymbolViewModel.DisplayGroupID" [(ngModel)]="manageSymbolViewModel.DisplayGroupID" required>
             <option value="0" selected disabled>Select</option>
             <option *ngFor="let group of result.DisplayGroups" value={{group.DisplayGroupId}}>
                  {{group.DisplayGroup}}
              </option>
          </select>
          <div *ngIf="f.submitted && !DisplayGroupID.valid && !manageSymbolViewModel.DisplayGroupID" class="help-block">Display Group is required</div>
      </div>

 <div class="form-group">
      <button [disabled]="loading" type="submit" class="btn btn primary">Save</button>
       <a [routerLink]="['/login']" class="btn btn-link">Cancel</a>
 </div>
</form>

This is component code:

SaveSymbol() {
       this.manageSymbolService.Save(this.manageSymbolViewModel).subscribe(data => {
                    debugger;

                },
                    error => {
                        // this.alertService.error(error);
                        // this.loading = false;
                    });
            }
    }
6
  • please show your submit function and also form initialisation in your component Commented May 8, 2017 at 5:53
  • edited my question for form initialisation. Commented May 8, 2017 at 6:12
  • please show your component also Commented May 8, 2017 at 6:17
  • also added component code so please give some solution Commented May 8, 2017 at 6:24
  • please check the below solution Commented May 8, 2017 at 6:35

4 Answers 4

5

The first and selected option of your select element has a value 0. So it basically it already has a value set to it on page load.

Try to change it to: <option value="" selected disabled>Select</option> and see if that fixes your problem.

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

3 Comments

i removed 0 from value but then also same issue
Ok then I suspect Angular is treating empty strings as valid input. Change to value selected disabled?
<option value="" selected disabled>Select</option> worked for me
0

Try this

<button [disabled]="loading || !DisplayGroupID.valid || !manageSymbolViewModel.DisplayGroupID" type="submit" class="btn btn primary">Save</button>

This will prevent the form from submitting while there is nothing selected in the dropdown or when the dropdown model is invalid.

2 Comments

this is not what i want. because it is not going to enable any time if i select correct value then also. and i do not want to disable the button.
@DhrutiRathod the I would suggest to go through: angular.io/docs/ts/latest/guide/forms.html the proper way to control the forms in angular 2
0

please change your form as given below and also pass your status of form through submit function if required

<form #f="ngForm" (submit)="f.valid && SaveSymbol(f.value, f.valid)" novalidate>
    <div class="form-group">
      <label>Display Group</label>
      <select ngControl="DisplayGroupID" #DisplayGroupID="ngForm" [(ngModel)]="manageSymbolViewModel.DisplayGroupID" required>
      <option value='' disabled>Select</option>
        <option *ngFor="let group of DisplayGroups" value={{group.DisplayGroupId}}>{{group.DisplayGroup}}</option>
      </select>
      <small [hidden]="DisplayGroupID.valid || (DisplayGroupID.pristine && !submitted)" class="text-danger">
           Displaygroup is required
       </small>
    </div>
    <button type="submit" class="btn btn-default">Submit</button>
  </form>

and initialise the model as

this.manageSymbolViewModel = {
      DisplayGroupID: ''
    };

refer this plunkr http://plnkr.co/edit/sFTM22xbZSXBLZcv90o2

1 Comment

every time isValid true, so page submitted every time
0

If I'm reading your question right...

Your first value will be 0. So simply set your [disabled] attribute accordingly.

Example: [disabled]="loading || DisplayGroupId.value == 0"

Your button will not be enabled while the first option is selected. Simple solution.

Comments

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.