業界トップクラスの求人数を誇る転職エージェントPR

リクルートグループのコネクションを活かした非公開求人も充実、他にはない好条件の求人と出会える

5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Tips]AngularDartのフォームバリデーション(Model Driven)

Posted at

はじめに

フォームバリデーションの方法を備忘録的に残します。
Template drivenなフォームバリデーションは公式に載っていたので、Model drivenなフォームバリデーションについて調べました。

実装内容

ログイン時の簡単なバリデーションです。
sample.gif

実装例

signin_component.dart
import 'package:angular/angular.dart';
import 'package:angular_components/material_input/material_input.dart';
import 'package:angular_components/material_button/material_button.dart';
import 'package:angular_forms/angular_forms.dart';
import 'package:materia_dart/materia_dart.dart';

@Component(
    selector: 'signin',
    templateUrl: 'signin_component.html',
    styleUrls: [
      'signin_component.css'
    ],
    directives: [
      coreDirectives,
      formDirectives,
      materialInputDirectives,
      MaterialInputComponent,
      MaterialButtonComponent,
      NgForm,
      NgFormControl,
      NgFormModel,
    ],
    providers: [
    ])
class SigninComponent {

  SigninComponent(
    ) {
      form = FormBuilder.controlGroup(
        {
          'email': ['', (AbstractControl c) {
            if (c.value == '') return {'error': 'メールアドレスは必須です'};
          }],
          'password': ['', (AbstractControl c) {
            if (c.value == '') return {'error': 'パスワードは必須です'};
          }]
        }
      );
    }

  ControlGroup form;
  
  void signIn() async {
    print(this.form.value);
  }
}

}
signin_component.html
<h1>SignIn</h1>
<div class="signin-form">
  <form [ngFormModel]="form">
    <material-input floatingLabel label="メールアドレス" type="text" ngControl="email"
      required></material-input>
    <material-input floatingLabel label="パスワード" type="text"
      ngControl="password" required></material-input>
    <material-button raised [disabled]="!form.valid" (click)="signIn()">ログイン</material-button>
  </form>
</div>

下記の行ですが

'email': ['', (AbstractControl c) {
            if (c.value == '') return {'error': 'メールアドレスは必須です'};
          }]

Validators.requiredを使って表現できます。ただこれだとエラー文が英語になってしまいます。自分は日本語化する方法が見つけられなかったため、自由にかける関数の形で定義しました。

'email': ['', Validators.required]

おわりに

もっと色々と深掘りしたい

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?