如何为每个 child 全局设置选项?
How to globally set option for each child?
我有 class 表格和一些 child:
class ReservationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('from', DateType::class, ["label" => "reservation.from?", "widget" => "single_text", "error_bubbling" => true]);
$builder->add('to', DateType::class, ["label" => "reservation.to?", "widget" => "single_text", "error_bubbling" => true]);
$builder->add("name", TextType::class, ["label" => "reservation.name", "error_bubbling" => true]);
$builder->add("surname", TextType::class, ["label" => "reservation.surname", "error_bubbling" => true]);
$builder->add('book', SubmitType::class, ["label" => "reservation.booking"]);
}
}
我分别为每个 child 设置了 error_bubling
选项。它工作正常。
我很确定有更好的方法。
您可以创建 Form Extension,但它会影响 所有 表格
namespace App\Form\Extension;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class ErrorBublingExtension extends AbstractTypeExtension
{
public static function getExtendedTypes(): iterable
{
return [DateType::class, TextType::class];
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'error_bubbling' => true,
]);
}
}
我有 class 表格和一些 child:
class ReservationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('from', DateType::class, ["label" => "reservation.from?", "widget" => "single_text", "error_bubbling" => true]);
$builder->add('to', DateType::class, ["label" => "reservation.to?", "widget" => "single_text", "error_bubbling" => true]);
$builder->add("name", TextType::class, ["label" => "reservation.name", "error_bubbling" => true]);
$builder->add("surname", TextType::class, ["label" => "reservation.surname", "error_bubbling" => true]);
$builder->add('book', SubmitType::class, ["label" => "reservation.booking"]);
}
}
我分别为每个 child 设置了 error_bubling
选项。它工作正常。
我很确定有更好的方法。
您可以创建 Form Extension,但它会影响 所有 表格
namespace App\Form\Extension;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class ErrorBublingExtension extends AbstractTypeExtension
{
public static function getExtendedTypes(): iterable
{
return [DateType::class, TextType::class];
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'error_bubbling' => true,
]);
}
}