Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
michalbundyra opened this issue Jan 15, 2020 · 0 comments
Open

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

michalbundyra opened this issue Jan 15, 2020 · 0 comments

Comments

@michalbundyra
Copy link
Member

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.



Originally posted by @GeeH at zendframework/zend-form#94

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant