Created
January 11, 2014 06:54
-
-
Save calvin/8367902 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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