View Source

<ac:macro ac:name="unmigrated-inline-wiki-markup"><ac:plain-text-body><![CDATA[{composition-setup}{composition-setup}]]></ac:plain-text-body></ac:macro>
<ac:macro ac:name="unmigrated-inline-wiki-markup"><ac:plain-text-body><![CDATA[{zone-template-instance:ZFPROP:Proposal Zone Template}
{note:title=Under Construction}
This proposal is under construction and is not ready for review.
{note}

{zone-data:component-name}
Zend_Validate_DateBetween
Zend_Validate_DateLessThan
Zend_Validate_DateGreaterThan
{zone-data}

{zone-data:proposer-list}
[Niek Slatius|http://framework.zend.com/wiki/display/~suitalsn]
{zone-data}

{zone-data:liaison}
TBD
{zone-data}

{zone-data:revision}
0.1 - 6 March 2009: Initial Draft.
{zone-data}

{zone-data:overview}
Zend_Validate currently does not have an easy means to compare date such that they can be easily used in form validation. This proposal strives to create such validators.
{zone-data}

{zone-data:references}

{zone-data}

{zone-data:requirements}

{zone-data}

{zone-data:dependencies}
* Zend_Date
* Zend_Validate_Exception
* Zend_Validate_Abstract
{zone-data}

{zone-data:operation}
The component will provide a means to add validators to form elements such that their values can be tested in comparison to some benchmark date.
{zone-data}

{zone-data:milestones}
* Milestone 1: Requirement specifications
* Milestone 2: Receive comments from the community about draft / suggestions from community for further improvements
* Milestone 3: Create working prototype
{zone-data}

{zone-data:class-list}
* Zend_Validate_DateBetween
* Zend_Validate_DateLessThan
* Zend_Validate_DateGreaterThan
{zone-data}

{zone-data:use-cases}
{deck:id=Use Cases}
{card:label=Use Case 1}
{code:php}

$probeDate = new Zend_Date( '2009-03-06' );

$form = new Zend_Form();

$date = new Zend_Form_Element_Text( 'date' );
$date->setLabel( 'date' )
->setRequired( true )
->addValidator( 'NotEmpty', true )
->addValidator( 'StringLength', true, array( 'min' => 0, 'max' => 19 ) )
->addValidator( 'Date', true, array( 'dd-MM-YYYY HH:mm:ss' ) );
->addValidator( 'DateGreaterThan', true, array( $probeDate, Zend_Date::TIMESTAMP ) );

{code}

User inputs "2009-03-05"

{code:php}

var_dump( $form->getErrors() );

{code}

Outputs

{code}
array(15) {
["date"]=>
array(1) {
[0]=>
string(20) "notGreaterThan"
}
}
{code}

{card}
{card:label=Use Case 2}
{card}
{card:label=Use Case 3}
{card}
{deck}
{zone-data}

{zone-data:skeletons}
{deck:id=Skeletons}
{card:label=Zend_Validate_DateBetween}
{code:php}

class Zend_Validate_DateBetween extends Zend_Validate_Abstract
{

const NOT_BETWEEN = 'notBetween';

const NOT_BETWEEN_STRICT = 'notBetweenStrict';

protected $_messageTemplates = array(
self::NOT_BETWEEN => "'%value%' is not between '%min%' and '%max%', inclusively",
self::NOT_BETWEEN_STRICT => "'%value%' is not strictly between '%min%' and '%max%'"
);

protected $_messageVariables = array(
'min' => '_min',
'max' => '_max'
);

protected $_min;

protected $_max;

protected $_inclusive;

protected $_datePart;

public function __construct( Zend_Date $min, Zend_Date $max, $inclusive = true, $datePart = Zend_Date::TIMESTAMP )
{
$this->setMin( $min )
->setMax( $max )
->setInclusive( $inclusive )
->setDatePart( $datePart );
}

public function getMin()
{
return $this->_min;
}

public function setMin( Zend_Date $min )
{
$this->_min = $min;
return $this;
}

public function getMax()
{
return $this->_max;
}

public function setMax( Zend_Date $max )
{
$this->_max = $max;
return $this;
}

public function getInclusive()
{
return $this->_inclusive;
}

public function setInclusive( $inclusive )
{
$this->_inclusive = $inclusive;
return $this;
}

public function getDatePart()
{
return $this->_datePart;
}

public function setDatePart( $datePart )
{
$this->_datePart = $datePart;
return $this;
}

public function isValid( $value )
{
if( !$value instanceof Zend_Date )
{
try
{
$value = new Zend_Date( $value );
}
catch( Exception $exception )
{
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception( $exception->getMessage() );
}
}

$this->_setValue( $value );

if( $this->_inclusive )
{
if( $this->_min->isLater( $value, $this->_datePart ) || $this->_max->isEarlier( $value, $this->_datePart ) )
{
$this->_error( self::NOT_BETWEEN );
return false;
}
}
else
{
if( $value->isLater( $this->_min, $this->_datePart ) || $value->isEarlier( $this->_max, $this->_datePart ) )
{
$this->_error( self::NOT_BETWEEN_STRICT );
return false;
}
}
return true;
}

}
{code}
{card}
{card:label=Zend_Validate_DateLessThan}
{code:php}

class Zend_Validate_DateLessThan extends Zend_Validate_Abstract
{

const NOT_LESS = 'notLessThan';

protected $_messageTemplates = array(
self::NOT_LESS => "'%value%' is not less than '%max%'"
);

protected $_messageVariables = array(
'max' => '_max'
);

protected $_max;

protected $_inclusive;

protected $_datePart;

public function __construct( Zend_Date $max, $datePart = Zend_Date::TIMESTAMP )
{
$this->setMax( $max )
->setDatePart( $datePart );
}

public function getMax()
{
return $this->_max;
}

public function setMax( Zend_Date $max )
{
$this->_max = $max;
return $this;
}

public function getDatePart()
{
return $this->_datePart;
}

public function setDatePart( $datePart )
{
$this->_datePart = $datePart;
return $this;
}

public function isValid( $value )
{
if( !$value instanceof Zend_Date )
{
try
{
$value = new Zend_Date( $value );
}
catch( Exception $exception )
{
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception( $exception->getMessage() );
}
}

$this->_setValue( $value );

if( $this->_max->isEarlier( $value, $this->_datePart ) )
{
$this->_error( self::NOT_LESS );
return false;
}

return true;
}

}
{code}
{card}
{card:label=Zend_Validate_DateGreaterThan}
{code:php}

class Zend_Validate_DateGreaterThan extends Zend_Validate_Abstract
{

const NOT_GREATER = 'notGreaterThan';

protected $_messageTemplates = array(
self::NOT_GREATER => "'%value%' is not greater than '%min%'"
);

protected $_messageVariables = array(
'min' => '_min'
);

protected $_min;

protected $_inclusive;

protected $_datePart;

public function __construct( Zend_Date $min, $datePart = Zend_Date::TIMESTAMP )
{
$this->setMin( $min )
->setDatePart( $datePart );
}

public function getMin()
{
return $this->_min;
}

public function setMin( Zend_Date $min )
{
$this->_min = $min;
return $this;
}

public function getDatePart()
{
return $this->_datePart;
}

public function setDatePart( $datePart )
{
$this->_datePart = $datePart;
return $this;
}

public function isValid( $value )
{
if( !$value instanceof Zend_Date )
{
try
{
$value = new Zend_Date( $value );
}
catch( Exception $exception )
{
require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception( $exception->getMessage() );
}
}

$this->_setValue( $value );

if( $this->_min->isLater( $value, $this->_datePart ) )
{
$this->_error( self::NOT_GREATER );
return false;
}

return true;
}

}
{code}
{card}
{deck}
{zone-data}

{zone-template-instance}]]></ac:plain-text-body></ac:macro>