douzhang1115
2012-10-31 18:33
浏览 28
已采纳

Symfony2中的CollectionType验证

I spent a lot of time to find a solution to this problem but I don't find the correct solution

I have a User entity that can contain multiple entity Address. The validation of the User entity should function like this:

  • User must have at least two entities Address
  • User must have a maximum of three entities Address
  • Address has its own validation constraints

Both entities have their formType and Address is a CollectionType in User formType.

Currently, I create in my User controller, 3 Address that appear correctly in the view. I use 'Assert\Count' with 'min=2' to validate the form in the entities. In my controller, I filter the query to remove the Address which have not been completed, then I validate the form. This approach is not correct. I am unable to display the 3 addresses if the form is not valid, I have only the addresses that have been completed. In addition, errors are not available in the fields when I made ​​the view with Twig

Ideally logic would be as follows: the Address are validated and only the valid Address are taken into account by User. User checks its validation constraints are respected then the User entity is stored in the database. Simple, right ? (... well that's what I thought)

Do you have any ideas or blog posts that cover my problem?

EDIT (to answer question in comment)

In the User entity

<?php
//...
* @Assert\Count(
*      min=2,
*      minMessage="user_form_not_enough_addresses",
*      max=3,
*      maxMessage="user_form_too_much_addresses"
* )
*/
private $addresses;
//...

In the User controller (when I filter the request)

//...

// Removing not completed addresses from request
$this->_filterRequest($request);

$userType = new UserType();
$user = new User();

$userForm = $request->get($userType->getName());
foreach ($userForm['addresses'] as $address) {
    $user->getAddresses()->add(new Address());
}

$form = $this->createForm($userType, $user);
$form->bind($request);

图片转代码服务由CSDN问答提供 功能建议

我花了很多时间找到这个问题的解决方案,但我找不到正确的解决方案

我有一个可以包含多个实体地址的用户实体。 User实体的验证应该如下所示:

  • 用户必须至少有两个实体地址
  • 用户必须具有最多 三个实体地址
  • 地址有自己的验证约束

    两个实体的formType和Address都是User formType中的CollectionType。 / p>

    目前,我在用户控制器中创建了3个地址,它在视图中正确显示。 我使用'Assert \ Count'和'min = 2'来验证实体中的表单。 在我的控制器中,我过滤查询以删除尚未完成的地址,然后我验证表单。 这种方法不正确。 如果表单无效,我无法显示3个地址,我只有已完成的地址。 此外,当我使用Twig进行视图时,字段中的错误不可用

    理想情况下,逻辑将如下:验证地址并仅考虑有效地址 用户。 用户检查其验证约束是否受到尊重,然后User实体存储在数据库中。 简单吧? (......这就是我的想法)

    您有任何想法或博客文章可以解决我的问题吗?

    编辑(回答问题) 在评论中)

    在用户实体

     &lt;?php 
     // ... 
     * @Assert \ Count  (
     * min = 2,
     * minMessage =“user_form_not_enough_addresses”,
     * max = 3,
     * maxMessage =“user_form_too_much_addresses”
     *)
     * / 
     nprivate $ addresses; 
     //。  .. 
       
     
     

    在用户控制器中(当我过滤请求时)

      // ... \  n 
     //从请求中删除未完成的地址
     $ this-&gt; _filterRequest($ request); 
     
     $ userType = new UserType(); 
     $ user = new User(); 
     
      $ userForm = $ request-&gt; get($ userType-&gt; getName()); 
    foreach($ userForm ['addresses'] as $ address){
     $ user-&gt; getAddresses() - &gt; add(  new Address()); 
    } 
     
     $ form = $ this-&gt; createForm($ userType,$ user); 
     $ form-&gt; bind($ request); 
      <  / pre> 
     
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dro44817 2012-11-08 16:25
    已采纳

    After some research the only acceptable solution was to use Symfony events.

    We must distinguish between two parts: validation and data recording. In both parts, you must validate addresses using the validator and do something based on errors that are found.

    In the case of validation, you must copy the file validation ValidationListener.php then use the services to match your file. Then simply customize the validation mechanism. The objective is to only display addresses that are not valid but that are required or not required but with some fields that are filled, just search the different cases and use ViolationMapperInterface::mapViolation() when the error should be displayed.

    In the case of data recording, just filter invalid addresses to be sure they are not taken into consideration by Doctrine (which would cause an SQL error otherwise)

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题