Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Validating empty arrays #22

daschl opened this Issue · 0 comments

1 participant


Note: this ticket is ported over from and consolidates a bunch of tickets of the same topic.

The enhancement states some way of validating if an array is empty or not (which is handy vor validating selects, checkboxes and other stuff).

Here is a proposal for a recursive approach to test for empty arrays. It basically extends the current pattern recursively on arrays. Some test cases included. Feel free to talk about and/or contribute!

//'notEmpty'     => '/[^\s]+/m',
'notEmpty'     => function($value, $format = null, array $options = array()) {
    $pattern = '/[^\s]+/m';
    $arrayTest = function($arr) use ($pattern, &$arrayTest) {
        foreach($arr as $k => $v) {
            if(is_array($v)) {
                return $arrayTest($v, $arrayTest);
            } else {
                if(preg_match($pattern, $v)) {
                    return true;
        return false;
    return $arrayTest((array)$value);

public function testNotEmptyRule() {
    $this->assertTrue(Validator::isNotEmpty('fasdf '));
    $this->assertTrue(Validator::isNotEmpty('fooo' . chr(243) . 'blabla'));
    $this->assertFalse(Validator::isNotEmpty("\t "));

    $this->assertTrue(Validator::isNotEmpty(array('foo', 'bar')));
    $this->assertFalse(Validator::isNotEmpty(array("", "\t ")));
    $this->assertFalse(Validator::isNotEmpty(array(array(""), "\t ")));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.