Skip to content

Instantly share code, notes, and snippets.

@calvin
Created January 11, 2014 06:54
Show Gist options
  • Save calvin/8367902 to your computer and use it in GitHub Desktop.
Save calvin/8367902 to your computer and use it in GitHub Desktop.
<?php
class sfWidgetFormInputColor extends sfWidgetFormInput
{
protected function configure($options = array(), $attributes = array())
{
parent::configure($options, $attributes);
$this->setOption('type', 'color');
$this->setAttribute('style', 'width: 30px');
}
public function render($name, $value = null, $attributes = array(), $errors = array())
{
if ($value && $value[0] != '#')
$value = '#' . $value;
return parent::render($name, $value, $attributes, $errors);
}
}
<?php
class sfValidatorColor extends sfValidatorRegex
{
const REGEX_PATTERN = '/^#?[a-f0-9]{3}|[a-f0-9]{6}$/i';
/**
* @see sfValidatorRegex
*/
protected function configure($options = array(), $messages = array())
{
parent::configure($options, $messages);
$this->setOption('pattern', self::REGEX_PATTERN);
}
protected function doClean($value)
{
$clean = parent::doClean($value);
if ($clean[0] == '#')
$clean = substr($clean, 1);
if (strlen($clean) == 3)
{
$tmp = $clean;
$clean = $tmp[0] . $tmp[0] . $tmp[1] . $tmp[1] . $tmp[2] . $tmp[2];
}
return strtolower($clean);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment