Source for file Length.php
Documentation is available at Length.php
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at
* http://formagic.weasle.de/licence.txt
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to licence@weasle.de so we can send you a copy immediately.
* @author Florian Sonnenburg
* @copyright Copyright (c) 2007 Florian Sonnenburg
* @license http://formagic.weasle.de/licence.txt New BSD License
* @revision $Revision: 17 $
require_once('Rule.php');
* Checks if value is between min and max length
* If only one numeric value is passed as argument, this value is interpreted
* Supported arguments are:
* - (integer)min: Minimum length of submitted value
* - (integer)max: Maximum length of submitted value
* - addRule('mandatory', 'errorMessage', 10);
* // at least 10 characters
* - addRule('mandatory', 'errorMessage', array('min' => 1));
* // same as addRule('mandatory')
* - addRule('mandatory', 'errorMessage', array('max' => 10));
* - addRule('mandatory', 'errorMessage', array('min' => 5, 'max' => 10));
* @author Florian Sonnenburg
* @copyright Copyright (c) 2007 Marc Schrader
* @version $Id: Length.php 17 2007-08-19 20:40:10Z meweasle $
private $_errorMessages = array(
'min' => 'Bitte mindestens %MIN% Zeichen eingeben',
'max' => 'Bitte maximal %MAX% Zeichen eingeben',
'both' => 'Eingabe muss zwischen %MIN% und %MAX% Zeichen haben',
'mandatory' => 'Bitte Wert eingeben'
* Tells what kind of violation occured
* Extends parent constructor
public function __construct($type, $errorMessage= null, $args= null)
if ( !isset ($errorMessage['min'])
|| !isset ($errorMessage['max'])
|| !isset ($errorMessage['both'])
|| !isset ($errorMessage['mandatory'])
'keys "min", "max", "both" and "mandatory"');
$this->_errorMessages = $errorMessage;
} elseif($errorMessage) {
$this->_errorMessages['min'] = $errorMessage;
$this->_errorMessages['max'] = $errorMessage;
$this->_errorMessages['both'] = $errorMessage;
$this->_errorMessages['mandatory'] = $errorMessage;
* Length rule checks if value is between min and max length.
* (integer)_args['min']: Min length
* (integer)_args['max']: Max length
* @param Formagic_Item $itemObj
public function check($item)
$this->_violatedType = null;
$value = $item->getValue();
// check if value has to be entered for the rule to be valid
&& !$item->hasRule('mandatory')
&& !isset ($this->_args['mandatory'])
// no arguments --> any length will do
// Check min and max length
if ( isset ($this->_args['min'])
&& isset ($this->_args['max'])
if ( $length >= (int) $this->_args['min']
&& $length <= (int) $this->_args['max']
array((int) $this->_args['min'], (int) $this->_args['max']),
$this->_errorMessages['both']);
if (isset ($this->_args['max'])) {
if ($length <= (int) $this->_args['max']) {
(int) $this->_args['max'],
$this->_errorMessages['max']);
if (isset ($this->_args['min'])) {
if ($length >= (int) $this->_args['min']) {
(int) $this->_args['min'],
$this->_errorMessages['min']);
// no min, no max --> mandatory rule applies
|