LoginSignup
3
3

More than 5 years have passed since last update.

5分で分かるSymfony2のValidation

Last updated at Posted at 2015-03-13

バリデーション | Symfony2日本語ドキュメント

の要約。

class Author
{
    public $name;
}

バリデーション対象のエンティティクラスがあるとして

# src/Acme/BlogBundle/Resources/config/validation.yml
Acme\BlogBundle\Entity\Author:
    properties:
        name:
            - NotBlank: ~

そのエンティティに対するバリデーションルールを設定して

$validator = $this->get('validator');
$errors = $validator->validate($author);

Validatorvalidateメソッドを実行するとConstraintViolationListが返ってくる。名前の通りConstraintViolationのlistなので

foreach ($errors as $error) {
    echo $error->getMessage();
}

でエラーメッセージが取れる

まとめ

ちゃんとしたIDEが手元にあるなら、このページをブラウザで開く必要はないのでIDEからAPIドキュメントを見よう

3
3
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
3
3