我在Symfony2.3项目中进行表单测试,我得到了没有选项的错误。 我不明白为什么会这样。
AdminMessageBundle\Tests\Form\ServiceAlertTypeTest::testSubmitValidData
with data set #0
(array(1, 'magic', '#2A8BCC', 'www', '23/12/14', 'text', 'all'))
Symfony\Component\OptionsResolver\Exception\InvalidOptionsException:
The options "attr", "class", "label", "multiple", "property", "query_builder",
"required" do not exist.
Known options are: ""
这是我的表单生成器
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormError;
use Doctrine\ORM\EntityRepository;
use MessageBundle\Utility\AlertForm;
/**
* Class ServiceAlertType
* @package Acme\MessageBundle\Form
*/
class ServiceAlertType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
if (!isset($options['attr']['date_format_js'])) {
$options['attr']['date_format'] = 'd/m/Y';
}
if (!isset($options['attr']['date_format_js'])) {
$options['attr']['date_format_js'] = 'd/m/yyyy';
}
if (!isset($options['attr']['scope'])) {
$options['attr']['scope'] = 'Empty Scope Error';
}
$builder
->add('icon', 'choice', array(
'choices' => AlertForm::getIcons(),
'required' => true,
'attr' => array(
'class' => 'icon-select'
),
))
->add('color', 'choice', array(
'choices' => AlertForm::getColors(true),
'label' => 'Choice color',
'required' => true,
'attr' => array(
'class' => 'hidden'
)
))
->add('text', 'textarea', array(
'required' => true
))
->add('link', 'url', array(
'required' => true
))
->add('date', 'text', array(
'required' => true,
'attr' => array(
'class' => 'date-picker',
'data-date-format' => $options['attr']['date_format_js'],
'value' => date($options['attr']['date_format']),
'input_group' => array(
'prepend' => '<i class="icon-calendar"></i>',
'size' => 'small'
),
'widget_col' => 2,
)
))
->add('all_users', 'checkbox', array(
'required' => false
))
->add(
'scope',
'entity',
array(
'class' => 'UserBundle:User',
'query_builder' => function (EntityRepository $er) use ($options) {
return $er->createQueryBuilder('u')->orderBy('u.username', 'ASC');
},
'property' => 'username',
'multiple' => true,
'label' => 'Users',
'required' => false,
'attr' => array(
'class' => 'chzn-select',
)
)
)
;
$error = $options['attr']['scope'];
$builder->addEventListener(FormEvents::POST_SUBMIT, function ($event) use ($error) {
$data = $event->getData();
$form = $event->getForm();
if (null === $data) {
return;
}
if (empty($data['all_users']) && !count($data['scope'])) {
$form->get('scope')->addError(new FormError($error));
return;
}
});
}
/**
* @return string
*/
public function getName()
{
return 'editorial_alert';
}
}
这是我的测试类
use MessageBundle\Entity\ServiceAlert;
use MessageBundle\Form\ServiceAlertType;
use Symfony\Component\Form\PreloadedExtension;
use Symfony\Component\Form\Test\TypeTestCase;
use Symfony\Component\Form\Forms;
class ServiceAlertTypeTest extends TypeTestCase
{
protected function setUp()
{
parent::setUp();
$this->factory = Forms::createFormFactoryBuilder()
->addExtensions($this->getExtensions())
->getFormFactory();
}
protected function getExtensions()
{
$mockEntityType = $this->getMockBuilder('Symfony\Bridge\Doctrine\Form\Type\EntityType')
->disableOriginalConstructor()
->getMock();
$mockEntityType->expects($this->any())->method('getName')
->will($this->returnValue('entity'));
return array(new PreloadedExtension(array(
$mockEntityType->getName() => $mockEntityType,
), array()));
}
public function getValidFormData()
{
$data = array(
array(
'data' => array(
'id'=> 1,
'icon' => 'magic',
'color' => '#2A8BCC',
'link' => 'www.example.com',
'date' => '23/12/14',
'text' => 'text',
'all_users' => 'all'
)
)
);
return $data;
}
/**
* @dataProvider getValidFormData
*/
public function testSubmitValidData($data)
{
$type = new ServiceAlertType(new TestChildType());
$form = $this->factory->create($type);
// submit the data to the form directly
$form->submit($data);
$this->assertTrue($form->isSynchronized());
}
}
我做错了什么?
感谢
答案 0 :(得分:0)
您需要在Form类
中实现setDefaultOptions
方法