Skip to content

Commit

Permalink
[Form] Made InputField instantiable so that simple input fields can b…
Browse files Browse the repository at this point in the history
…e created on the fly
  • Loading branch information
Bernhard Schussek authored and fabpot committed Oct 22, 2010
1 parent e9fcacd commit 96a0bff
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 24 deletions.
8 changes: 4 additions & 4 deletions src/Symfony/Component/Form/CheckboxField.php
Expand Up @@ -21,10 +21,10 @@ class CheckboxField extends ToggleField
/**
* {@inheritDoc}
*/
public function getAttributes()
public function __construct($key, array $options = array())
{
return array_merge(parent::getAttributes(), array(
'type' => 'checkbox',
));
$options['type'] = 'checkbox';

parent::__construct($key, $options);
}
}
8 changes: 4 additions & 4 deletions src/Symfony/Component/Form/HiddenField.php
Expand Up @@ -21,11 +21,11 @@ class HiddenField extends InputField
/**
* {@inheritDoc}
*/
public function getAttributes()
public function __construct($key, array $options = array())
{
return array_merge(parent::getAttributes(), array(
'type' => 'hidden',
));
$options['type'] = 'hidden';

parent::__construct($key, $options);
}

/**
Expand Down
13 changes: 12 additions & 1 deletion src/Symfony/Component/Form/InputField.php
Expand Up @@ -16,8 +16,18 @@
*
* @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
*/
abstract class InputField extends Field
class InputField extends Field
{
/**
* {@inheritDoc}
*/
protected function configure()
{
parent::configure();

$this->addRequiredOption('type');
}

/**
* {@inheritDoc}
*/
Expand All @@ -28,6 +38,7 @@ public function getAttributes()
'name' => $this->getName(),
'value' => $this->getDisplayedData(),
'disabled' => $this->isDisabled(),
'type' => $this->getOption('type'),
));
}
}
4 changes: 2 additions & 2 deletions src/Symfony/Component/Form/IntegerField.php
Expand Up @@ -33,12 +33,12 @@ class IntegerField extends NumberField
*/
protected function configure()
{
parent::configure();

$this->addOption('precision', 0);

// Integer cast rounds towards 0, so do the same when displaying fractions
$this->addOption('rounding-mode', NumberToLocalizedStringTransformer::ROUND_DOWN);

parent::configure();
}

/**
Expand Down
22 changes: 12 additions & 10 deletions src/Symfony/Component/Form/NumberField.php
Expand Up @@ -20,11 +20,23 @@
*/
class NumberField extends InputField
{
/**
* {@inheritDoc}
*/
public function __construct($key, array $options = array())
{
$options['type'] = 'text';

parent::__construct($key, $options);
}

/**
* {@inheritDoc}
*/
protected function configure()
{
parent::configure();

// default precision is locale specific (usually around 3)
$this->addOption('precision');
$this->addOption('grouping', false);
Expand All @@ -36,14 +48,4 @@ protected function configure()
'rounding-mode' => $this->getOption('rounding-mode'),
)));
}

/**
* {@inheritDoc}
*/
public function getAttributes(array $attributes = array())
{
return array_merge(parent::getAttributes(), array(
'type' => 'text',
));
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/PasswordField.php
Expand Up @@ -34,6 +34,7 @@ protected function configure()
public function getAttributes()
{
return array_merge(parent::getAttributes(), array(
// override getDisplayedData() instead?
'value' => $this->getOption('always_empty') && !$this->isBound() ? '' : $this->getDisplayedData(),
'type' => 'password',
));
Expand Down
12 changes: 11 additions & 1 deletion src/Symfony/Component/Form/RadioField.php
Expand Up @@ -18,13 +18,23 @@
*/
class RadioField extends ToggleField
{
/**
* {@inheritDoc}
*/
public function __construct($key, array $options = array())
{
$options['type'] = 'radio';

parent::__construct($key, $options);
}

/**
* {@inheritDoc}
*/
public function getAttributes()
{
return array_merge(parent::getAttributes(), array(
'type' => 'radio',
// TODO: should getName() be overridden instead?
'name' => $this->getParent() ? $this->getParent()->getName() : $this->getName(),
));
}
Expand Down
11 changes: 10 additions & 1 deletion src/Symfony/Component/Form/TextField.php
Expand Up @@ -18,6 +18,16 @@
*/
class TextField extends InputField
{
/**
* {@inheritDoc}
*/
public function __construct($key, array $options = array())
{
$options['type'] = 'text';

parent::__construct($key, $options);
}

/**
* {@inheritDoc}
*/
Expand All @@ -34,7 +44,6 @@ protected function configure()
public function getAttributes()
{
return array_merge(parent::getAttributes(), array(
'type' => 'text',
'maxlength' => $this->getOption('max_length'),
));
}
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Form/ToggleField.php
Expand Up @@ -25,6 +25,8 @@ abstract class ToggleField extends InputField
*/
protected function configure()
{
parent::configure();

$this->addOption('value');
$this->addOption('label');

Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/Form/UrlField.php
Expand Up @@ -16,13 +16,15 @@
*
* @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
*/
class UrlField extends InputField
class UrlField extends TextField
{
/**
* {@inheritDoc}
*/
protected function configure()
{
parent::configure();

$this->addOption('default_protocol', 'http');
}

Expand Down
Expand Up @@ -6,4 +6,13 @@

class TestInputField extends InputField
{
/**
* {@inheritDoc}
*/
public function __construct($key, array $options = array())
{
$options['type'] = 'text';

parent::__construct($key, $options);
}
}
Expand Up @@ -6,4 +6,13 @@

class TestToggleField extends ToggleField
{
/**
* {@inheritDoc}
*/
public function __construct($key, array $options = array())
{
$options['type'] = 'checkbox';

parent::__construct($key, $options);
}
}

0 comments on commit 96a0bff

Please sign in to comment.