Skip to content

Instantly share code, notes, and snippets.

@nervetattoo
Created November 2, 2010 18:01
Show Gist options
  • Save nervetattoo/660020 to your computer and use it in GitHub Desktop.
Save nervetattoo/660020 to your computer and use it in GitHub Desktop.
<?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
),
),
);
}
<?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