Php Symfony表单侦听器
我有一个带有选择字段Php Symfony表单侦听器,php,symfony,Php,Symfony,我有一个带有选择字段customertypeentity的表单,我想在另一个地方选择某个客户(输入或标签,无论什么)时更改数据,我使用了cook book中的示例,但不起作用 我的表格 /** * @param FormBuilderInterface $builder * @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options) { $
customer
typeentity
的表单,我想在另一个地方选择某个客户(输入或标签,无论什么)时更改数据,我使用了cook book中的示例,但不起作用
我的表格
/**
* @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中的data have name表单、customer name key和data id中更改customer时,然后在请求中更改customer时,我看到了这些数据
在$customer
中使用事件表单FormEvents::POST_SUBMIT
时,我拥有客户实体
$builder->get('customer')->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event) use ($formModifier) {
$customer = $event->getForm()->getData();
$formModifier($event->getForm()->getParent(), $customer);
}
);
我想了解为什么需要PRE\u SET\u数据,POST\u SET\u数据
,我有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
中总是有空值,以及如何更改另一个字段(标签或默认数据,无论是在另一个字段中)?FormEvents::PRE_SET_data事件调度在表单::setData()的开头。所以你现在不能有一些数据。POST_SET_DATA呢?$event->getData()已经提供了客户实体,我认为您不应该再调用getCustomer()。或者您有方法$customer->getCustomer()??POST_SET_DATA
have null in customer toothis forinvoice
实体$DATA={AppBundle\entity\OutboundInvoice}
变量中的该实体DATA
并具有函数getCustomer
,但您注入了一个客户类:“class'=>customer::class,