/** * (non-PHPdoc) * @see lib/form/validator/phValidator::validate() */ public function validate($value, phValidatable $errors) { $compareValue = $this->_compareWith instanceof phData ? $this->_compareWith->getValue() : $this->_compareWith; $valid = false; switch ($this->_operator) { case self::EQUAL: $valid = $value == $compareValue; break; case self::NOT_EQUAL: $valid = $value != $compareValue; break; case self::GREATER_THAN: $valid = $value > $compareValue; break; case self::GREATER_EQUAL: $valid = $value >= $compareValue; break; case self::LESS_THAN: $valid = $value < $compareValue; break; case self::LESS_EQUAL: $valid = $value <= $compareValue; break; } if (!$valid) { $errors->addError($this->getError(self::INVALID)); } return $valid; }
/** * (non-PHPdoc) * @see lib/form/phFormDataItem::bind() */ public function bind($file) { if (!$this->isValidFilePost($file)) { throw new phFileDataException('bound data is not a valid file', phFileDataException::INVALID_FILE_DATA); } return parent::bind($file); }
/** * (non-PHPdoc) * @see lib/form/phDataChangeListener::dataChanged() */ public function dataChanged(phFormDataItem $item) { if ($item instanceof phSimpleArrayDataItem) { /* * $item->getValue will be an array, we need to see if our * value is in it - if so set ourselves to checked by calling * $this->setValue */ $values = $item->getValue(); $ourValue = $this->getRawValue(); if (in_array($ourValue, $values)) { $this->checkOn(); } else { $this->checkOff(); } } else { parent::dataChanged($item); } }
public function testCompareValidator() { $password = new phFormDataItem('password'); $confirmPassword = new phFormDataItem('confirmPassword'); $confirmPassword->bind('password'); $compareVal = new phCompareValidator($confirmPassword, phCompareValidator::EQUAL, array(phCompareValidator::INVALID => "The passwords are not the same")); $this->assertFalse($compareVal->validate('notequal', $password), 'The validator is correctly not valid'); $messages = $this->extractErrorMessages($password->getErrors()); $this->assertTrue(in_array('The passwords are not the same', $messages), 'error message set properly'); $this->assertTrue($compareVal->validate('password', $password), 'The validator is correctly valid'); $compareVal = new phCompareValidator($confirmPassword, phCompareValidator::NOT_EQUAL); $this->assertFalse($compareVal->validate('password', $password), 'The validator is correctly not valid'); $this->assertTrue($compareVal->validate('notequal', $password), 'The validator is correctly valid'); $confirmPassword->bind(5); $compareVal = new phCompareValidator($confirmPassword, phCompareValidator::LESS_THAN); $this->assertFalse($compareVal->validate(10, $password), 'The validator is correctly not valid'); $this->assertTrue($compareVal->validate(1, $password), 'The validator is correctly valid'); $compareVal = new phCompareValidator($confirmPassword, phCompareValidator::LESS_EQUAL); $this->assertFalse($compareVal->validate(10, $password), 'The validator is correctly not valid'); $this->assertTrue($compareVal->validate(5, $password), 'The validator is correctly valid'); $compareVal = new phCompareValidator($confirmPassword, phCompareValidator::GREATER_THAN); $this->assertFalse($compareVal->validate(1, $password), 'The validator is correctly not valid'); $this->assertTrue($compareVal->validate(10, $password), 'The validator is correctly valid'); $compareVal = new phCompareValidator($confirmPassword, phCompareValidator::GREATER_EQUAL); $this->assertFalse($compareVal->validate(1, $password), 'The validator is correctly not valid'); $this->assertTrue($compareVal->validate(5, $password), 'The validator is correctly valid'); /* * test scalar values can be used */ $compareVal = new phCompareValidator(1, phCompareValidator::EQUAL); $this->assertFalse($compareVal->validate(2, $password), 'The validator is correctly not valid'); $this->assertTrue($compareVal->validate(1, $password), 'The validator is correctly valid'); }
public function testFormDataItemToString() { $i = new phFormDataItem('test'); $i->bind('hello world'); $this->assertEquals('hello world', (string) $i, 'phFormDataItem __toString works'); $i->bind(null); $this->assertEquals('', (string) $i, 'phFormDataItem __toString works with nulls'); }
public function bind($value) { $this->wasBound = true; parent::bind($value); }
/** * When a phform element in the view is found a phFormDataItem instance is created * and then when, in bind it has its values set * @see lib/form/phDataChangeListener::dataChanged() */ public function dataChanged(phFormDataItem $item) { $this->setValue($item->getValue()); }
/** * (non-PHPdoc) * @see lib/form/phFormViewElement::bindDataItem() */ public function bindDataItem(phFormDataItem $item) { $item->addChangeListener($this); }