Skip to content

Instantly share code, notes, and snippets.

@kurtsson
Created November 11, 2018 21:04
Show Gist options
  • Save kurtsson/e7f730339e4749e36fc5f9ae8694f82a to your computer and use it in GitHub Desktop.
Save kurtsson/e7f730339e4749e36fc5f9ae8694f82a to your computer and use it in GitHub Desktop.
testar-validering-av-formular
<div class="container">
<h1>Hero Form</h1>
<form class="needs-validation was-validated">
<div class="form-row">
<div class="col-md-4 mb-3">
<label for="validationCustomUsername">Username</label>
<div class="input-group">
<input [formControl]="usernameinput" type="text" class="form-control {{(usernameinput.invalid && usernameinput.dirty) ? 'is-invalid' : ''}}" id="validationCustomUsername" placeholder="Username">
<div class="invalid-feedback">
Please choose a username.
{{usernameinput.errors}}
</div>
</div>
</div>
</div>
<button type="submit" class="btn btn-success" (click)="onSubmit()">Submit</button>
</form>
</div>
import { Component } from "@angular/core";
import { Hero } from "../hero";
import { FormControl, Validators } from "@angular/forms";
@Component({
selector: "app-hero-form",
templateUrl: "./hero-form.component.html",
styleUrls: ["./hero-form.component.scss"]
})
export class HeroFormComponent {
powers = ["Really Smart", "Super Flexible", "Super Hot", "Weather Changer"];
model = new Hero(18, "Dr IQ", this.powers[0], "Chuck Overstreet");
submitted = false;
usernameinput = new FormControl("", [
Validators.required,
Validators.maxLength(3)
]);
onSubmit() {
this.submitted = true;
}
// TODO: Remove this when we're done
get diagnostic() {
return JSON.stringify(this.model);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment