Actions

Source Code of This Project

/lib/validator/myValidatorDateRange.php

<?php
/*
 * This file is part of the pwp package.
 * (c) 2009-2010 Victor Rad' <victor.v.rad[at]gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
*/

/**
 * myValidatorDateRange validates a date range
 * e.g.: 01.01.1999-01.01.2000
 *
 * @package    pwp
 * @subpackage myValidatorDateRange
 * @author     Victor Rad'
 */
class myValidatorDateRange extends sfValidatorDate
{
    /**
     * Configures the current validator.
     *
     * Available options:
     *
     *  * separator:            The delimiter of dates
     *
     * Available error codes:
     *
     *  * separator
     *
     * @param array $options    An array of options
     * @param array $messages   An array of error messages
     *
     * @see sfValidatorBase
     */
    protected function configure($options = array(), $messages = array())
    {
        parent::configure($options, $messages);
        $this->addMessage('separator', 'The delimiter of dates is not found');

        $this->addRequiredOption('default_value', '');
        $this->addOption('separator', '-');
        $this->addOption('date_output', 'd.m.Y');
    }

    /**
     * @see sfValidatorBase
     */
    protected function doClean($value)
    {
        if ($this->getOption('default_value') == $value){
            return $value;
        }

        if (!strstr($value, '-')) {
            throw new sfValidatorError($this, 'separator');
        }

        list($from, $till) = explode($this->getOption('separator'), $value);
        $from = parent::doClean($from);
        $till = parent::doClean($till);

        return $from . $this->getOption('separator') . $till;
    }
}