我正在使用集合制作自定义表单。到目前为止,一切都在运作,但我有一点烦人的事情,我无法解决这个问题:我只需要禁用某些字段,但无法解决这个问题。
我使用此数组来提供表单:
$aData = [
'group1' => [
'A6C6' => ['status' => false, 'claseid' => 6],
'A3C12' => ['status' => false, 'claseid' => 12 ]
],
'group2' => [
'A15C1' => ['status' => false, 'claseid' => 1],
'A25' => ['status' => false, 'claseid' => null ]
]
];
控制器中添加集合的代码:
$formu = $this->createFormBuilder($aData)
->add(
'submit',
SubmitType::class,
array(
'label' => 'Matricular',
'attr' => array(
'class' => 'btn btn-primary',
)
)
->getForm();
for($i = 1; $i <= $numCursos; $i++ ) {
$formu->add('group'.$i, CollectionType::class,
array(
'label' => 'Asignaturas del curso '.$i,
'entry_type' => BloqueMatriculaType::class
)
);
}
自定义表单类型:
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
//use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
class BloqueMatriculaType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
//$this->traitChoices = $options['trait_choices'];
$builder->add('claseid', HiddenType::class);
$builder->add('status', CheckboxType::class,
array(
'label' => false,
'required' => false,
'attr' => array(
'class' => 'form-control-static',
'style' => 'margin:10px 0 0 0'
)
)
);
}
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults(array(
'data_class' => null,
'trait_choices' => null
));
}
}
所以我需要禁用某些字段(例如,如果案例&#39; claseid&#39;为空)我想知道,是否有方法在aData中添加disabled属性数组 ...我一直在尝试使用options,entry_options,trait_choices,但是我们无法解决这个问题!