[Form] Removed deprecated fields

This commit is contained in:
Bernhard Schussek 2011-02-23 21:23:14 +01:00
parent ac5440f8d4
commit 5a2404a1d6
3 changed files with 0 additions and 656 deletions

View File

@ -1,258 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form;
use Symfony\Component\Form\ValueTransformer\ReversedTransformer;
use Symfony\Component\Form\ValueTransformer\DateTimeToStringTransformer;
use Symfony\Component\Form\ValueTransformer\DateTimeToTimestampTransformer;
use Symfony\Component\Form\ValueTransformer\ValueTransformerChain;
use Symfony\Component\Form\ValueTransformer\DateTimeToLocalizedStringTransformer;
use Symfony\Component\Form\ValueTransformer\DateTimeToArrayTransformer;
use Symfony\Component\Form\ChoiceList\PaddedChoiceList;
use Symfony\Component\Form\ChoiceList\MonthChoiceList;
/**
* Represents a date field.
*
* Available options:
*
* * widget: How to render the field ("input" or "choice"). Default: "choice".
* * type: The type of the date stored on the object. Default: "datetime":
* * datetime: A DateTime object;
* * string: A raw string (e.g. 2011-05-01, Y-m-d);
* * timestamp: A unix timestamp (e.g. 1304208000);
* * raw: A year, month, day array.
* * pattern: The pattern for the select boxes when "widget" is "choice".
* You can use the placeholders "{{ year }}", "{{ month }}" and "{{ day }}".
* Default: locale dependent.
*
* * years: An array of years for the year select tag.
* * months: An array of months for the month select tag.
* * days: An array of days for the day select tag.
*
* * format: The date format type to use for displaying the data. Default: medium.
* * data_timezone: The timezone of the data. Default: server timezone.
* * user_timezone: The timezone of the user entering a new value. Default: server timezone.
*
*/
class DateField extends HybridField
{
const FULL = 'full';
const LONG = 'long';
const MEDIUM = 'medium';
const SHORT = 'short';
const DATETIME = 'datetime';
const STRING = 'string';
const TIMESTAMP = 'timestamp';
const RAW = 'raw';
const INPUT = 'input';
const CHOICE = 'choice';
protected static $formats = array(
self::FULL,
self::LONG,
self::MEDIUM,
self::SHORT,
);
protected static $intlFormats = array(
self::FULL => \IntlDateFormatter::FULL,
self::LONG => \IntlDateFormatter::LONG,
self::MEDIUM => \IntlDateFormatter::MEDIUM,
self::SHORT => \IntlDateFormatter::SHORT,
);
protected static $widgets = array(
self::INPUT,
self::CHOICE,
);
protected static $types = array(
self::DATETIME,
self::STRING,
self::TIMESTAMP,
self::RAW,
);
/**
* The ICU formatter instance
* @var \IntlDateFormatter
*/
protected $formatter;
/**
* {@inheritDoc}
*/
public function __construct($key, array $options = array())
{
// Override parent option
// \DateTime objects are never edited by reference, because
// we treat them like value objects
$this->addOption('by_reference', false);
parent::__construct($key, $options);
}
protected function configure()
{
$this->addOption('widget', self::CHOICE, self::$widgets);
$this->addOption('type', self::DATETIME, self::$types);
$this->addOption('pattern');
$this->addOption('years', range(date('Y') - 5, date('Y') + 5));
$this->addOption('months', range(1, 12));
$this->addOption('days', range(1, 31));
$this->addOption('format', self::MEDIUM, self::$formats);
$this->addOption('data_timezone', date_default_timezone_get());
$this->addOption('user_timezone', date_default_timezone_get());
$this->formatter = new \IntlDateFormatter(
\Locale::getDefault(),
self::$intlFormats[$this->getOption('format')],
\IntlDateFormatter::NONE
);
if ($this->getOption('type') === self::STRING) {
$this->setNormalizationTransformer(new ReversedTransformer(
new DateTimeToStringTransformer(array(
'input_timezone' => $this->getOption('data_timezone'),
'output_timezone' => $this->getOption('data_timezone'),
'format' => 'Y-m-d',
))
));
} else if ($this->getOption('type') === self::TIMESTAMP) {
$this->setNormalizationTransformer(new ReversedTransformer(
new DateTimeToTimestampTransformer(array(
'output_timezone' => $this->getOption('data_timezone'),
'input_timezone' => $this->getOption('data_timezone'),
))
));
} else if ($this->getOption('type') === self::RAW) {
$this->setNormalizationTransformer(new ReversedTransformer(
new DateTimeToArrayTransformer(array(
'input_timezone' => $this->getOption('data_timezone'),
'output_timezone' => $this->getOption('data_timezone'),
'fields' => array('year', 'month', 'day'),
))
));
}
if ($this->getOption('widget') === self::INPUT) {
$this->setValueTransformer(new DateTimeToLocalizedStringTransformer(array(
'date_format' => $this->getOption('format'),
'time_format' => DateTimeToLocalizedStringTransformer::NONE,
'input_timezone' => $this->getOption('data_timezone'),
'output_timezone' => $this->getOption('user_timezone'),
)));
$this->setFieldMode(self::FIELD);
} else {
$this->setValueTransformer(new DateTimeToArrayTransformer(array(
'input_timezone' => $this->getOption('data_timezone'),
'output_timezone' => $this->getOption('user_timezone'),
)));
$this->setFieldMode(self::FORM);
$this->add(new ChoiceField('year', array(
'choice_list' => new PaddedChoiceList($this->getOption('years'), 2, '0', STR_PAD_LEFT),
)));
$this->add(new ChoiceField('month', array(
'choice_list' => new MonthChoiceList($this->formatter, $this->getOption('months')),
)));
$this->add(new ChoiceField('day', array(
'choice_list' => new PaddedChoiceList($this->getOption('days'), 2, '0', STR_PAD_LEFT),
)));
}
}
// temporary
public function getFormatter()
{
return $this->formatter;
}
/**
* Returns whether the year of the field's data is valid
*
* The year is valid if it is contained in the list passed to the field's
* option "years".
*
* @return Boolean
*/
public function isYearWithinRange()
{
$date = $this->getNormalizedData();
return $this->isEmpty() || ($this->isGroup() && $this->get('year')->isEmpty())
|| in_array($date->format('Y'), $this->getOption('years'));
}
/**
* Returns whether the month of the field's data is valid
*
* The month is valid if it is contained in the list passed to the field's
* option "months".
*
* @return Boolean
*/
public function isMonthWithinRange()
{
$date = $this->getNormalizedData();
return $this->isEmpty() || ($this->isGroup() && $this->get('month')->isEmpty())
|| in_array($date->format('m'), $this->getOption('months'));
}
/**
* Returns whether the day of the field's data is valid
*
* The day is valid if it is contained in the list passed to the field's
* option "days".
*
* @return Boolean
*/
public function isDayWithinRange()
{
$date = $this->getNormalizedData();
return $this->isEmpty() || ($this->isGroup() && $this->get('day')->isEmpty())
|| in_array($date->format('d'), $this->getOption('days'));
}
/**
* Returns whether the field is neither completely filled (a selected
* value in each dropdown) nor completely empty
*
* @return Boolean
*/
public function isPartiallyFilled()
{
if ($this->isField()) {
return false;
}
if ($this->isEmpty()) {
return false;
}
if ($this->get('year')->isEmpty() || $this->get('month')->isEmpty()
|| $this->get('day')->isEmpty()) {
return true;
}
return false;
}
}

