gpt4 book ai didi

forms - 表单集合错误冒泡

转载 作者:行者123 更新时间:2023-12-01 09:52:22 24 4
gpt4 key购买 nike

您好,我在表单中收集文本字段时遇到问题。当其中一个字段出现错误时,这些错误会冒泡到父表单中,因此它们不会分配给字段,而是分配给父表单本身。它是以下代码中的“积分”集合。我试图将 error_bubbling 设置为 false,但没有效果。

    <?php
namespace JamaLvova\AdminBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use JamaLvova\AdminBundle\Form\Type\ExercisePointsFormType;

class StartContestFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{

$builder->add('startYear', 'hidden')
/*
some other form elements
*/
->add('points', 'collection', array(
'type' => 'text',
'allow_add' => true,
'label' => 'Body za jednotlivé úlohy:',
'error_bubbling' => false,
'options' => array(
'error_bubbling' => false,
'attr' => array("maxlength" => "4", "size" => "4")
)
));
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'JamaLvova\AdminBundle\Form\StartContestForm',
));
}

public function getName()
{
return 'startContestForm';
}
}

在 StartContestForm 我有这样写的 $points 属性:
     /**
* @Assert\Type(type="integer", message="Hodnota {{ value }} není celé číslo.")
* @Assert\Range(
* min = "0",
* max = "",
* minMessage = "Body nemohou být záporné",
* maxMessage = "Příliš mnoho bodů"
* )
*/
private $points;

在 twig 模板中,当我遍历 form.points 时,没有字段有错误,但 form.points 有错误。有谁知道问题可能出在哪里?或者我错过了什么?非常感谢:-)(Symfony v. 2.1.4)

编辑:似乎如果我使用表单集合 ('type' => new PointsFormType()) 而不是 'type' => 'text',它以某种方式按预期工作。这是否意味着我总是需要使用表单集合才能将错误分配给某个字段?

最佳答案

您可能需要添加 cascade_validation' => true

$builder->add('startYear', 'hidden')
/*
some other form elements
*/
->add('points', 'collection', array(
'type' => 'text',
'allow_add' => true,
'label' => 'Body za jednotlivé úlohy:',
'error_bubbling' => false,
'cascade_validation' => true,
'attr' => array("maxlength" => "4", "size" => "4")
));
}

关于forms - 表单集合错误冒泡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15621368/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com