Skip to content

Instantly share code, notes, and snippets.

@77web
Created May 26, 2011 04:02
Show Gist options
  • Save 77web/992527 to your computer and use it in GitHub Desktop.
Save 77web/992527 to your computer and use it in GitHub Desktop.
integer filter widget and integer range validator for symfony1.4
<?php
/**
* sfValidatorIntegerRange
* @package symfony
* @subpackage validator
* @auther Hiromi Hishida<info@77-web.com>
*/
class sfValidatorIntegerRange extends sfValidatorBase
{
protected function configure($options = array(), $messages = array())
{
$this->addMessage('invalid', 'The min value must be less than max value.');
$this->addRequiredOption('from_validator');
$this->addRequiredOption('to_validator');
$this->addOption('from_field', 'from');
$this->addOption('to_field', 'to');
}
protected function doClean($value)
{
$fromField = $this->getOption('from_field');
$toField = $this->getOption('to_field');
$value[$fromField] = $this->getOption('from_validator')->clean(isset($value[$fromField]) ? $value[$fromField] : null);
$value[$toField] = $this->getOption('to_validator')->clean(isset($value[$toField]) ? $value[$toField] : null);
if($value[$fromField] && $value[$toField])
{
$validator = new sfValidatorSchemaCompare($fromField, sfValidatorSchemaCompare::LESS_THAN_EQUAL, $toField, array('throw_global_error'=>true), array('invalid'=>$this->getMessage('invalid')));
$validator->clean($value);
}
return $value;
}
}
<?php
/**
* sfWidgetFormFilterInteger
* @package symfony
* @subpackage widget
* @auther Hiromi Hishida<info@77-web.com>
*/
class sfWidgetFormFilterInteger extends sfWidgetForm
{
public function configure($options = array(), $attributes = array())
{
$this->addOption('with_empty', true);
$this->addOption('empty_label', 'is empty');
$this->addOption('template', 'from %from_input% to %to_input%<br />%empty_checkbox% %empty_label%');
}
public function render($name, $value = null, $attributes = array(), $errors = array())
{
$values = array_merge(array('from'=>'', 'to'=>'', 'is_empty'=>false), is_array($value) ? $value : array());
$from_input = $this->renderTag('input', array_merge(array('type'=>'text', 'size'=>5, 'id'=>$this->generateId($name), 'name'=>$name.'[from]', 'value'=>$values['from']), $attributes));
$to_input = $this->renderTag('input', array_merge(array('type'=>'text', 'size'=>5, 'id'=>$this->generateId($name), 'name'=>$name.'[to]', 'value'=>$values['to']), $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]'))) : '';
return strtr($this->getOption('template'), array('%from_input%'=>$from_input, '%to_input%'=>$to_input, '%empty_checkbox%'=>$empty_checkbox, '%empty_label%'=>$empty_label));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment