Skip to content

Commit

Permalink
validate() method for Container and Form
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.php.net/repository/pear/packages/HTML_QuickForm2/trunk@235647 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information
sad-spirit committed May 14, 2007
1 parent 7976e4d commit 4f9d548
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
17 changes: 17 additions & 0 deletions QuickForm2.php
Expand Up @@ -194,5 +194,22 @@ public function __toString()
{
throw new HTML_QuickForm2_Exception('Not implemented');
}

/**
* Performs the server-side validation
*
* @return boolean Whether all form's elements are valid
*/
public function validate()
{
$isSubmitted = false;
foreach ($this->datasources as $ds) {
if ($ds instanceof HTML_QuickForm2_DataSource_Submit) {
$isSubmitted = true;
break;
}
}
return $isSubmitted? parent::validate(): false;
}
}
?>
17 changes: 17 additions & 0 deletions QuickForm2/Container.php
Expand Up @@ -403,6 +403,23 @@ protected function updateValue()
$child->updateValue();
}
}


/**
* Performs the server-side validation
*
* This method also calls validate() on all contained elements.
*
* @return boolean Whether the container and all contained elements are valid
*/
protected function validate()
{
$valid = parent::validate();
foreach ($this as $child) {
$valid = $child->validate() && $valid;
}
return $valid;
}
}

/**
Expand Down
46 changes: 45 additions & 1 deletion tests/QuickForm2/ContainerTest.php
Expand Up @@ -46,10 +46,19 @@
* Container class
*/
require_once 'HTML/QuickForm2/Container.php';

/**
* Base class for "scalar" elements
*/
require_once 'HTML/QuickForm2/Element.php';

/**
* PHPUnit2 Test Case
* Base class for HTML_QuickForm2 rules
*/
require_once 'HTML/QuickForm2/Rule.php';

/**
* PHPUnit Test Case
*/
require_once 'PHPUnit/Framework/TestCase.php';

Expand Down Expand Up @@ -81,12 +90,15 @@ public function setValue($value)
* A non-abstract subclass of Container
*
* Container class is still abstract, we should "implement" the remaining methods
* and also make validate() public to be able to test it.
*/
class HTML_QuickForm2_ContainerImpl extends HTML_QuickForm2_Container
{
public function getType() { return 'concrete'; }
public function setValue($value) { return ''; }
public function __toString() { return ''; }

public function validate() { return parent::validate(); }
}

/**
Expand Down Expand Up @@ -428,5 +440,37 @@ public function testGetValue()
'baz' => 'yet another value'
), $c1->getValue());
}

public function testValidate()
{
$cValidate = new HTML_QuickForm2_ContainerImpl('validate');
$el1 = $cValidate->appendChild(new HTML_QuickForm2_ElementImpl2('foo'));
$el2 = $cValidate->appendChild(new HTML_QuickForm2_ElementImpl2('bar'));

$ruleTrue1 = $this->getMock(
'HTML_QuickForm2_Rule', array('checkValue'),
array($cValidate, 'irrelevant message')
);
$ruleTrue1->expects($this->once())->method('checkValue')
->will($this->returnValue(true));
$ruleFalse = $this->getMock(
'HTML_QuickForm2_Rule', array('checkValue'),
array($el1, 'some error')
);
$ruleFalse->expects($this->once())->method('checkValue')
->will($this->returnValue(false));
$ruleTrue2 = $this->getMock(
'HTML_QuickForm2_Rule', array('checkValue'),
array($el2, 'irrelevant message')
);
$ruleTrue2->expects($this->once())->method('checkValue')
->will($this->returnValue(true));

$cValidate->addRule($ruleTrue1);
$el1->addRule($ruleFalse);
$el2->addRule($ruleTrue2);
$this->assertFalse($cValidate->validate());
$this->assertEquals('', $cValidate->getError());
}
}
?>
9 changes: 9 additions & 0 deletions tests/QuickForm2Test.php
Expand Up @@ -173,6 +173,15 @@ public function testSetDataSources()
$this->fail('Expected HTML_QuickForm2_InvalidArgumentException was not thrown');
}

public function testValidateChecksWhetherFormIsSubmitted()
{
$form1 = new HTML_QuickForm2('notrack', 'post');
$this->assertFalse($form1->validate());

$form2 = new HTML_QuickForm2('track', 'post');
$this->assertTrue($form2->validate());
}

public function tearDown()
{
$_REQUEST = $this->request;
Expand Down

0 comments on commit 4f9d548

Please sign in to comment.