View File

@ -1,138 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form;
use Symfony\Component\Form\Exception\FormException;
/**
* A field that can dynamically act like a field or like a field group
*
* You can use the method setFieldMode() to switch between the modes
* HybridField::FIELD and HybridField::FORM. This is useful when you want
* to create a field that, depending on its configuration, can either be
* a single field or a combination of different fields (e.g. a date field
* that might be a textbox or several select boxes).
*
* @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
*/
class HybridField extends Form
{
const FIELD = 0;
const FORM = 1;
protected $mode = self::FIELD;
/**
* Sets the current mode of the field
*
* Note that you can't switch modes anymore once you have added children to
* this field.
*
* @param integer $mode One of the constants HybridField::FIELD and
* HybridField::FORM.
*/
public function setFieldMode($mode)
{
if (count($this) > 0 && $mode === self::FIELD) {
throw new FormException('Switching to mode FIELD is not allowed after adding nested fields');
}
$this->mode = $mode;
}
/**
* @return Boolean
*/
public function isField()
{
return self::FIELD === $this->mode;
}
/**
* @return Boolean
*/
public function isGroup()
{
return self::FORM === $this->mode;
}
/**
* @return integer
*/
public function getFieldMode()
{
return $this->mode;
}
/**
* {@inheritDoc}
*
* @throws FormException When the field is in mode HybridField::FIELD adding
* subfields is not allowed
*/
public function add($field)
{
if ($this->mode === self::FIELD) {
throw new FormException('You cannot add nested fields while in mode FIELD');
}
return parent::add($field);
}
/**
* {@inheritDoc}
*/
public function getDisplayedData()
{
if ($this->mode === self::FORM) {
return parent::getDisplayedData();
} else {
return Field::getDisplayedData();
}
}
/**
* {@inheritDoc}
*/
public function setData($data)
{
if ($this->mode === self::FORM) {
parent::setData($data);
} else {
Field::setData($data);
}
}
/**
* {@inheritDoc}
*/
public function submit($data)
{
if ($this->mode === self::FORM) {
parent::submit($data);
} else {
Field::submit($data);
}
}
/**
* {@inheritDoc}
*/
public function isEmpty()
{
if ($this->mode === self::FORM) {
return parent::isEmpty();
} else {
return Field::isEmpty();
}
}
}

