Symfony ValidatorComponent>FormComponent中的AnnotationMapping [英] Symfony ValidatorComponent > AnnotationMapping in FormComponent

查看:78
本文介绍了Symfony ValidatorComponent>FormComponent中的AnnotationMapping的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个正在使用一些Symfony组件的项目中工作.我的问题是如何使表单组件的表单验证使用AnnotationMapping查找约束.

Im working on a project where I'm using some Symfony Components. My problem is how to make the Form Component's validation of Forms use AnnotationMapping to find the constraints.

设置:

global $loader; //composer - autoload
AnnotationRegistry::registerLoader([$loader, 'loadClass']);

$validator = Validation::createValidatorBuilder()
    ->enableAnnotationMapping()
    ->getValidator();

$formFactory = Forms::createFormFactoryBuilder()
    [...]
    ->addExtension(new ValidatorExtension($validator))
    ->getFormFactory();

实体

/**
 * @ORM\Entity
 * @ORM\Table(name="..")
 */
class Conductor extends AbstractEntity {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     */
    protected $id;

    /**
     * @Assert\NotBlank()
     * @ORM\Column(type="string")
     */
    protected $pattern;

[...]

}

构建表单


$builder = $App->getFormFactory()->createBuilder(FormType::class, $entity_data);
foreach ($fields as $field) {
    $builder->add(
        $field,
        null,
        [
            "attr" => array("class" => "..."),
        ]
    );
}
$builder->getForm();

表单提交/验证


if($request->isMethod('POST')) {
            
    $formTable = $this->createFormTable( array() );
    $form = $formTable->buildForm($entity);
    $form->submit($this->dataMapper->formDataFromPost());

    /*
    $entity = $this->dataMapper->mapFromPost();
    $validator = Validation::createValidatorBuilder()
      ->enableAnnotationMapping()
      ->getValidator();
    */


    if($form->isValid()) {
        [...]
    } else {
        [...]
    }
}

我正在尝试使NotBlank()约束起作用.但是无论如何,我的表单都会通过验证.如果我使用新的验证器并对其进行验证,它将显示正确的错误.但是Form-> isValid()函数没有.也许未正确配置为使用AnnotationMapping?非常感谢您提供小费或解决方案!

Im trying to make the NotBlank() Constraints work. But my form passes the validation in any case. If I use a new validator and validate with it, it will show me the correct Errors. But the Form->isValid() function does not. Maybe it is not configured correctly to use AnnotationMapping? Thank you very much in advance for tipps or solutions!

问题本地化

handleRequest/提交和验证表单按预期工作!

The form handleRequest / submit and validation are working as expected!

该表单没有任何限制!->从注释映射约束不会发生/无法正常工作.

The form does not have any constraints!! -> Mapping the Constraints from Annotation is not happening / working.

我确实找到了一个类似的问题:

I did find a similar question: Why does Symfony form not validate my DTO with constraint annotations?

推荐答案

我找不到能够通过ValidatorExtension在FormComponent内部进行映射的解决方案.

I wasn't able to find a solution to enable the mapping that should happen inside the FormComponent with the ValidatorExtension.

但是我确实找到了一个功能性的解决方法.我的方法是从验证器的readPropertyMetadata函数获取约束:

But I did find a functional workaround. My approach is to get the Constraints from the readPropertyMetadata function of the validator:


use Symfony\Component\Validator\Validation;



public function buildForm(AbstractEntity $entity) {

    $validator = Validation::createValidatorBuilder()
        ->enableAnnotationMapping()
        ->getValidator();

    $fields = [*ENTITY PRPERTIES*];

    $classMeta = $validator->getMetadataFor($entity);

    foreach ($fields as $field) {
        $metadata = $classMeta->getPropertyMetadata($field);
        if(is_array($metadata) && count($metadata) > 0) {
            $constraints = $classMeta->getPropertyMetadata($field)[0]->constraints;
        } else {
            $constraints = [];
        }
            

        $builder->add(
            $field,
            null,
            [
                "attr" => array("class" => "..."),
                "constraints" => $constraints
            ]
        );
    }
}

现在将约束添加到表单中,验证最终将按预期工作.

As now the constraints are added to the form the validation finally works as expected.

这篇关于Symfony ValidatorComponent>FormComponent中的AnnotationMapping的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