【问题标题】:Symfony get submitted values in Form classSymfony 在 Form 类中获取提交的值
【发布时间】:2011-08-24 15:26:06
【问题描述】:

我需要在模型之后生成的表单中使用 sfValidatorDoctrineChoice 的提交值。

我尝试了$this->getValue('country'),但它不起作用:

$query2 = Doctrine_Core::getTable('sate')->createQuery('s')
            ->select('s.id')
            ->where('s.idcountry = ?', $this->getValue('country'));

我怎样才能得到那个参数?

【问题讨论】:

    标签: php symfony1 symfony-1.4


    【解决方案1】:

    如果你喜欢 *Form,试试这个:

    $query2 = Doctrine_Core::getTable('sate')->createQuery('s')
                ->select('s.id')
                ->where('s.idcountry = ?', $this->getObject()->getCountry());
    

    否则,如果您进入动作类,则需要使用$this->form->getObject()->getCountry()

    【讨论】:

    • SELECT COUNT(*) AS num_results FROM sate WHERE idcountry = '' AND id = '2' 它仍然没有返回 idcountry 的值
    【解决方案2】:
    $somevar = sfContext::getInstance()->getRequest()->getParameter('register')
    $query2 = Doctrine_Core::getTable('sate')->createQuery('s')
            ->select('s.id')
            ->where('s.idcountry = ?', $somevar['country']);
    

    这个有效。

    【讨论】:

      【解决方案3】:

      记住,sfContext 不是免费的 :)

      测试一堆依赖于上下文的代码真的很难,因为你需要引导整个 symfony 上下文,从而失去了测试的隔离性。

      另外,请记住,您是在 Form 中访问整个 Request 对象,这很糟糕。

      尝试遵循 Fabio Cinerchia 的提示。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多