提交前验证表格 [英] Validation of a form before submission

查看:87
本文介绍了提交前验证表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Symfony(版本2.3和更高版本),我希望用户单击链接以转到现有实体的版本页面,并且所显示的表单已经过验证,并且每个错误都与其相关联.相应的领域,即我想要 提交表单之前要验证的表单.

Using Symfony, version 2.3 and more recent, I want the user to click on a link to go to the edition page of an already existing entity and that the form which is displayed to be already validated, with each error associated to its corresponding field, i.e. I want the form to be validated before the form is submitted.

我遵循了食谱的此项:

$form = $this->container->get('form.factory')->create(new MyEntityFormType, $myEntity, array('validation_groups' => 'my_validation_group'));
$form->submit($request->request->get($form->getName()));
if ($form->isValid()) {
    ...
}

但是该表单未填充实体数据:所有字段均为空.我尝试将$request->request->get($form->getName())替换为$myEntity,但它触发了异常:

But the form is not populated with the entity datas : all fields are empty. I tried to replace $request->request->get($form->getName()) with $myEntity, but it triggered an exception :

$ myEntity不能用作Symfony/Component/Form/Extension/Csrf/EventListener/CsrfValidationListener.php中的数组

$myEntity cannot be used as an array in Symfony/Component/Form/Extension/Csrf/EventListener/CsrfValidationListener.php

有人知道一种用正确格式的数据为Submit方法提供数据的方法,以便我可以实现自己的目标吗?注意:我不希望包含Javascript.

Does anyone know a method to feed the submit method with properly formatted datas so I can achieve my goal ? Note : I don't want Javascript to be involved.

推荐答案

代替:

$form->submit($request->request->get($form->getName()));

尝试:

$form->submit(array(), false);

这篇关于提交前验证表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