我有这个formType
class ParameterType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('vehicleType', 'choice', [ 'required' => false, 'expanded' => true, 'choices' => [ 'turismo' => 'touring', 'corsa' => 'ride' ] ]) ->add('historicalParameterDatas', 'collection', array( 'type' => new HistoricalParameterType(), 'allow_add' => true, 'allow_delete' => true, 'options' => array( 'label' => false, ), )) ->add('save', SubmitType::class, array('label' => 'save')); }
其中,historicalParameterDatas为此Embedded Form
class HistoricalParameterType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('raceEdition', 'entity', array( 'class' => 'AppBundle:RaceEdition', 'choice_label' => 'year', 'label' => 'raceEdition', )) ->add('pilot', 'text', [ 'attr' => [ 'maxlength' => 50, ], ]) ->add('copilot', 'text', [ 'attr' => [ 'maxlength' => 50, ], ]) ->add('raceNumber', 'integer', [ ]) ->add('historicalAttachedFile', 'file', [ 'required' => false ]); }
所以我的表格必须保留一些关于一辆车的信息,关于一些raceEdition(在我的数据库中保存)。我需要做的是让这成为可能,但有一些限制。对于一辆车,用户只能选择一次raceEdition。因此,在下一个历史参数中(通过symfony最佳实践添加有关此案例的内容),在previuos字段中选择的raceEditions将无法选择。
<ul id='historical-parameter-list' data-prototype="{{ form_widget(form.historicalParameterDatas.vars.prototype)|e('html_attr') }}"> {% for historicalParameterData in form.historicalParameterDatas %} {{ form_errors(historicalParameterData) }} <li class='form-inline'> {{ form_row(historicalParameterData) }} </li> {% endfor %} </ul> function addNewExamForm($collectionHolder, $newLinkLi) { // Get the data-prototype explained earlier var prototype = $collectionHolder.data('prototype'); // get the new index var index = $collectionHolder.data('index'); // Replace '__name__' in the prototype's HTML to // instead be a number based on how many items we have var newForm = prototype.replace(/__name__/g, index); // increase the index with one for the next item $collectionHolder.data('index', index + 1); // Display the form in the page in an li, before the "Add a tag" link li var $newFormLi = $("<div class='form-group'></div>").append(newForm); $newFormLi = $("<li class='form-inline'></li>").append($newFormLi); $newLinkLi.before($newFormLi); $("#pt_historicalParameterDatas_" + index + "_quantity").val(1); addExamDeleteLink($newFormLi); }
我该怎么做?也许有一些JavaScript?我不知道怎么做。