Skip to content

Validators: required <select> marked as ng-invalid for option value "0" #6527

@cghislai

Description

@cghislai

I have a required select with enum values options :

 export enum Pricing {
  VALUE0,
  VALUE1,
  VALUE2
}


@Component({
  selector: 'home',
  template: `
    Welcome Home
    <form>
     <select id="pricing"
        [ngModel]="pricingModel"
        (change)="onPricingChanged($event)"
        ngControl="pricingControl" required>
        <option *ngFor="#pricing of allPricings"
                [value]="pricing">
            {{ getPricingLabel(pricing) }}
        </option>
    </select>
    {{ pricingModel }}
  `,
  styles: [`
  .ng-invalid { border: 2px solid red; }
  `]
  viewProviders: [FORM_DIRECTIVES]
})
export class Home {

  pricingModel: Pricing;

  allPricings: Pricing[] = [
    Pricing.VALUE0,
    Pricing.VALUE1,
    Pricing.VALUE2
    ];
    constructor() {
      this.pricingModel = Pricing.VALUE0;
    }

    onPricingChanged(event) {
      var value:string = event.target.value;
      var pricing:Pricing = <Pricing>parseInt(value);
      this.pricingModel = pricing;
    }

    getPricingLabel(pricing: Pricing) {
      return Pricing[pricing];
    }
}

Using this code, the control is marked as invalid when selecting the first value.
plunker: http://plnkr.co/edit/ABh8pzp1afXkINcunKR8?p=preview

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions