我有选择字段customer
类型entity
的表单,我希望在另一个地方选择一些客户(输入或标签,无论如何)更改数据,我使用烹饪书中的示例但不工作< / p>
我的表格
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('customer', 'entity', array(
'class' => Customer::class,
'property' => 'name',
'empty_value' => 'Choice Customer',
'query_builder' => function ($repository) {
/** @var CustomerRepository $repository */
return $repository->getAllQuery();
},
'required' => true
));
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) {
$form = $event->getForm();
$data = $event->getData();
/** @var Customer $customer */
$customer = $data->getCustomer(); //always have null, why ?
$positions = null === $customer ? '-' : 'its_work';
$form
->add('invoicing_address', 'text', [
'mapped' => false,
'data' => $positions
]);
}
);
/**
* @return string
*/
public function getName()
{
return 'out_bound_invoice';
}
我的js?
var $sport = $('#out_bound_invoice_customer');
// When sport gets selected ...
$sport.change(function() {
// ... retrieve the corresponding form.
var $form = $(this).closest('form');
// Simulate form data, but only include the selected sport value.
var data = {};
data[$sport.attr('name')] = $sport.val();
// Submit data via AJAX to the form's action path.
$.ajax({
url : $form.attr('action'),
type: $form.attr('method'),
data : data,
success: function(html) { // in html always have empty, like ''
// Replace current position field ...
$('#out_bound_invoice_address').replaceWith(
// ... with the returned one from the AJAX response.
$(html).find('#out_bound_invoice_address')
);
// Position field now displays the appropriate positions.
}
});
});
当我在数据中的js中更改客户时,有姓名表和客户名称密钥和数据ID,然后在请求中执行我看到此数据
在FormEvents::POST_SUBMIT
中使用事件表单$customer
时,我有客户实体
$builder->get('customer')->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event) use ($formModifier) {
$customer = $event->getForm()->getData();
$formModifier($event->getForm()->getParent(), $customer);
}
);
我希望了解为什么我需要PRE_SET_DATA, POST_SET_DATA
这个事件null
我的树枝
{{ form_start(form) }}
{{ form_errors(form) }}
{{ form_label(form.customer, 'customer')}}
{{ form_widget(form.customer, {'attr': {'class': 'select2'}}) }}
{{ form_label(form.invoicing_address)}}
{{ form_widget(form.invoicing_address) }}
{{ form_end(form) }}
为什么我在表单侦听器中的$customer
中都有null,以及如何更改另一个字段(标签或默认数据,无论在另一个字段中是什么)?