Ich benutze einen FormFilter für eine Klasse meines Modells, und es ist sehr nützlich für mich. Aber ich brauche ein Feature, das nicht zu existieren scheint.Symfony 1.4 FormFilter: Wie fügt man "ist nicht leer" an?
Ich verwende bereits die "with_empty" -Option, um ein "ist leer" -Kontrollkästchen neben einem Feld hinzuzufügen. Es filtert die Objekte so, dass nur diejenigen angezeigt werden, die in diesem Feld einen NULL-Wert haben. Aber ich muss das Gegenteil tun. Ich möchte ein "ist nicht leer" -Kontrollkästchen hinzufügen, um die Objekte anzuzeigen, die einen NOT NULL-Wert in diesem Feld haben.
Also habe ich diese Widget-Klasse erstellt die zusätzliche Option anzuzeigen:
<?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]'))) : '',
));
}
}
Aber jetzt, mein Problem ist, dass ich den „is_not_empty“ Wert in meinen Formularen manuell zu handhaben ...
Wie würden Sie das besser umsetzen?
Danke!
PS: Ich benutze Lehre
Oh, ich habe vergessen zu sagen, dass ich Lehre verwende! Es tut uns leid :) – Gregoire