I use a FormFilter on a class of my model, and it's very useful to me. But I need a feature that does not seem to exist.
I already use the "with_empty" option to add an "is empty" checkbox next to a field. It filters the objects to display only the ones that have a NULL value in this field. But I need to do the opposite. I want to add an "is not empty" checkbox to display the objects that have a NOT NULL value in this field.
So I've created this widget class to display the additional checkbox:
<?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%' 开发者_如何学C => $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]'))) : '',
));
}
}
But now, my problem is that I have to handle the "is_not_empty" value in my forms manually...
How would you implement that in a better way?
Thanks!
PS: I use Doctrine
Please see sfFormFilterDoctrine::doBuildQuery method.
In brief you need to implement add%sColumnQuery.
If this is a common requirement among your filters, i would recommend reimplementing the add*Criteria on sfFormFilterPropel. This class implements the is_empty option so you should add here your is_not_empty option.
But if what you need its a localized solution, the best way to do this is implement you own
function addYourColumnNameCriteria(Criteria $criteria, $field, $value){...}
function in the filter that needs it.
精彩评论