0

symfony2ValidatorComponentを使用して文字列の簡単な検証を行いたいです。(これはsymfony 2.0にある必要があります)

$responseEmail = 'somestring';

$validator = new Validator(
    new ClassMetadataFactory(new StaticMethodLoader()),
    new ConstraintValidatorFactory()
);

$constraint = new Assert\Collection(array(
    'responseEmail' => new Assert\Collection(array(
        new Assert\Email(),
        new Assert\NotNull(),
    )),
));

$violations = $validator->validateValue(array('responseEmail' => $responseEmail), $constraint);

これは私にエラーを与えます:

Expected argument of type array or Traversable and ArrayAccess, string given

誰もが理由を知っていますか?

4

1 に答える 1

3

現時点では$constraint、responseEmail が配列であることを伝えています。

これを試して:

use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Constraints as Assert;
...
class ...
{
    public function validationAction()
    {
        $validator = Validation::createValidator();
        $responseEmail = 'somestring';
        $constraint = new Assert\Collection(array(
            'responseEmail' => array(new Assert\Email(), new Assert\NotNull()),
        ));

        $violations = $validator->validateValue(array('responseEmail' => $responseEmail), $constraint);
        ...
    }
}
于 2012-11-01T18:06:00.133 に答える