Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ValidationMessageをいい感じにしようとした話

 ValidationMessageをいい感じにしようとした話

Angular入門者向けLT会 #4のLT資料です

Hiroshi Oda

July 12, 2017
Tweet

More Decks by Hiroshi Oda

Other Decks in Technology

Transcript

  1. AngularͷυΩϡϝϯτͰ͸ɺ ϝοηʔδ͕ϕλॻ͖ validationMessages = { 'name': { 'required': 'Name is

    required.', 'minlength': 'Name must be at least 4 characters long.', 'maxlength': 'Name cannot be more than 24 characters long.', 'forbiddenName': 'Someone named "Bob" cannot be a hero.' }, 'power': { 'required': 'Power is required.' } }; ࢀߟɿhttps://angular.io/guide/form-validation
  2. AngularͷValidation࣮૷ export class Validators { … static required(control: AbstractControl): ValidationErrors|null

    { return isEmptyInputValue(control.value) ? {'required': true} : null; } … Angularͷίʔυ͔Βൈਮ
  3. AngularͷValidation࣮૷ export class Validators { … static required(control: AbstractControl): ValidationErrors|null

    { return isEmptyInputValue(control.value) ? {'required': true} : null; } … Angularͷίʔυ͔Βൈਮ Messageʹؔ͢Δ৘ใΛ࣋ͬͯͳ͍
  4. ཧ૝ͷΠϝʔδ JavaͷBean Validation࢓༷ • Message using parameters Value must be

    between {min} and {max} • Message interpolation @Size(min=5, max=15, message="Key must have \\{{min}\\} \\ \\{{max}\\} characters")
  5. ΍ͬͯΈͨ • Message using parameters ValidationMessageModule.configure({ messages: {between: "Value must

    be between {min} and {max}"} }) NgModuleΛ༻ҙͯ͠ɺϝοηʔδΛઃఆ
  6. ΍ͬͯΈͨ • Message interpolation config = { "name": { required:

    { name: "Name" }, minlength: { name: "Name", min: "4" } }, "power": { required: { name: "Power" } } }; ϝοηʔδͷύϥϝʔλ͚ͩࢦఆ
  7. ΍ͬͯΈͨ • Message interpolation config = { "name": { required:

    { message: "Name is invalid." } } }; ϝοηʔδ·Δ͝ͱͷࢦఆ΋Մೳ