使用 STI(单表继承)进行 Symfony2 表单验证



我正在使用Symfony 2框架构建一个Web应用程序,其中我有一个Notification类,由OrderCloseNotification和OrderDelayNotification使用单表继承进行子类化,如Doctrine 2文档中所述,以用于稍微不同的目的(你可以通过类名猜到(。

我需要以不同的方式验证表单提交,这导致我为每个表单提交创建自定义类型和控制器。我将使用 OrderDelayNotification,因为它是需要验证的通知类型。这是我的设置:

超级舱:

# src/MyNamespace/MyBundle/Entity/Noticication.php
namespace MyNamespaceMyBundleEntity;
use DoctrineORMMapping as ORM;
class Notification
{
    # common attributes, getters and setters
}

亚纲:

# src/MyNamespace/MyBundle/Entity/OrderDelayNotification.php
namespace MyNamespaceMyBundleEntity;
use DoctrineORMMapping as ORM;
class OrderDelayNotification extends Notification
{
    private $message;
    # getters and setters
}

子类控制器:

namespace MyNamespaceMyBundleController;
use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentHttpFoundationResponse;
use MyNamespaceMyBundleEntityOrderDelayNotification;
use MyNamespaceMyBundleFormTypeOrderDelayNotificationType;

class OrderDelayNotificationController extends Controller
{    
    public function createAction() {
    $entity  = new OrderDelayNotification();
        $request = $this->getRequest();
        $form    = $this->createForm(new OrderDelayNotificationType(), $entity);
        $form->bindRequest($request);
        if ($form->isValid()) {
             //$em = $this->getDoctrine()->getEntityManager();
             //$em->persist($entity);
             //$em->flush();
        } else {
        }   
        // I'm rendering javascript that gets eval'ed on the client-side. At the moment, the js file is only displaying the errors for validation purposes  
        if ($request->isXmlHttpRequest()) {
            return $this->render('LfmCorporateDashboardBundle:Notification:new.js.twig', array('form' => $form->createView()));
        } else {
        return $this->redirect($this->generateUrl('orders_list'));
        }
    }
}

我的自定义表单类型

# src/MyNamespace/MyBundle/Form/Type/OrderDelayNotificationType.php
class OrderDelayNotificationType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('message')
                ->add('will_finish_at', 'date')
                ->add('order', 'order_selector'); //*1
    return $builder;
    }
    public function getName()
    {
        return 'orderDelayNotification';
     }
}

*1 : order_selector它是一种自定义类型,我与将数据转换器一起将订单映射到其主键中,以便在给定订单集的表视图中创建通知。

最后,我有一个validation.yml(我为每个配置使用YAML(

# src/MyNamespace/MyBundle/Resources/config.validation.yml
MyNamespaceMyBundleEntityOrderDelayNotification:
    properties:
        message:
            - NotBlank: ~

这里发生的事情是:当我尝试通过 AJAX 创建 OrderDelayNotification 时(没有尝试过 html 请求(,即使消息为空,订单也始终被认为是有效的。我也试图强加一个最小长度,但没有运气。我通读了symfony的文档,他们说默认情况下启用验证。还尝试将validation.yml上的属性名称更改为无效名称,Symfony抱怨它,这意味着文件已加载,但验证没有发生。

有人对此有任何指示吗?

编辑:ajax调用是这样进行的:

$('form[data-remote="true"]').submit(function(event){
    $.ajax({
        type:       $(this).attr('method'),
        url:            $(this).attr('action'),
        data:       $(this).serialize(),
        success:    function(response) {
            eval(response)
        }
    });
    event.preventDefault();
});

这会产生:

# src/MyNamespace/MyBundle/Resources/views/Notification/new.js.twig
alert("{{ form_errors(form) }}");

而且我可以看到Symfony的验证器服务(根据Symfony的文档,由我的AbstractType子类间接调用(没有抛出任何错误。

找出问题所在。表单实际上无效,但未显示错误。我必须为每个表单字段包含以下选项:

$builder->add('message', null, array('error_bubbling' => true))

错误现在可以正确显示。

最新更新