Issues

ZF-10205: Zend_Form_Element_Note to allow arbatary text in forms

Description

Can Zend/Form/Element/Note.php be added to allow for arbatary text to be added in forms. Example of its use would be to allow a Username to be displayed as non-editable text along side editable text boxes such as Email/Password/Name but have decorators still applied.

class Zend_Form_Element_Note extends Zend_Form_Element_Xhtml
{
public $helper = 'formNote';
}

Comments

Isn't this just a textbox with ...

$element->setAttrib('disabled', 'disabled');

Or is this something else?

not really, there are many cases where a text box disabled would still not be appropriate.

Sorry, I didn't really say what I meant to.

It was more a question. What output did you want the element to generate? It's just not very clear what the new element is going to achieve in terms of output.

It would make sense to implement this new form element, as displaying a value only can't be solved with decorators.

I have attached the suggested implementation with the associated unit test as a patch.

@Richard Tuin ??I have attached the suggested implementation with the associated unit test as a patch.??

You forgot something: Your note element loses the value after sending the form!

Here's an example to reproduce:


// View
$view = new Zend_View();
$view->setEncoding('utf-8');
$view->doctype('XHTML1_STRICT');

// Form
$form = new Zend_Form();

// Info text
$form->addElement('note', 'info', array(
    'value' => '

If you are already a registered customer, please sign in.

', )); // Button $form->addElement('submit', 'submit', array( 'label' => 'Send', )); if (isset($_POST['submit']) && $form->isValid($_POST)) { // ... } echo $form->render($view);

The solution: override the method "isValid".


/**
 * Element to show an HTML note
 *
 * @category   Zend
 * @package    Zend_Form
 * @subpackage Element
 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id$
 */
class Zend_Form_Element_Note extends Zend_Form_Element_Xhtml
{
    /**
     * Default form view helper to use for rendering
     * @var string
     */
    public $helper = 'formNote';
    
    /**
     * Validate element value (pseudo)
     *
     * There is no need to reset the value
     *
     * @param  mixed $value Is always ignored
     * @param  mixed $context Is always ignored
     * @return boolean Returns always TRUE
     */
    public function isValid($value, $context = null)
    {
        return true;
    }
}

I received these files from Kaiuwe, who cannot upload them atm.

@Kai Uwe Thank you for your correction.

As for now, no new components will be added to ZF1. I'll will be introduced in ZF 2.0. Changing the fix version to next major.

A 2.0 version of this component should be developed.

New version:


/**
 * Ignore flag (used when retrieving values at form level)
 * @var bool
 */
protected $_ignore = true;

Fixed on trunk (25208) and release-1.12 (25209)

Thanks to Richard and Kai!