Symfony2表单字段约束验证之前的数据转换器



我已经创建了需要数据转换器的形式,但让自己陷入了一个问题:我通过爆炸字符串转换数据(字符串应该被爆炸成3部分),一切都有效,如果我提供正确的格式字符串,但否则它会在数据转换器内抛出错误,因为如果提供错误的字符串格式,就不能发生转换(这是预期的行为)。

所以问题是有一种方法来验证表单字段的正确字符串数据转换之前?我知道数据转换在默认情况下发生在验证之前,但也许有其他方法?

我发现了一个解决方案,可能在这个线程工作:结合约束和数据转换器,但它看起来像一个粗略的解决方案,除此之外,我需要翻译验证消息,我真的很想使用symfony表单的默认翻译方法(不使用翻译服务)

我想,也有人从symfony IRC (Iltar)建议通过使用事件做到这一点,但我不确定如何去这个-如何将数据转换器动态地附加到表单字段?或许还有别的办法?

可能太晚了,但我最终还是做到了。也许它会对你有帮助。

这是我的FormType:
class PersonType extends AbstractType{
    public function buildForm(FormBuilderInterface $builder, array $options){
        $builder->add('mother', 'personSelector', array('personEntity' => $options['personEntity']));
    }
}

这里是我的customField,这里是验证:

class PersonSelectorType extends AbstractType{
    public function buildForm(FormBuilderInterface $builder, array $options){
        $transformer = new PersonByFirstnameAndLastnameTransformer($this->entityManager,$options);
        $builder->addModelTransformer($transformer);
        $builder->addEventListener(FormEvents::PRE_SUBMIT, array($this, 'onPreSubmitForm'));
    }
    public function onPreSubmitForm(FormEvent $event){
        $mother     = $event->getData();
        $form       = $event->getForm();
        $options    = $form->getConfig()->getOptions();
        if (!empty($mother)){
            preg_match('#(.*) (.*)#', $mother, $personInformations);
            if (count($personInformations) != 3){
                $form->addError(new FormError('[Format incorrect] Le format attendu est "Prénom Nom".'));
            }else{
                $person = $this->entityManager->getRepository($options['personEntity'])->findOneBy(array('firstname' => $personInformations[1],'lastname' =>$personInformations[2]));
                if ($person === null) {
                    $form->addError(new FormError('Il n'existe pas de person '.$personInformations[1].' '.$personInformations[2].'.'));
                }
            }
        }
    }
}

这是我的变压器:

class PersonByFirstnameAndLastnameTransformer implements DataTransformerInterface{
    public function reverseTransform($firstnameAndLastname) {
        if (empty($firstnameAndLastname)) { return null; }
        preg_match('#(.*) (.*)#', $firstnameAndLastname, $personInformations);
        $person = $this->entityManager->getRepository($this->options['personEntity'])->findOneBy(array('firstname' =>$personInformations[1],'lastname' =>$personInformations[2]));
        if (count($personInformations) == 3){
            $person = $this->entityManager->getRepository($this->options['personEntity'])->findOneBy(array('firstname' =>$personInformations[1],'lastname' =>$personInformations[2]));
        }
        return $person;
    }
    public function transform($person) {
        if ($person === null) { return ''; }
        return $person->getFirstname().' '.$person->getLastname();
    }
}

也许您可以将表单的实例传递给您的转换器。如果字符串不能正确解析,只需在表单中添加一个验证错误,如下所示:

<?php
// src/Acme/MyBundle/Form/DataTransformer/StringTransformer.php
namespace AcmeMyBundleFormDataTransformer;
use SymfonyComponentFormDataTransformerInterface;
use SymfonyComponentFormExceptionTransformationFailedException;
use DoctrineCommonPersistenceObjectManager;
use AcmeMyBundleEntityMyEntity;
use AcmeMyBundleEntityAnotherEntity;
use AcmeMyBundleTypeMyEntityType;
class StringTransformer implements DataTransformerInterface
{
  /**
   * @var MyEntityType
   */
  private $form;
  /**
   * @param ObjectManager $om
   */
  public function __construct(MyEntityType $form)
  {
    $this->form = $form;
  }
  /**
   * Transforms an object (entity) to a string (number).
   *
   * @param  MyEntity|null $entity
   * @return string
   */
  public function transform($value)
  {
    // ...
  }
  /**
   * Transforms a string (number) to an object (entity).
   *
   * @param  string $number
   *
   * @return MyEntity|null
   *
   * @throws TransformationFailedException if object (entity) is not found.
   */
  public function reverseTransform($value)
  {
    $collection = new ArrayCollection();
    try{
      $vals = explode(',', $value);
      foreach($vals as $v){
        $entity = new AnotherEntity();
        $entity->setValue($v);
        $collection->add($v);
      }
    } catch(Exception $e){
      $this->form
        ->get('my_location')
        ->addError(new FormError('error message'));
    }
    return $collection;
  }
}

但它看起来像一个粗略的解决方案,除此之外,我需要翻译验证消息,我真的很想使用symfony表单的默认翻译方法(不使用翻译服务)

我知道这个问题很老了,但由于任何答案都被标记为正确的解决方案,我将与您分享另一种方法。

emottet解决方案,在应用模型转换器之前使用预提交侦听器来验证数据,是一种很好的方法。

如果你想继续使用Symfony验证系统来处理这些错误,你可以在你的预提交监听器中使用Symfony验证器服务(ValidatorInterface),并传递它所需的约束,例如:

$builder
    ->add('whatever1', TextType::class)
    ->add('whatever2', TextType::class)
;
$builder->get('whatever1')
    ->addEventListener(FormEvents::PRE_SUBMIT, function(FormEvent $event) {
        $data = $event->getData();
        $form = $event->getForm();
        /** @var ConstraintViolationListInterface $errors */
        if ($errors = $this->validator->validate($data, new Choice([
            'choices' => $allowedChoices,
            'message' => 'message.in.validators.locale.xlf'
        ]))) {
            /** @var ConstraintViolationInterface $error */
            foreach ($errors as $error) {
                $form->addError(new FormError($error->getMessage()));
            }
        }
    })
    ->addModelTransformer($myTransformer)
;

有点多余,但它有效。

最新更新