基本上,我希望在我的 DataTransformer 中进行表单验证或以任何方式验证不存在的字段.如果我手动调用 RegEx-Validator,我的代码会是什么样子?或者是否有另一种(更好的)方法来实现我正在寻找的东西? 解决方案 好的,我找到了答案.这远不是最好的方法.除了一个字符串addressstring,我刚刚从Address-Entity 中删除了所有字段.现在我可以将多个事件映射到一个位置并进行适当的 ajax 调用.但不幸的是,我无法再确定每个地址的城市、街道或邮政编码.这工作正常,但正如我所说的不是解决方案.我避免了真正的问题......但它有效.In my Symfony application, I have an event entity with an address. This address should be available for many events, so I added a ManyToOne-relation.The address entity contains 3 fields (street, zipcode and city), but I would like to have only 1 text input with typeahead for the address in my form EventType. So I created a DataTransformer to do so.Via an AJAX Request, I recieve the addresses and insert them. If a new address is entered I would like to create a new Database record. My problem now would be the validation for the new entered address. My DataTransformer looks like this:class StringToAddressTransformer implements DataTransformerInterface { /** * @var ObjectManager */ private $om; /** * @param ObjectManager $om */ public function __construct(ObjectManager $om){ $this->om = $om; } public function transform($addrobj){ if (!$addrobj) { return null; } return (string)$addrobj; } public function reverseTransform($address){ if (null === $address) { return ""; } preg_match('/^([A-Z][-A-Z ]+)\s+(\d+), (\d{5}) ([A-Z]+)$/i', $address, $res); $street = $res[1] . ' '. $res[2]; $zipcode = $res[3]; $city = $res[4]; $addrobj = $this->om->getRepository('FSchubert\SiyabongaBundle\Entity\Address')->findOneBy(array( 'street' => $street, 'zipcode' => $zipcode, 'city' => $city )); if(is_null($addrobj)){ $addrobj = new Address(); $addrobj->setStreet($street); $addrobj->setZipcode($zipcode); $addrobj->setCity($city); } return $addrobj; }}It all works out fine, but I have absolutely no validation for the entered address. I tried adding a constraint in the form class but since the DataTransformer is called on bindRequest() it appeares to me that the validation-constraint is ignored.Basically, I want form validation in my DataTransformer or any way to validate a non-existend field. If I call the RegEx-Validator manually what would my code look like? Or is there another (better) way to achive what I am looking for? 解决方案 Okay I found an answer. It is by far not the best way. I just removed all fields from the Address-Entity except for one string addressstring. Now I can map multiple events to one location and make propper ajax-calls. But unfortunaly I can nolonger determine the city, street or zipcode of each address. This works fine but is, as I stated not the solution. I avoided the real problem... but it works. 这篇关于在应用 DataTransformer 之前验证字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-24 23:19