public function validateOpenDate($validator, $value) { if ($this->isNew()) { $dateValidator = new sfValidatorDate(array('min' => strtotime(date('Y-m-d'))), array('min' => sfContext::getInstance()->getI18N()->__('The open date must be after now.'))); $value['open_date'] = $dateValidator->clean($value['open_date']); } return $value; }
public function validateOpenDate($validator, $value) { if ($this->isNew()) { $dateValidator = new sfValidatorDate(array('min' => strtotime(date('Y-m-d')), 'required' => false), array('min' => 'The open date must be after now.')); try { $value['open_date'] = $dateValidator->clean($value['open_date']); } catch (sfValidatorError $e) { throw new sfValidatorErrorSchema($validator, array('open_date' => $e)); } } return $value; }
protected function checkRange($range = array()) { // checking single values if (isset($range['date_from'])) { $v = new sfValidatorDate(); $range['date_from'] = $v->clean($range['date_from']); } if (isset($range['date_to'])) { $v = new sfValidatorDate(); $range['date_to'] = $v->clean($range['date_to']); } if (isset($range['kilometers_from'])) { $v = new sfValidatorInteger(array('min' => 0)); $range['kilometers_from'] = $v->clean($range['kilometers_from']); } if (isset($range['kilometers_to'])) { $v = new sfValidatorInteger(array('min' => 0)); $range['kilometers_to'] = $v->clean($range['kilometers_to']); } // checking ranges if (isset($range['date_from']) && isset($range['kilometers_from'])) { throw new sfException('Only one between "date_from" and "kilometers_from" can be set.'); } if (isset($range['date_to']) && isset($range['kilometers_to'])) { throw new sfException('Only one between "date_to" and "kilometers_to" can be set.'); } $this->log('Range: '); if (isset($range['date_from'])) { $this->log(' - From ' . $range['date_from']); } if (isset($range['kilometers_from'])) { $this->log(' - From ' . $range['kilometers_from'] . ' km'); } if (isset($range['date_to'])) { $this->log(' - To ' . $range['date_to']); } if (isset($range['kilometers_to'])) { $this->log(' - To ' . $range['kilometers_to'] . ' km'); } if (!isset($range['date_from']) && !isset($range['kilometers_from'])) { $range['kilometers_from'] = 0; $this->log(' - From 0 km'); } if (!isset($range['date_to']) && !isset($range['kilometers_to'])) { $range['date_to'] = date('Y-m-d'); $this->log(' - To ' . date('Y-m-d')); } $this->log(' '); return $range; }
/** * prepares the value to be processed by sfValidatorDate::doClean by creating * flattening the array structure of the date+timewidget to a string * * Input: $value['date'], $value['hour'], $value['minute'], $value['second'] * where $value['date'] is in the format: yyyy-mm-dd * * Output: yyyy-mm-dd hh:mm:ss * * @param mixed $value * @return */ protected function doClean($value) { $hour = isset($value['hour']) ? $value['hour'] : 0; $minute = isset($value['minute']) ? $value['minute'] : 0; $second = isset($value['second']) ? $value['second'] : 0; return parent::doClean(sprintf('%s %s:%s:%s', $value['date'], $hour, $minute, $second)); }
/** * Configures the current validator. * * Available options: * * * date_format: A regular expression that dates must match * * with_time: true if the validator must return a time, false otherwise * * date_output: The format to use when returning a date (default to Y-m-d) * * datetime_output: The format to use when returning a date with time (default to Y-m-d H:i:s) * * date_format_error: The date format to use when displaying an error for a bad_format error (use date_format if not provided) * * max: The maximum date allowed (as a timestamp) * * min: The minimum date allowed (as a timestamp) * * date_format_range_error: The date format to use when displaying an error for min/max (default to d/m/Y H:i:s) * * Available error codes: * * * bad_format * * min * * max * * @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); $i18n = dm::getI18n(); $this->addMessage('bad_format', '"%value%" ' . $i18n->__('does not match the date format') . ' (%date_format%).'); $this->addMessage('max', $i18n->__('The date must be before') . ' %max%.'); $this->addMessage('min', $i18n->__('The date must be after') . ' %min%.'); // $this->addOption('date_format', $this->getDateFormat(dm::getUser()->getCulture())); $this->addOption('context', dmContext::getInstance()); }
/** * Configures the current validator. * * Available options from sfValidatorDate: * * * date_format: A regular expression that dates must match * * with_time: true if the validator must return a time, false otherwise * * date_output: The format to use when returning a date (default to Y-m-d) * * datetime_output: The format to use when returning a date with time (default to Y-m-d H:i:s) * * date_format_error: The date format to use when displaying an error for a bad_format error (use date_format if not provided) * * max: The maximum date allowed (as a timestamp) * * min: The minimum date allowed (as a timestamp) * * from_date: true if the date to validate is a begin date, false if an end date in a range * * date_format_range_error: The date format to use when displaying an error for min/max (default to d/m/Y H:i:s) * * New options added for the fuzzyDateValidator: * * * from_date: A flag telling if the date entered is a from or a to date in a range of date: used to correct what is inserted * * Available error codes: * * * bad_format * * min * * max * * year_missing * * month_missing * * day_missing * * hour_missing * * minute_missing * * wrong_date_part_length * * @param array $options An array of options * @param array $messages An array of error messages * @see sfValidatorDate */ protected function configure($options = array(), $messages = array()) { $this->addMessage('year_missing', 'Year missing.'); $this->addMessage('month_missing', 'Month missing or remove the day and time.'); $this->addMessage('day_missing', 'Day missing or remove the time.'); $this->addMessage('hour_missing', 'Hour missing or remove the time.'); $this->addMessage('minute_missing', 'minute missing or remove the seconds.'); $this->addMessage('time_without_date', 'Day missing or remove the time.'); $this->addMessage('wrong_date_part_length', 'A part of date is wrong.'); $this->addOption('from_date', true); parent::configure($options, $messages); }
protected function convertDateArrayToString($value) { if ($this->getOption('with_meridiem')) { if (!isset($value['meridiem']) || !in_array(strtolower($value['meridiem']), array('am', 'pm'))) { throw new sfValidatorError($this, 'invalid', array('value' => $value)); } if (strtolower($value['meridiem']) == 'pm') { $value['hour'] += 12; } } $ret = parent::convertDateArrayToString($value); $timestamp = strtotime($ret); sfContext::getInstance()->getLogger()->debug("Timestamp: " . $timestamp . ' for ' . date('Y-m-d H:i:s', $timestamp) . ' (' . date('Y-m-d h:i:s A', $timestamp) . ')'); return $ret; }
/** * Converts an array representing a date to a timestamp. * * The array can contains the following keys: year, month, day, hour, minute, second * * @param array $value An array of date elements * * @return int A timestamp */ protected function convertDateArrayToString($value) { /** * The (original) date validator was written to convert three or six single * input boxes to one date. * * We don't want that here, so we will take the value and convert it on our own, * if it seems to be a value submitted by our imput->date. */ if (array_key_exists(0, $value)) { //our value comes from the preg_match matches field return $this->convertPregDateToString($value); } return parent::convertDateArrayToString($value); }
protected function doClean($value) { if (!is_array($value)) { $dateSeparator = $this->getOption('date_separator'); $dateArray = explode($dateSeparator, $value); if (count($dateArray) != 3) { throw new sfValidatorError($this, 'invalid', array('value' => $value)); } else { $value = array(); $value['day'] = $dateArray[0]; $value['month'] = $dateArray[1]; $value['year'] = $dateArray[2]; } } return parent::doClean($value); }
protected function isEmpty($value) { if (!is_array($value)) { return parent::isEmpty($value); } foreach ($value as $v) { if ($v || is_numeric($v)) { return false; } } return true; }
/** * @see sfValidatorDate */ protected function configure($options = array(), $messages = array()) { parent::configure($options, $messages); $this->setOption('with_time', true); }
$t->is($v->clean(time()), date('Y-m-d H:i:s', time()), '->clean() can accept date time with the with_time option'); $v->setOption('date_format', '~(?P<day>\\d{2})/(?P<month>\\d{2})/(?P<year>\\d{4})~'); $t->is($v->clean('18/10/2005'), '2005-10-18 00:00:00', '->clean() can accept date time with the with_time option'); $v->setOption('date_format', '~(?P<day>\\d{2})/(?P<month>\\d{2})/(?P<year>\\d{4}) (?P<hour>\\d{2})\\:(?P<minute>\\d{2})~'); $t->is($v->clean('18/10/2005 12:30'), '2005-10-18 12:30:00', '->clean() can accept date time with the with_time option'); $v->setOption('date_format', null); // change date output $t->diag('change date output'); $v->setOption('with_time', false); $v->setOption('date_output', 'U'); $t->is($v->clean(time()), time(), '->clean() output format can be change with the date_output option'); $v->setOption('datetime_output', 'U'); $v->setOption('with_time', true); $t->is($v->clean(time()), time(), '->clean() output format can be change with the date_output option'); // required $v = new sfValidatorDate(); foreach (array(array('year' => '', 'month' => '', 'day' => ''), array('year' => null, 'month' => null, 'day' => null), '', null) as $input) { try { $v->clean($input); $t->fail('->clean() throws an exception if the date is empty and required is true'); } catch (sfValidatorError $e) { $t->pass('->clean() throws an exception if the date is empty and required is true'); } } // max and min options $t->diag('max and min options'); $v->setOption('min', strtotime('1 Jan 2005')); $v->setOption('max', strtotime('31 Dec 2007')); $t->is($v->clean('18 october 2005'), '2005-10-18', '->clean() can accept a max/min option'); try { $v->clean('18 october 2004');
/** * Configure the validator. Adding two more options to the regular validator * input_date_format : date format from the form's input. Default is Y-m-d (ex: 2009-08-19) * output_date_format : date format to get as output * Allowed format masks: * d : 01 to 31 * m : 01 to 12 * y : 00 to 99 * Y : example 2009 * Allowed format separators: /-_,. and space * @see trunk/lib/vendor/symfony/lib/validator/sfValidatorDate#configure($options, $messages) */ protected function configure($options = array(), $messages = array()) { $this->addOption('input_date_format', 'Y-m-d'); $this->addOption('output_date_format', 'Y-m-d'); parent::configure($options, $messages); }
/** * Configures the current validator. * * Available options: * * * date_format: A regular expression that dates must match * * with_time: true if the validator must return a time, false otherwise * * date_output: The format to use when returning a date (default to Y-m-d) * * datetime_output: The format to use when returning a date with time (default to Y-m-d H:i:s) * * date_format_error: The date format to use when displaying an error for a bad_format error * (use date_format if not provided) * * max: The maximum date allowed (as a timestamp) * * min: The minimum date allowed (as a timestamp) * * date_format_range_error: The date format to use when displaying an error for min/max (default to d/m/Y H:i:s) * * context: The symfony application context * * Available error codes: * * * bad_format * * min * * max * * @param array $options An array of options * @param array $messages An array of error messages * * @see sfValidatorBase */ protected function configure($options = array(), $messages = array()) { $this->addOption('context', sfContext::getInstance()); parent::configure($options, $messages); }