zendframework / zend-form

Form component from Zend Framework
BSD 3-Clause "New" or "Revised" License
69 stars 87 forks source link

Force input type also on attributes in Form\Factory #94

Open GeeH opened 8 years ago

GeeH commented 8 years ago

This issue has been moved from the zendframework repository as part of the bug migration program as outlined here - http://framework.zend.com/blog/2016-04-11-issue-closures.html


Original Issue: https://api.github.com/repos/zendframework/zendframework/issues/7172 User: @Martin-P Created On: 2015-01-31T00:36:54Z Updated At: 2015-11-06T20:07:59Z Body I think there should be some refactoring of adding elements to Zend\Form, because inputs can be represented by different classes (duplicate code).

$form = new \Zend\Form\Form();
$form->add(array(
    'name' => 'password',
    'type' => 'password',
));

This adds an <input type="password" /> which is represented by an instance of Zend\Form\Element\Password. When rendering it uses view helper Zend\Form\View\Helper\FormPassword.

However this code:

$form = new \Zend\Form\Form();
$form->add(array(
    'name' => 'password',
    'attributes' => array(
        'type' => 'password',
    ),
));

adds an <input type="password" /> which is represented by an instance of Zend\Form\Element. When rendering it uses view helper Zend\Form\View\Helper\FormInput.

Because there are 2 ways to create an <input type="password" /> and 2 different classes and 2 different view helpers are used for each way, whenever there is a bug in a form element it possibly needs fixing in 2 places like in #2613 and #7171.

In Zend\Form\Factory line 108 there is a check for the type key:

 $type = isset($spec['type']) ? $spec['type'] : 'Zend\Form\Element';

Ideally when adding an element to Zend\Form the check should also include checking the attributes array for a type key. With that check an <input type="password" /> will always be represented by an instance of Zend\Form\Element\Password and as a consequence will always be rendered by Zend\Form\View\Helper\FormPassword.

This also applies to all other types of <input />, but to explain the issue I only used <input type="password" /> here.


Comment

User: @jaapio Created On: 2015-01-31T10:31:32Z Updated At: 2015-01-31T10:31:32Z Body In some cases this could be useful. For example custom elements without a viewhelper could be rendered as a default element. But still have different behavior inside the element class.


Comment

User: @Martin-P Created On: 2015-01-31T11:43:35Z Updated At: 2015-01-31T11:43:35Z Body I can see the use for Zend\Form\Element, but as far as I can see now it would be better to declare it as an abstract class and only use instances of Zend\Form\Element\* for the predefined elements.


michalbundyra commented 4 years ago

This repository has been closed and moved to laminas/laminas-form; a new issue has been opened at https://github.com/laminas/laminas-form/issues/34.