Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8129445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:18:49+00:00 2026-06-06T08:18:49+00:00

i was just reading a great presentation about Quality Assurance for PHP Projects by

  • 0

i was just reading a great presentation about Quality Assurance for PHP Projects by Michelangelo van Dam aka DragonBe. In his presentation he uses Zend-Framework as an example, which i am familiar with on a basic level.

Since i want to improve myself, im starting with unit testing, though i feel the problem is on ZFs side.

The Problem:

With given code-artifacts whenever i assign a boolean value of false the Zend_Filter or Zend_Validator changes this into null. I can’t figure out why this is the case.

Please see update on bottom for newer code and test

First the Tests

public function goodData() {
  return array(
    array('DU-IT', 'Sample Description', true, false),
    array('Mercüß', 'bla"ß"trager', false, true),
    array('Mia123', '728 Tage im Jahr', false, false)
  );
}

/**
 * @dataProvider goodData()
 */
public function testModelAcceptsValidData($name, $description, $flag_active, $flag_deleted)
{
  $data = array(
    'id'            => 0,
    'name'          => $name,
    'description'   => $description,
    'flag_active'   => $flag_active,
    'flag_deleted'  => $flag_deleted
  );

  try {
    $this->_model->populate($data);
  } catch (Zend_Exception $e) {
    $this->fail('Unexpected Exception: '.$e->getMessage());
  }

  $this->assertSame($data, $this->_model->toArray());
}

The Model

public function __construct($props = null)
{
  // Set Filters
  $this->_filters = array(
    'id'            => array('Int'),
    'name'          => array('StringTrim', 'StripTags'),
    'description'   => array('StringTrim', 'StripTags'),
    'flag_active'   => array(new Zend_Filter_Boolean()),
    'flag_deleted'  => array(new Zend_Filter_Boolean())
  );

  // Set Validators
  $this->_validators = array(
    'id'            => array('Int'),
    'name'          => array(new Zend_Validate_StringLength(array('min'=>4, 'max'=>50))),
    'description'   => array(new Zend_Validate_StringLength(array('max'=>5000))),
    'flag_active'   => array(new Zend_Validate_InArray(array(true, false))),
    'flag_deleted'  => array(new Zend_Validate_InArray(array(true, false)))
  );

  // Set Properties
  if (!is_null($props)) {
    $this->populate($props);
  }
}

public function setFlagActive($arg)
{
  $input = new Zend_Filter_Input($this->_filters, $this->_validators);
  $input->setData(array('flag_active'=>$arg));
  if (!$input->isValid('flag_active')) {
    throw new Zend_Exception('Invalid FLAG_ACTIVE provided'. gettype($input->flag_active));
  }
  $this->_flag_active = (bool) $input->flag_active;
  return $this;
}

As far as the model is concerned, i tried leaving the validator for flag_active and flag_deleted empty, but this did not change my results. The error message of phpunit remains the same:

Unexpected Exception: Invalid FLAG_ACTIVE providedNULL

Whereas NULL is the type of the variable, WHENEVER i pass false as the argument for the flag via the data-provider goodData() (same happens with badData, too).

My guess is this has something to do with Zend_Filter, but i just can’t figure this out. And hints are greatly appreciated!

UPDATE
Since Drew gave a little hint i’ve tested a little further, yet the problem remains. I have pasted the updated Class and unit test onto pastebin for better readability.

  • PasteBin of the Model Class
  • PasteBin of the TestClass

PHPUnit gives out the following error: You must give a non-empty value for field 'flag_active' same for flag_deleted. A second error on half the tests is <null> does not match expected type "boolean". I’m stuck. No matter if i use filters or an inArrayValidator(array(0=>true,1=>false)) it doesn’t change a thing :\

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T08:18:51+00:00Added an answer on June 6, 2026 at 8:18 am

    If you examine the contents of $input->getMessages(), it should provide some explanation as to why that particular value is coming through as invalid.

    When you use Zend_Filter_Input, any value that did not pass validation will not be magically accessible by its name (e.g. $input->flag_active). The value will always be NULL if it didn’t pass the validation step.

    Certain filters can almost guarantee validation in some cases (i.e. an Int filter with an Int validator) because the filter will filter the data and return a valid value for the validator. Passing hello through the Int filter returns 0 which may validate with a simple Int validator even though the original input data is not valid.

    The reason you are most likely getting a null value within your exception is because the value is not available since it didn’t validate; therefore check the input messages for any hints as to why.

    EDIT:

    I think the problem you are having is that Zend_Filter_Input is not allowing empty values by default. Therefore if the value coming into the filter is false or NULL, it is causing an error with the filter.

    Check out this code:

        <?php
    
        // set up a boolean validator and inarray validator like you have
        $filters    = array('test' => array(new Zend_Filter_Boolean()));
        $validators = array('test' => array(new Zend_Validate_InArray(array(true, false))));
    
        // set up the input filter with default options
        $input      = new Zend_Filter_Input($filters, $validators);
    
        // our "input" value which is (bool)false
        $test       = false;
    
        $input->setData(array('test' => $test));
    
        // run through input filter using default values
        // should throw error about a non-empty value
        if (!$input->isValid('test')) {
            echo "Test is not valid.  <br />Original Value: ";
            var_dump($test);
            echo "<br />";
            print_r($input->getMessages());
        } else {
            echo "Test is valid.<br />Original Value: ";
            var_dump($test);
            echo "<br />Value now: ";
            var_dump($input->test);
        }
    
        // now allowing empty values...
        echo "<hr />\n";
    
        // tell Zend_Filter_Input to allow empty values on input
        $options = array(Zend_Filter_Input::ALLOW_EMPTY => true);
    
        // set up new input filter with option
        $input   = new Zend_Filter_Input($filters, $validators, null, $options);
    
        $input->setData(array('test' => $test));
    
        // run through input filter allowing empty values
        // this should NOT throw an error
        if (!$input->isValid('test')) {
            echo "Test is not valid.  <br />Original Value: ";
            var_dump($test);
            echo "<br />";
            print_r($input->getMessages());
        } else {
            echo "Test is valid.<br />Original Value: ";
            var_dump($test);
            echo "<br />Value now: ";
            var_dump($input->test);
        }
    

    So the problem seems to be that Zend_Filter_Input doesn’t like empty values unless you specify an option. Therefore sending a valid (false) value into the filter causes an error unless you set Zend_Filter_Input::ALLOW_EMPTY to true.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just been reading the article on MSDN about remote validation. This is great,
I am just reading a great tutorial about threads and have a problem with
I was just reading abit about JMS and Apache ActiveMQ. And was wondering what
I was just reading about Magento , a free framework for easily creating an
just was reading this article http://highscalability.com/blog/2010/3/23/digg-4000-performance-increase-by-sorting-in-php-rather-than.html And found this nice article http://wiki.apache.org/cassandra/DataModel I just
Just finished reading Jon Skeet's article about events and delegates and got a question.
I was just reading about Linq to SQL being discontinued. For a while I
While reading through the great online PHP tutorials of Paul Hudson he said Perhaps
I've been reading about how great difference lists are and I was hoping to
I just discovered this great site. I was reading bash tagged posts when the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.