Php 使用STI进行Symfony2表单验证(单表继承)
我正在使用Symfony 2框架构建一个web应用程序,其中我有一个通知类,由OrderCloseNotification和OrderDelayNotification子类,使用单表继承,如条令2文档中所述,以服务稍微不同的用途(您可以通过类名猜到) 我需要以不同的方式验证表单提交,这会导致我为每个表单创建自定义类型和控制器。我将使用OrderDelayNotification,因为它是需要验证的通知类型。以下是我的设置: 超级班:Php 使用STI进行Symfony2表单验证(单表继承),php,ajax,symfony,symfony-forms,Php,Ajax,Symfony,Symfony Forms,我正在使用Symfony 2框架构建一个web应用程序,其中我有一个通知类,由OrderCloseNotification和OrderDelayNotification子类,使用单表继承,如条令2文档中所述,以服务稍微不同的用途(您可以通过类名猜到) 我需要以不同的方式验证表单提交,这会导致我为每个表单创建自定义类型和控制器。我将使用OrderDelayNotification,因为它是需要验证的通知类型。以下是我的设置: 超级班: # src/MyNamespace/MyBundle/Enti
# src/MyNamespace/MyBundle/Entity/Noticication.php
namespace MyNamespace\MyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
class Notification
{
# common attributes, getters and setters
}
子类:
# src/MyNamespace/MyBundle/Entity/OrderDelayNotification.php
namespace MyNamespace\MyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
class OrderDelayNotification extends Notification
{
private $message;
# getters and setters
}
子类控制器:
namespace MyNamespace\MyBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use MyNamespace\MyBundle\Entity\OrderDelayNotification;
use MyNamespace\MyBundle\Form\Type\OrderDelayNotificationType;
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)
这里发生的事情是:当我尝试通过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))
错误现在正确显示
$builder->add('message', null, array('error_bubbling' => true))