Symfony 1.4 FormFilter:如何添加“不空””复选框?

我在我的模型类中使用FormFilter,这对我非常有用。但我需要一个似乎不存在的功能。 我已经使用“with_empty”选项在字段旁边添加“is empty”复选框。它过滤对象以仅显示此字段中具有NULL值的对象。但我需要做相反的事情。我想添加一个“is not empty”复选框来显示该字段中具有NOT NULL值的对象。 所以我创建了这个widget类来显示附加的复选框:
<?php
/**
 * sfWidgetFormFilterInputExtended represents an HTML input tag used for filtering text.
 * It adds the possibility to insert a "is not empty" checkbox that does the opposite
 * of "is empty" checkbox
 */
class sfWidgetFormFilterInputExtended extends sfWidgetFormFilterInput
{
  protected function configure($options = array(), $attributes = array())
  {
    parent::configure($options, $attributes);
    $this->addOption('with_not_empty', true);
    $this->addOption('not_empty_label', 'is not empty');
    $this->addOption('template', '%input%<br />%empty_checkbox% %empty_label%<br />%not_empty_checkbox% %not_empty_label%');
  }

  /**
   * @param  string $name        The element name
   * @param  string $value       The value displayed in this widget
   * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
   * @param  array  $errors      An array of errors for the field
   *
   * @return string An HTML tag string
   *
   * @see sfWidgetForm
   */
  public function render($name, $value = null, $attributes = array(), $errors = array())
  {
    $values = array_merge(
      array(
        'text' => '',
        'is_empty' => false,
        'is_not_empty' => false,
      ),
      is_array($value) ? $value : array()
    );

    return strtr($this->getOption('template'), array(
      '%input%'              => $this->renderTag('input', array_merge(array('type' => 'text', 'id' => $this->generateId($name), 'name' => $name.'[text]', 'value' => $values['text']), $attributes)),
      '%empty_checkbox%'     => $this->getOption('with_empty') ? $this->renderTag('input', array('type' => 'checkbox', 'name' => $name.'[is_empty]', 'checked' => $values['is_empty'] ? 'checked' : '')) : '',
      '%empty_label%'        => $this->getOption('with_empty') ? $this->renderContentTag('label', $this->translate($this->getOption('empty_label')), array('for' => $this->generateId($name.'[is_empty]'))) : '',
      '%not_empty_checkbox%' => $this->getOption('with_not_empty') ? $this->renderTag('input', array('type' => 'checkbox', 'name' => $name.'[is_not_empty]', 'checked' => $values['is_not_empty'] ? 'checked' : '')) : '',
      '%not_empty_label%'    => $this->getOption('with_not_empty') ? $this->renderContentTag('label', $this->translate($this->getOption('not_empty_label')), array('for' => $this->generateId($name.'[is_not_empty]'))) : '',
    ));
  }
}
但现在,我的问题是我必须手动处理表单中的“is_not_empty”值... 你会如何以更好的方式实现它? 谢谢! PS:我使用Doctrine     
已邀请:
请参阅sfFormFilterDoctrine :: doBuildQuery方法。 简而言之,您需要实现add%sColumnQuery。     
如果这是您的过滤器中的常见要求,我建议重新实现sfFormFilterPropel上的add * Criteria。此类实现了is_empty选项,因此您应在此处添加is_not_empty选项。 但是,如果你需要它的本地化解决方案,最好的方法是实现你自己的
function addYourColumnNameCriteria(Criteria $criteria, $field, $value){...}
在需要它的过滤器中运行。     

要回复问题请先登录注册