【问题标题】:Symfony - PHPunit Submit form with jquery-generation of values (for selects)Symfony - PHPunit 提交带有 jquery 生成值的表单(用于选择)
【发布时间】:2014-08-31 14:52:13
【问题描述】:

我对 symfony2 的 PHPUnit 功能测试有疑问。

我有一些带有select的表单,它们的值不一定是在formtype中设置的,而是由jquery生成的。

例如: 一个 FormType 包含:

class MyType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
    $builder
        ->add('zipCode',    'text')
        ->add('city',       'choice', array('choices' => array('' => ''), 'mapped' => false))
        ->add('way',        'choice', array('choices' => array('' => ''), 'mapped' => false));
    }
}

在我的视图中,用户设置了邮政编码,并通过 AJAX 请求设置了城市选项,然后设置了方式选项。

通常,我会这样获取/提交表单:

$form = $crawler->selectButton($submit)->form();
$form['nameOfZipCodeField'] = 'VALUE';
$form['nameOfCityField'] = 'VALUE';
$form['nameOfWayField'] = 'VALUE';
$crawler = $client->submit($form);

但是,Way 和 City 的值没有在 formType 中设置,所以 phpunit 会说:VALUE is not a possible value (possible value :)

这是合乎逻辑的......但我不知道该怎么做。 我尝试直接设置输入值(通过 Crawler 的 filter() 方法),但我无法模拟单击按钮(不是链接,是按钮)。

如果你有答案,我很乐意 :) 谢谢

【问题讨论】:

    标签: javascript php jquery forms symfony


    【解决方案1】:

    这样做:

    $form = $crawler->selectButton($submit)->form();
    $form['nameOfZipCodeField'] = 'VALUE';
    $form['nameOfCityField']->disableValidation();
    $form['nameOfCityField'] = 'VALUE';
    $form['nameOfWayField']->disableValidation();
    $form['nameOfWayField'] = 'VALUE';
    $crawler = $client->submit($form);
    

    默认情况下,选择字段(选择、单选)已激活内部验证,以防止您设置无效值。如果您希望能够设置无效值,可以在整个表单或特定字段上使用 disableValidation() 方法。

    阅读文档http://symfony.com/doc/current/components/dom_crawler.html#selecting-invalid-choice-values

    【讨论】:

    • 我认为对您的代码进行一些解释将对许多 SO 用户有用。
    猜你喜欢
    • 2017-01-14
    • 1970-01-01
    • 2018-04-17
    • 2018-02-01
    • 2011-07-22
    • 2011-01-13
    • 1970-01-01
    • 2017-01-30
    • 2012-10-31
    相关资源
    最近更新 更多