Created
November 2, 2010 18:01
-
-
Save nervetattoo/660020 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 | |
Validator::add('array', function(&$value, $format = null, array $options = array()) { | |
$options += array( | |
'threshold' => 0, | |
'validator' => 'notEmpty' | |
); | |
if (is_array($value)) { | |
$failed = 0; | |
$valueCount = count($value); | |
foreach ($value as $item) { | |
if (is_string($item) && !Validator::rule($options['validator'], $item)) | |
$failed++; | |
} | |
/** | |
* Threshold meanings: | |
* 0 = Ever set value must validate | |
* +x = At least x values must validate | |
* -x = At max x values can fail | |
*/ | |
if ($options['threshold'] == 0) | |
return $failed == 0; | |
elseif ($options['threshold'] < 0) | |
return ($failed <= abs($options['threshold'])); | |
else | |
return (($valueCount - $failed) >= $options['threshold']); | |
} | |
elseif (is_string($value)) | |
return Validator::rule($options['validator'], $value); | |
return true; | |
}); | |
class Bar extends \lithium\data\Model | |
{ | |
public $validates = array( | |
'foo' => array( | |
array( | |
'array', | |
'validator' => 'notEmpty', | |
'message' => 'Must have foo', | |
'threshold' => 1 // Need at least on | |
), | |
), | |
); | |
} |
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 Bar extends \lithium\data\Model | |
{ | |
public $validates = array( | |
'foo' => array( | |
array( | |
'notEmpty', | |
'message' => 'Must have foo', | |
), | |
), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment