ant*_*kas 11 forms validation symfony
我已经创建了需要数据转换器的表单,但是让我自己陷入单一问题:我通过爆炸字符串转换数据(字符串应该被分解为3个部分),一切正常,如果我提供正确的格式字符串,但否则会在数据内部引发错误变换器,因为如果提供了错误的字符串格式,则不会发生转换(这是预期的行为).
所以问题是有没有办法在数据转换之前验证表单字段的正确字符串?我知道默认情况下,数据转换发生在验证之前,但也许有办法以其他方式做到这一点?
我找到了一个可能在这个线程上工作的解决方案:组合约束和数据转换器,但它看起来像粗略的解决方案,除了我需要翻译验证消息,我真的想使用symfony表单的默认翻译方法(不使用翻译服务)
我想,也有来自symfony IRC(Iltar)的人建议通过使用事件来做,但我不知道如何去做 - 如何动态地将数据转换器附加到表单字段?或者可能还有其他方式?
小智 6
这可能为时已晚,但我最终设法做到了.也许它会帮助你.
这是我的FormType:
class PersonType extends AbstractType{
public function buildForm(FormBuilderInterface $builder, array $options){
$builder->add('mother', 'personSelector', array('personEntity' => $options['personEntity']));
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的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].'.'));
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的变压器:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
也许您可以将表单的实例传递给变压器。如果字符串无法正确解析,只需向表单添加验证错误,如下所示:
<?php
// src/Acme/MyBundle/Form/DataTransformer/StringTransformer.php
namespace Acme\MyBundle\Form\DataTransformer;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Doctrine\Common\Persistence\ObjectManager;
use Acme\MyBundle\Entity\MyEntity;
use Acme\MyBundle\Entity\AnotherEntity;
use Acme\MyBundle\Type\MyEntityType;
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;
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2673 次 |
最近记录: |