2

I'm using the md-input component of material for Angular 2. I know it's still a alpha version of material but maybe someone could explain me how to use the html validation attribute required of Angular 2 with md-input(Is it implemented yet?). I have tried this(works fine):

<md-card>
    <md-input 
        placeholder="Url"
        id="url"
        url="url"
        [(ngModel)]="urlInputValue"
        #url="ngModel"
        required>
        <md-hint *ngIf="url.errors && (url.dirty || url.touched)" [ngStyle]="{'color': 'red'}"> Url is required </md-hint>            
    </md-input>

    <button
        md-raised-button color="accent"
        [disabled]="isUrlInputEmpty()"
        (click)="onRequestBtnClick()">
        Request
    </button>
</md-card>

How I can use 'required'?

<md-hint *ngIf="url.errors.required && (url.dirty || url.touched)" [ngStyle]="{'color': 'red'}"> Url is required </md-hint>
5
  • you want a control over the md-input or just you want that it should be required ? Commented Dec 13, 2016 at 20:21
  • It should be required. When I use url.errors.required instead of url.errors I get "Cannot read property 'required' of null" error message. Commented Dec 13, 2016 at 20:27
  • yes , normally , what do you have in the url in the TS part ? and normally if it is mentioned required in the md-input element it should be required. Commented Dec 13, 2016 at 20:32
  • I implemented nothing specific for the url in the ts file. Just declared the 'urlInputValue' property. I think I need a container(div?) to prevent the null error of the url.errors.required. How can I do that with md-input? Commented Dec 13, 2016 at 20:57
  • Do you know FromGroup module ? Commented Dec 13, 2016 at 21:05

1 Answer 1

2

In your TS file you should have :

import { FormGroup, FormBuilder, FormControl, Validators } from '@angular/forms';

export class UrlComponent  {   
   public urlForm: FormGroup;
  constructor(private formBuilder: FormBuilder) {

  this.urlForm = this.formBuilder.group({
  url:    new FormControl('', Validators.required),
   });
  }
//... codes..
}

And change your HTML to :

<form role="form" [formGroup]="urlForm" novalidate>
<md-input 
    placeholder="Url"
    id="url"
    url="url"
    [(ngModel)]="urlInputValue"
    formControlName="url"
    #url="ngModel"
     >
    <md-hint *ngIf="url.errors && (url.dirty || url.touched)" [ngStyle]="{'color': 'red'}"> Url is required </md-hint>            
</md-input>

<button
    md-raised-button color="accent"
    [disabled]="isUrlInputEmpty()"
    (click)="onRequestBtnClick()">
    Request
</button>

</form>
Sign up to request clarification or add additional context in comments.

1 Comment

this is reactive form approach how to to it using template driver approach ?

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.