[英]Is there a way to add a form field depending on a condition on Symfony?
Friends.朋友们。 This is the first question I ask here;
这是我在这里提出的第一个问题; so, any feedback will be welcome.
所以,任何反馈都将受到欢迎。
I'm new to Symfony;我是 Symfony 的新手; the version in which I'm working is 3.4 I already implemented three Type classes for my form and the structure is something like this: There is a Registry which has a header plus zero, one or more questions.
我正在使用的版本是 3.4 我已经为我的表单实现了三个 Type 类,结构是这样的:有一个注册表,它有一个 header 加上零,一个或多个问题。 Each question is a "RegistryElement" which consist on zero, one or more answers plus an "elementScore" and an optional "elementComment."
每个问题都是一个“RegistryElement”,由零个、一个或多个答案加上一个“elementScore”和一个可选的“elementComment”组成。
The Type classes buildForm methods look like this: Type 类的 buildForm 方法如下所示:
On RegistryType.php:在 RegistryType.php 上:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('registryHeaderId', RegistryHeaderType::class, array(
'label' => false,
))
->add('registryElements', CollectionType::class, array(
'label'=>false,
'entry_type' => RegistryElementType::class,
'entry_options' => [
'label' => false,
'tokens'=>$options['tokens'],
],
'allow_add' => true,
'by_reference' => false,
))
->add('save', SubmitType::class, array("attr"=>array(
"class"=>"form-submit btn-success",
"style"=>"margin:10px;"
)))
;
}
On RegistryElementType.php:在 RegistryElementType.php 上:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('registryElementAnswers', CollectionType::class, array(
'label'=>false,
'entry_type' => RegistryElementAnswerType::class,
'entry_options' => [
'label' => false,
],
'allow_add' => true,
'by_reference' => false,
))
->add('elementScore', CheckboxType::class, array(
"label"=>'type.registry_element_type.score',
'required' => false,
"translation_domain" => "FirstPieceBundle",
))
->add('elementComment', TextType::class, array(
"label"=>'type.registry_element_type.element_comment',
"required"=>false,
"attr"=>array(
"class"=>"form-name form-control",
),
"translation_domain" => "FirstPieceBundle",
))
;
}
And, on RegistryElementAnswerType.php:并且,在 RegistryElementAnswerType.php 上:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('answer', TextType::class, array(
"label"=>false,
"required"=>false,
"attr"=>array(
"class"=>"form-name form-control",
),
"translation_domain" => "FirstPieceBundle",
))
;
}
Now, what I would like to know is if there is a way to "add a different type of field" depending on a condition.现在,我想知道是否有一种方法可以根据条件“添加不同类型的字段”。 Something like this:
像这样的东西:
public function buildForm(FormBuilderInterface $builder, array $options)
{
if(condition){
$builder
->add('answer', TextType::class, array(
"label"=>false,
"required"=>false,
"attr"=>array(
"class"=>"form-name form-control",
),
"translation_domain" => "FirstPieceBundle",
))
;
}
else{
$builder
->add('answer', ChoiceType::class, array(
"label"=>false,
"required"=>false,
"attr"=>array(
"class"=>"form-name form-control",
),
"translation_domain" => "FirstPieceBundle",
))
;
}
}
I tried to pass some values through the $options array, but I really got stuck here.我试图通过 $options 数组传递一些值,但我真的被困在这里。 I'd really appreciate your knowledge and guidance.
我非常感谢您的知识和指导。
Greetings.问候。
What exactly is your condition you need to use there?您需要在那里使用的具体条件是什么?
If you want to pass data in the $options
array, you will need to add the option key as a required option in the configureOptions
method.如果要在
$options
数组中传递数据,则需要在configureOptions
方法中添加选项键作为必需选项。
Let's say you want to pass condition
in the $options
, your RegistryElementAnswerType
class should contain:假设您要在
$options
中传递condition
,您的RegistryElementAnswerType
class 应包含:
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setRequired('condition');
}
Now you are required to pass this option whenever you are referring to the RegistryElementAnswerType
.现在,每当您引用
RegistryElementAnswerType
时,您都需要传递此选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.