
A simple validator for the Zend Framework to check if a value is greater than or equal to a specified value.
Put this in library/Carbidefinger/Validate/Greaterthaneq.php
<?php
/**
* Greater than or equal to Validator
*
* @package Carbidefinger_Validate
* @author Nick Pack <nick@carbidefinger.net>
* @copyright Copyright (c) 2009, Carbide-Finger Ltd, All Rights Reserved
*/
class Carbidefinger_Validate_Greaterthaneq extends Zend_Validate_Abstract
{
const NOT_GREATEREQUAL = 'notGreaterEqual';
const MISSING_FIELD_NAME = 'missingFieldName';
const INVALID_FIELD_NAME = 'invalidFieldName';
/**
* @var array
*/
protected $_messageTemplates = array(
self::MISSING_FIELD_NAME =>
'ERROR: Field name to match against was not provided.',
self::INVALID_FIELD_NAME =>
'ERROR: The field "%fieldName%" was not provided to match against.',
self::NOT_GREATEREQUAL =>
'Is not more than or equal to %fieldTitle%.'
);
/**
* @var array
*/
protected $_messageVariables = array(
'fieldName' => '_fieldName',
'fieldTitle' => '_fieldTitle'
);
/**
* Name of the field as it appear in the $context array.
*
* @var string
*/
protected $_fieldName;
/**
* Title of the field to display in an error message.
*
* If evaluates to false then will be set to $this->_fieldName.
*
* @var string
*/
protected $_fieldTitle;
/**
* Sets validator options
*
* @param string $fieldName
* @param string $fieldTitle
* @return void
*/
public function __construct($fieldName, $fieldTitle = null) {
$this->setFieldName($fieldName);
$this->setFieldTitle($fieldTitle);
}
/**
* Returns the field name.
*
* @return string
*/
public function getFieldName() {
return $this->_fieldName;
}
/**
* Sets the field name.
*
* @param string $fieldName
* @return Zend_Validate_IdenticalField Provides a fluent interface
*/
public function setFieldName($fieldName) {
$this->_fieldName = $fieldName;
return $this;
}
/**
* Returns the field title.
*
* @return integer
*/
public function getFieldTitle() {
return $this->_fieldTitle;
}
/**
* Sets the field title.
*
* @param string:null $fieldTitle
* @return Zend_Validate_IdenticalField Provides a fluent interface
*/
public function setFieldTitle($fieldTitle = null) {
$this->_fieldTitle = $fieldTitle ? $fieldTitle : $this->_fieldName;
return $this;
}
/**
* Defined by Zend_Validate_Interface
*
* Returns true if a field name has been set, the field name is available in the
* context, and the value of that field name is greater than or equal to the provided value.
*
* @param string $value
*
* @return boolean
*/
public function isValid($value, $context = null) {
$this->_setValue($value);
$field = $this->getFieldName();
if (empty($field)) {
$this->_error(self::MISSING_FIELD_NAME);
return false;
} elseif (!isset($context[$field])) {
$this->_error(self::INVALID_FIELD_NAME);
return false;
} elseif (is_array($context)) {
if ($value >= $context[$field]) {
return true;
}
} elseif (is_string($context) && ($value >= $context)) {
return true;
}
$this->_error(self::NOT_GREATEREQUAL);
return false;
}
}
To use this validator in Zend Form
$element->addPrefixPath(’Carbidefinger’,’Carbidefinger/’)
->addValidator(’Greaterthaneq’, false, array(’FieldToMatch’));
No comments have been posted for this article
HTML tags allowed in comments are: strong,em,ul,ol,li, URL's are automatically converted to links so no need to use <a>.