Skip to content

Commit

Permalink
Added sfWidgetFormInputRead
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromemacias committed Dec 29, 2011
1 parent 3d88c1b commit 1337909
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/autoload/sfCoreAutoload.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @subpackage autoload
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @version SVN: $Id: sfCoreAutoload.class.php 32415 2011-03-30 16:09:00Z Kris.Wallsmith
$
*
*/
class sfCoreAutoload
{
Expand Down Expand Up @@ -501,6 +501,7 @@ static public function make()
'sfwidgetforminputfileeditable' => 'widget/sfWidgetFormInputFileEditable.class.php',
'sfwidgetforminputhidden' => 'widget/sfWidgetFormInputHidden.class.php',
'sfwidgetforminputpassword' => 'widget/sfWidgetFormInputPassword.class.php',
'sfwidgetforminputread' => 'widget/sfWidgetFormInputRead.class.php',
'sfwidgetforminputtext' => 'widget/sfWidgetFormInputText.class.php',
'sfwidgetformschema' => 'widget/sfWidgetFormSchema.class.php',
'sfwidgetformschemadecorator' => 'widget/sfWidgetFormSchemaDecorator.class.php',
Expand Down
71 changes: 71 additions & 0 deletions lib/widget/sfWidgetFormInputRead.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?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.
*/

/**
* sfWidgetFormInputRead represents an HTML text hidden input tag with readonly text input tag containing text to read.
*
* @package symfony
* @subpackage widget
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @version SVN: $Id$
*/
class sfWidgetFormInputRead extends sfWidgetFormInput
{
/**
* Configures the current widget.
*
* @param array $options An array of options
* @param array $attributes An array of default HTML attributes
*
* @see sfWidgetForm
*/
protected function configure($options = array(), $attributes = array())
{
parent::configure($options, $attributes);

$this->addOption('text');
$this->setOption('type', 'hidden');
}

/**
* Render the current widget
*
* @param string $name The element name
* @param string $value The this widget is checked if value is not null
* @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
* @param array $errors An array of errors for the field
*
* @return string An HTML tag string
*
* @see sfWidgetForm
*/
public function render($name, $value = null, $attributes = array(), $errors = array())
{
$attributes = array_merge($this->attributes, $attributes);
$this->attributes = array();

$tag = parent::render($name, $value, array(), $errors);

$style = 'border: 0;';
if (isset($attributes['style']))
{
$style .= ' '.$attributes['style'];
unset($attributes['style']);
}

return $tag.$this->renderTag('input', array_merge(array(
'type' => 'text',
'value' => $this->getOption('text', $value),
'readonly' => 'readonly',
'style' => $style,
), $attributes));
}
}

28 changes: 28 additions & 0 deletions test/unit/widget/sfWidgetFormInputReadTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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.
*/

require_once(dirname(__FILE__).'/../../bootstrap/unit.php');

$t = new lime_test(5);

$w = new sfWidgetFormInputRead();

// ->render()
$t->diag('->render()');
$t->is($w->render('foo'), '<input type="hidden" name="foo" id="foo" /><input type="text" readonly="readonly" style="border: 0;" />', '->render() renders the widget as HTML');
$t->is($w->render('foo', 'bar'), '<input type="hidden" name="foo" value="bar" id="foo" /><input type="text" value="bar" readonly="readonly" style="border: 0;" />', '->render() can take a value for the input');
$t->is($w->render('foo', '', array('class' => 'foobar', 'style' => 'width: 500px;')), '<input type="hidden" name="foo" value="" id="foo" /><input type="text" value="" readonly="readonly" style="border: 0; width: 500px;" class="foobar" />', '->render() can take HTML attributes as its third argument');

$w = new sfWidgetFormInputRead(array('text' => 'Read text'));
$t->is($w->render('foo', 'bar'), '<input type="hidden" name="foo" value="bar" id="foo" /><input type="text" value="Read text" readonly="readonly" style="border: 0;" />', '->render() can take a value for the input and another value for read input');

$w = new sfWidgetFormInputRead(array(), array('class' => 'foobar', 'style' => 'width: 500px;'));
$t->is($w->render('foo'), '<input type="hidden" name="foo" id="foo" /><input type="text" readonly="readonly" style="border: 0; width: 500px;" class="foobar" />', '__construct() can take default HTML attributes');
$t->is($w->render('foo', null, array('class' => 'barfoo')), '<input type="hidden" name="foo" id="foo" /><input type="text" readonly="readonly" style="border: 0;" class="barfoo" />', '->render() can override default attributes');

2 comments on commit 1337909

@GromNaN
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess a "readonly" option in sfWidgetFormInput would have been enough. This way you can make read-only any radio/checkbox/textarea ...

@jeromemacias
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The widget allow to set a different value for visible input, with default custom style.

I don't know if it can be usefull for many people.

Please sign in to comment.