My
我的
if ($editForm->isValid()) {
keeps returning false but I get no error messages.
一直返回false,但我没有收到错误消息。
I tried all suggested techniques in below thread, I for example do
我尝试了下面的所有建议技术,例如我做的。
var_dump( $editForm->getErrorsAsString() );
die;
and get
并获得
string(266) "name: No errors syncSchema: 1: No errors 12: No errors 37: No errors 38: No errors 49: No errors 14: No errors 39: No errors 76: No errors 152: No errors "
Symfony2 : How to get form validation errors after binding the request to the form
Symfony2:如何在将请求绑定到表单后得到表单验证错误。
anything else that could cause this behavior?
还有什么能引起这种行为的吗?
*** UPDATE (code included) *****
***更新(包含代码)*****。
Controller
控制器
public function updateAction(Request $request, $id)
{
$currentCompany = $this->container->get('security.context')->getToken()->getUser()->getCompany();
$folderHelper = $this->get('biztv.helper.folderHelper');
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('BizTVMediaManagementBundle:Video')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Video entity.');
}
if ($entity->getCompany() != $currentCompany) {
throw $this->createNotFoundException('Unable to find Folder entity in THIS company.');
}
$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createEditForm($entity);
$folders = $folderHelper->getFolders();
$editForm->handleRequest($request);
if ($editForm->isValid()) {
$entity->setCompany($currentCompany);
//Get and check the folder chosen as parent
$entity->setFolder( $folderHelper->checkFolderId($request->request->get('folder')) ); //will cause die() if folder doesn't belong to this company
//Check which folder to go back to later on
$goToFolder = $folderHelper->goToFolder($entity);
$helper = $this->get('biztv.helper.globalHelper');
$helper->log('success', 'Videon <strong>'. $entity->getName() .'</strong> har uppdaterats.');
$em->flush();
return $this->redirect($this->generateUrl('video'));
}
/**
* Creates a form to edit a Video entity.
*
* @param Video $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createEditForm(Video $entity)
{
$currentCompanyId = $this->container->get('security.context')->getToken()->getUser()->getCompany()->getId();
$form = $this->createForm(new VideoTypeEdit($currentCompanyId), $entity, array(
'action' => $this->generateUrl('video_update', array('id' => $entity->getId())),
'method' => 'PUT',
));
return $form;
}
FormType
FormType
namespace BizTV\MediaManagementBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityRepository;
class VideoTypeEdit extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
function __construct($company)
{
$this->company = $company;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$company = $this->company;
$builder
->add('name')
;
$builder
->add('syncSchema', 'entity', array(
'label' => ' ',
'multiple' => true, // Multiple selection allowed
'expanded' => true, // Render as checkboxes
'property' => 'select_label',
'class' => 'BizTV\ContainerManagementBundle\Entity\Container',
'query_builder' => function(\Doctrine\ORM\EntityRepository $er) use ($company) {
$qb = $er->createQueryBuilder('a');
$qb->innerJoin('a.containerType', 'ct');
$qb->where('a.containerType IN (:containers)', 'a.company = :company');
$qb->setParameters( array('containers' => array(1,2,3,4), 'company' => $company) );
$qb->orderBy('ct.id', 'ASC');
return $qb;
}
));
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'BizTV\MediaManagementBundle\Entity\Video'
));
}
/**
* @return string
*/
public function getName()
{
return 'biztv_mediamanagementbundle_videoedit';
}
}
View
视图
{% extends '::base.html.twig' %}
{% block contents -%}
<h1>Video edit</h1>
<div class="userAction_wrapper" style="width:800px;">
{{ form_errors(edit_form.name) }}
{{ form_errors(edit_form.syncSchema) }}
{{ form_errors(edit_form) }}
<form action="{{ path('video_update', { 'id': entity.id }) }}" method="post" {{ form_enctype(edit_form) }} novalidate>
{{ form_widget(edit_form) }}
<label for="folder">Mapp</label>
<select name='folder'>
<option value='none'>Ingen mapp</option>
{% include 'BizTVMediaManagementBundle:Image:options.html.twig' with {'folders': folders, 'level': 0 } %}
</select>
<div>
<table>
<tr>
<td>Längd</td>
<td>{{ entity.duration }}s</td>
</tr>
<tr>
<td>Bredd</td>
<td>{{ entity.width }}px</td>
</tr>
<tr>
<td>Höjd</td>
<td>{{ entity.height }}px</td>
</tr>
<tr>
<td>Codec</td>
<td>{{ entity.codec }}</td>
</tr>
<tr>
<td>Filstorlek</td>
<td>{{ entity.filesize }}bytes</td>
</tr>
</table>
</div>
Välj vilka ställen videofilen skall kopieras till
{% render(controller('BizTVContentManagementBundle:Default:index', {'userEdit': true})) %}
<div>
<button type="submit">Spara</button>
</div>
</form>
<ul class="record_actions">
<li>
<a href="{{ path('video') }}">
Back to the list
</a>
</li>
<li>{{ form(delete_form) }}</li>
</ul>
</div>
<script type="text/javascript">
$(function() {
$('#biztv_mediamanagementbundle_videoedit_syncSchema').children().each(function() {
fromId = $(this).attr('id');
if (typeof fromId != 'undefined') {
Id = fromId.replace("biztv_mediamanagementbundle_videoedit_syncSchema_","");
toId = 'container_'+Id;
$('#'+toId).append( ' ' );
$('#'+toId).append( $('#'+fromId) );
}
});
$('#biztv_mediamanagementbundle_videoedit_syncSchema').remove();
});
</script>
{% endblock %}
3 个解决方案
#1
3
Apparently, when building the form the symfony2.3 way, you need to do this in twig,
显然,当构建形式为symfony2.3时,你需要在twig中做这个,
{{ form_start(form) }}
{{ form_errors(form) }}
{{ form_row(form.task) }}
{{ form_row(form.dueDate) }}
{{ form_end(form) }}
Although, for forms that are built the old fashioned way, you can still TWIG them the old fashioned way.
虽然,以传统的方式建造的形式,你仍然可以用古老的方式。
#2
1
Assuming you use Symfony 2.3, I think that you miss form_end()
in your template (iirc it was called form_rest()
prior 2.3). See the current docmentation. So instead of simply writing </form>
form_end(edit_form)
should make your form valid.
假设您使用Symfony 2.3,我认为您在模板中忽略了form_end() (iirc,它被称为form_rest(),之前是2.3)。看到当前docmentation。因此,与其简单地编写 form_end(edit_form),不如让表单有效。
#3
1
think about it, you're validating everything shown, no errors by what's shown, so there's something hidden to be validated :)
考虑一下,你验证了所显示的一切,没有显示错误,所以有一些隐藏的东西需要验证:)
{{ form_widget(form._token) }} ? csrf check?
{ { form_widget(form._token)} } ?csrf支票吗?
btw form_rest != form_end
顺便说一句form_rest ! = form_end
#1
3
Apparently, when building the form the symfony2.3 way, you need to do this in twig,
显然,当构建形式为symfony2.3时,你需要在twig中做这个,
{{ form_start(form) }}
{{ form_errors(form) }}
{{ form_row(form.task) }}
{{ form_row(form.dueDate) }}
{{ form_end(form) }}
Although, for forms that are built the old fashioned way, you can still TWIG them the old fashioned way.
虽然,以传统的方式建造的形式,你仍然可以用古老的方式。
#2
1
Assuming you use Symfony 2.3, I think that you miss form_end()
in your template (iirc it was called form_rest()
prior 2.3). See the current docmentation. So instead of simply writing </form>
form_end(edit_form)
should make your form valid.
假设您使用Symfony 2.3,我认为您在模板中忽略了form_end() (iirc,它被称为form_rest(),之前是2.3)。看到当前docmentation。因此,与其简单地编写 form_end(edit_form),不如让表单有效。
#3
1
think about it, you're validating everything shown, no errors by what's shown, so there's something hidden to be validated :)
考虑一下,你验证了所显示的一切,没有显示错误,所以有一些隐藏的东西需要验证:)
{{ form_widget(form._token) }} ? csrf check?
{ { form_widget(form._token)} } ?csrf支票吗?
btw form_rest != form_end
顺便说一句form_rest ! = form_end