Symfony 3中的自定义验证器约束

时间:2017-03-21 18:48:18

标签: php symfony

我正在尝试在Symfony 3中创建自定义验证器约束。

当然,我已经按照文档结束了这一步:

#AppBundle\Validator\Constraint\Device.php

namespace AppBundle\Validator\Constraint;
/**
 * @Annotation
 */
class Device extends \Symfony\Component\Validator\Constraint
{
    public $message = 'Device "%device%" is not valid';
    public $shortMessage = 'Device ID is too short';
    public $chunkMessage = 'Device ID doesn\'t have a correct number of parts';
 }

约束

#AppBundle\Validator\DeviceValidator.php
namespace AppBundle\Validator;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class DeviceValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint) : void
{
    if (!$myconstraint) {
        //A lot of code here
        $this->context->buildViolation($constraint->shortMessage)->addViolation();
    }
}
}

和FormType

#AppBundle\Form\Type\LoginType.php

namespace AppBundle\Form\Type;

use AppBundle\Validator\Constraint\Device;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Length;

class LoginType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('username', TextType::class, ['required' => true, 'constraints' => [new Length(['min' => 3, 'minMessage' => 'Username is too short'])]])
        ->add('password', TextType::class, ['required' => true, 'constraints' => [new Length(['min' => 3, 'minMessage' => 'Password is too short'])]])
        ->add('device', TextType::class, ['required' => true, 'constraints' => [new Device()]]);
}
}

表单创建如下:

$loginForm = Forms::createFormFactoryBuilder()->addExtension(new HttpFoundationExtension())->getFormFactory()->create(LoginType::class);

但无论我做什么,我得到了这样的回应:

  

{     “代码”:500,    “message”:“选项\”constraints \“不存在。定义的选项包括:\”action \“,\”attr \“,\”auto_initialize \“,\”block_name \“,\”by_reference \“, \“compound \”,\“data \”,\“data_class \”,\“disabled \”,\“empty_data \”,\“error_bubbling \”,\“inherit_data \”,\“label \”,\“ label_attr \“,\”label_format \“,\”mapped \“,\”method \“,\”post_max_size_message \“,\”property_path \“,\”required \“,\”translation_domain \“,\”trim \ “,”upload_max_size_message \“。”   }

根据文件,在我放的地方允许“约束”,我真的不知道为什么Symfony在这里哭。

有什么想法吗? 非常感谢。

2 个答案:

答案 0 :(得分:1)

AppBundle \ Validator \ Constraint \ Device.php中的命名空间不太对。删除尾随"约束":

namespace AppBundle\Validator;

答案 1 :(得分:1)

我认为你缺少ValidatorExtension。

use Symfony\Component\Validator\Validation;
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
$validator = Validation::createValidator();

$formFactory = Forms::createFormFactoryBuilder()
    ->addExtension(new HttpFoundationExtension())
    ->addExtension(new ValidatorExtension($validator))
    ->getFormFactory()
    ->create(LoginType::class);