views:

293

answers:

1
$form = new Zend_Form();

$mockDate = new Zend_Form_Element_Text('mock');

$mockDate->addValidator(???????);

$form->addElements(array($mockDate));

$result = $form->isValid();

if ($result) echo "YES!!!";
else echo "NO!!!";

Assumption that the element is in a date format. How do I determine that the date given is greater than or equal to today?

+2  A: 

You can create a simple validator to do this:

class My_Validate_DateGreaterThanToday extends Zend_Validate_Abstract
{
    const DATE_INVALID = 'dateInvalid';

    protected $_messageTemplates = array(
        self::DATE_INVALID => "'%value%' is not greater than or equal today"
    );

    public function isValid($value)
    {
        $this->_setValue($value);

        $today = date('Y-m-d');

        // expecting $value to be YYYY-MM-DD
        if ($value < $today) {
            $this->_error(self::DATE_INVALID);
            return false;
        }

        return true;
    }
}

And add it to the element:

$mockDate->addValidator(new My_Validate_DateGreaterThanToday());

You probably want to check the date with Zend_Date for localization of dates and further benefits.

For creating custom validates, take a look at writing validators from Zend´s manual.

Luiz Damim
Thanks. It worked great!
Brant