View File

@ -1,260 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form;
use Symfony\Component\Form\ValueTransformer\ReversedTransformer;
use Symfony\Component\Form\ValueTransformer\DateTimeToArrayTransformer;
use Symfony\Component\Form\ValueTransformer\DateTimeToStringTransformer;
use Symfony\Component\Form\ValueTransformer\DateTimeToTimestampTransformer;
use Symfony\Component\Form\ValueTransformer\ValueTransformerChain;
use Symfony\Component\Form\ChoiceList\HourChoiceList;
use Symfony\Component\Form\ChoiceList\MinuteChoiceList;
use Symfony\Component\Form\ChoiceList\SecondChoiceList;
/**
* Represents a time field.
*
* Available options:
*
* * widget: How to render the time field ("input" or "choice"). Default: "choice".
* * type: The type of the date stored on the object. Default: "datetime":
* * datetime: A DateTime object;
* * string: A raw string (e.g. 2011-05-01 12:30:00, Y-m-d H:i:s);
* * timestamp: A unix timestamp (e.g. 1304208000).
* * raw: An hour, minute, second array
* * with_seconds Whether or not to create a field for seconds. Default: false.
*
* * hours: An array of hours for the hour select tag.
* * minutes: An array of minutes for the minute select tag.
* * seconds: An array of seconds for the second select tag.
*
* * data_timezone: The timezone of the data. Default: UTC.
* * user_timezone: The timezone of the user entering a new value. Default: UTC.
*/
class TimeField extends Form
{
const INPUT = 'input';
const CHOICE = 'choice';
const DATETIME = 'datetime';
const STRING = 'string';
const TIMESTAMP = 'timestamp';
const RAW = 'raw';
protected static $widgets = array(
self::INPUT,
self::CHOICE,
);
protected static $types = array(
self::DATETIME,
self::STRING,
self::TIMESTAMP,
self::RAW,
);
/**
* {@inheritDoc}
*/
public function __construct($key, array $options = array())
{
// Override parent option
// \DateTime objects are never edited by reference, because
// we treat them like value objects
$this->addOption('by_reference', false);
parent::__construct($key, $options);
}
/**
* {@inheritDoc}
*/
protected function configure()
{
$this->addOption('widget', self::CHOICE, self::$widgets);
$this->addOption('type', self::DATETIME, self::$types);
$this->addOption('with_seconds', false);
$this->addOption('hours', array());
$this->addOption('minutes', array());
$this->addOption('seconds', array());
$this->addOption('data_timezone', date_default_timezone_get());
$this->addOption('user_timezone', date_default_timezone_get());
if ($this->getOption('widget') == self::INPUT) {
$this->add(new TextField('hour', array('max_length' => 2)));
$this->add(new TextField('minute', array('max_length' => 2)));
if ($this->getOption('with_seconds')) {
$this->add(new TextField('second', array('max_length' => 2)));
}
} else {
$this->add(new ChoiceField('hour', array(
'choice_list' => new HourChoiceList($this->getOption('hours')),
)));
$this->add(new ChoiceField('minute', array(
'choice_list' => new MinuteChoiceList($this->getOption('minutes')),
)));
if ($this->getOption('with_seconds')) {
$this->add(new ChoiceField('second', array(
'choice_list' => new SecondChoiceList($this->getOption('seconds')),
)));
}
}
$fields = array('hour', 'minute');
if ($this->getOption('with_seconds')) {
$fields[] = 'second';
}
if ($this->getOption('type') == self::STRING) {
$this->setNormalizationTransformer(new ReversedTransformer(
new DateTimeToStringTransformer(array(
'format' => 'H:i:s',
'input_timezone' => $this->getOption('data_timezone'),
'output_timezone' => $this->getOption('data_timezone'),
))
));
} else if ($this->getOption('type') == self::TIMESTAMP) {
$this->setNormalizationTransformer(new ReversedTransformer(
new DateTimeToTimestampTransformer(array(
'input_timezone' => $this->getOption('data_timezone'),
'output_timezone' => $this->getOption('data_timezone'),
))
));
} else if ($this->getOption('type') === self::RAW) {
$this->setNormalizationTransformer(new ReversedTransformer(
new DateTimeToArrayTransformer(array(
'input_timezone' => $this->getOption('data_timezone'),
'output_timezone' => $this->getOption('data_timezone'),
'fields' => $fields,
))
));
}
$this->setValueTransformer(new DateTimeToArrayTransformer(array(
'input_timezone' => $this->getOption('data_timezone'),
'output_timezone' => $this->getOption('user_timezone'),
// if the field is rendered as choice field, the values should be trimmed
// of trailing zeros to render the selected choices correctly
'pad' => $this->getOption('widget') == self::INPUT,
'fields' => $fields,
)));
}
public function isField()
{
return self::INPUT === $this->getOption('widget');
}
public function isWithSeconds()
{
return $this->getOption('with_seconds');
}
/**
* Generates an array of choices for the given values
*
* If the values are shorter than $padLength characters, they are padded with
* zeros on the left side.
*
* @param array $values The available choices
* @param integer $padLength The length to pad the choices
* @return array An array with the input values as keys and the
* padded values as values
*/
protected function generatePaddedChoices(array $values, $padLength)
{
$choices = array();
foreach ($values as $value) {
$choices[$value] = str_pad($value, $padLength, '0', STR_PAD_LEFT);
}
return $choices;
}
/**
* Returns whether the hour of the field's data is valid
*
* The hour is valid if it is contained in the list passed to the field's
* option "hours".
*
* @return Boolean
*/
public function isHourWithinRange()
{
$date = $this->getNormalizedData();
return $this->isEmpty() || $this->get('hour')->isEmpty()
|| in_array($date->format('H'), $this->getOption('hours'));
}
/**
* Returns whether the minute of the field's data is valid
*
* The minute is valid if it is contained in the list passed to the field's
* option "minutes".
*
* @return Boolean
*/
public function isMinuteWithinRange()
{
$date = $this->getNormalizedData();
return $this->isEmpty() || $this->get('minute')->isEmpty()
|| in_array($date->format('i'), $this->getOption('minutes'));
}
/**
* Returns whether the second of the field's data is valid
*
* The second is valid if it is contained in the list passed to the field's
* option "seconds".
*
* @return Boolean
*/
public function isSecondWithinRange()
{
$date = $this->getNormalizedData();
return $this->isEmpty() || !$this->has('second') || $this->get('second')->isEmpty()
|| in_array($date->format('s'), $this->getOption('seconds'));
}
/**
* Returns whether the field is neither completely filled (a selected
* value in each dropdown) nor completely empty
*
* @return Boolean
*/
public function isPartiallyFilled()
{
if ($this->isField()) {
return false;
}
if ($this->isEmpty()) {
return false;
}
if ($this->get('hour')->isEmpty() || $this->get('minute')->isEmpty()
|| ($this->isWithSeconds() && $this->get('second')->isEmpty())) {
return true;
}
return false;
}
}