Tôi có hai trường lựa chọn, một trường tùy thuộc vào trường khác.Xác thực trường lựa chọn được điền động
Khi tạo biểu mẫu, trường tùy chọn có một mảng lựa chọn trống.
Sau đó, tôi điền vào trường này trong JavaScript yêu cầu một số dữ liệu từ một hành động.
Sự cố đến từ việc xác thực. Tất nhiên nó không vượt qua vì các giá trị đơn hoặc bội số không thể hợp lệ đối với giá trị rỗng. Để giải quyết rằng tôi đã tạo một trình nghe PRE_BIND
mà về cơ bản xóa, sau đó tạo lại trường lựa chọn với các giá trị đúng, nhưng nó vẫn không vượt qua xác thực.
$form->getErrors()
không trả lại gì ngoài số $form->getErrorsAsString()
trả về lỗi của tôi trên trường lựa chọn của tôi.
hình thức của tôi:
<?php
namespace Foo\BarBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class BarFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
// other fields
// This field is filled in ajax
$builder->add('stores', 'choice', array(
'label' => 'form.label.stores',
'translation_domain' => 'FooBarBundle',
'choices' => $options['storesList'],
'required' => false,
'multiple' => true,
'auto_initialize' => false,
'attr' => array(
'class' => 'chzn-select',
'placeholder' => 'form.placeholder.stores'
)));
$func = function (FormEvent $e) use ($options) {
$data = $e->getData();
$form = $e->getForm();
if ($form->has('stores')) {
$form->remove('stores');
}
$brand = isset($data['brand']) ? $data['brand'] : null;
if ($brand !== null) {
$choices = $options['miscRepo']->getStoresNameIndexedById($brand);
$choices = array_keys($choices);
$choices = array_map('strval', $choices);
} else {
$choices = array();
}
$form->add('stores', 'choice', array('choices' => $choices, 'multiple' => true, 'attr' => array('class' => 'chzn-select')));
};
$builder->addEventListener(FormEvents::PRE_SUBMIT, $func);
}
public function getName()
{
return 'bar_form_campaign';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setRequired(array(
'storesList',
'miscRepo',
));
}
}
Lỗi nào được trả về? Cẩn thận rằng các lựa chọn phải được chứa trong các * khóa * của mảng "lựa chọn", không phải trong các giá trị. –
Điều gì về việc sử dụng loại hình thức 'Entity'? http://symfony.com/doc/current/reference/forms/types/entity.html – gondo
Bạn có thể viết ajax được sử dụng để điền vào phần tử biểu mẫu "cửa hàng" không? Lỗi nào được trả lại? – jdharandas