2010-12-22 43 views
0

我在我的模型的類上使用FormFilter,它對我非常有用。但是我需要一個似乎不存在的功能。Symfony 1.4 FormFilter:如何添加「不爲空」複選框?

我已經使用「with_empty」選項在字段旁邊添加「is empty」複選框。它過濾對象以僅顯示在此字段中具有NULL值的對象。但我需要做相反的事情。我想添加一個「不是空的」複選框來顯示這個字段中具有NOT NULL值的對象。

所以我創造了這個小部件類顯示額外的複選框:

<?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

回答

1

請參閱sfFormFilterDoctrine :: doBuildQuery方法。

簡而言之,您需要實現添加%sColumnQuery。

0

如果這是你的過濾器之間的共同要求,我會建議重新實現附加*上sfFormFilterPropel標準。這個類實現了is_empty選項,所以你應該在這裏添加你的is_not_empty選項。

但是,如果你需要它的本地化解決方案,要做到這一點的最好辦法是實現你自己在需要它的過濾器

function addYourColumnNameCriteria(Criteria $criteria, $field, $value){...} 

功能。

+0

哦,我忘了說我使用教義!對不起:) – Gregoire 2010-12-22 14:42:13

相關問題