Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

formCollection View Helper doesn't render name #86

Open
GeeH opened this issue Jun 28, 2016 · 6 comments
Open

formCollection View Helper doesn't render name #86

GeeH opened this issue Jun 28, 2016 · 6 comments

Comments

@GeeH
Copy link
Contributor

GeeH commented Jun 28, 2016

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/7687
User: @gregGit
Created On: 2016-03-14T16:20:39Z
Updated At: 2016-04-13T07:48:07Z
Body
When using form view helper, the formCollection view helper is used to render fieldset.
But form some reason that i don't understand this view helper doesn't set the name attribute in the fieldset (unset($attributes['name']); in the render Method).
I belive name is a valid html attributes for fieldset i don't understand why ZF2 never render this tag in a fieldset, and it can be usefull to have it in the DOM.

For test create an action like this :

  $fs = new \Zend\Form\Fieldset('my-fieldset');
        $fs->setAttribute('name', 'my-fieldset');
        $fs->add(array(
            'type' => 'text',
            'name' => 'text',
            'options' => array(
                'label' => 'The Text'
            )
        ));

        $fs->add(array(
            'type' => 'text',
            'name' => 'title',
            'options' => array(
                'label' => 'Blog Title'
            )
        ));


        $form=new \Zend\Form\Form('my-form');
        $form->add($fs);
        return new ViewModel(array('form' => $form));

and just " echo $this->form($form)" in the view.

No attribute name for the fieldset is rendered.


Comment

User: @froschdesign
Created On: 2016-04-13T07:48:07Z
Updated At: 2016-04-13T07:48:07Z
Body

I belive name is a valid html attributes for fieldset

It's only allowed in HTML5.


@hschletz
Copy link

hschletz commented Aug 9, 2018

Having a "name" attribute can be really useful -- it was added to the HTML5 standard for a reason. Being unable to use it is a real bummer, and the way the helper is implemented, there is no other choice than reimplementing it from scratch if the attribute is needed.

What about checking the doctype via the "Doctype" helper, or simply adding a flag?

@froschdesign
Copy link
Member

This part needs to be changed

if ($this->shouldWrap) {
$attributes = $element->getAttributes();
unset($attributes['name']);
$attributesString = $attributes ? ' ' . $this->createAttributesString($attributes) : '';

And the property $validTagAttributes must be added to Zend\Form\View\Helper\FormCollection class.

protected $validTagAttributes = [
    'name' => true,
];

@froschdesign
Copy link
Member

froschdesign commented Aug 9, 2018

Unit test for this case can be:

public function testRenderCollectionWithNameAttributeAndDoctypeHtml5()
{
    $this->helper->setDoctype(Doctype::HTML5);

    $form = $this->getForm();
    $collection = $form->get('colors');
    $collection->setAttribute('name', 'foo');

    $markup = $this->helper->render($collection);
    $this->assertContains('<fieldset name="foo">', $markup);
}

public function testRenderCollectionWithNameAttributeAndDoctypeXhtml1()
{
    $this->helper->setDoctype(Doctype::XHTML1_STRICT);

    $form = $this->getForm();
    $collection = $form->get('colors');
    $collection->setAttribute('name', 'foo');

    $markup = $this->helper->render($collection);
    $this->assertContains('<fieldset>', $markup);
}

@hschletz
Copy link

createAttributesString() already removes invalid attributes, via prepareAttributes(). There should be no need to hardcode removal of the "name" attribute.

#194 added methods to declare valid attributes, so we could add the "name" attribute manually if it wasn't forcefully removed.

However, I don't see the point in disallowing the attribute in the first place, other than compliance with doctypes other than HTML5. This only adds confusion when the attribute is ignored. Non-HTML5 developers may choose not to use it. Or allow/disallow it based on the doctype.

@froschdesign
Copy link
Member

@hschletz

createAttributesString() already removes invalid attributes, via prepareAttributes(). There should be no need to hardcode removal of the "name" attribute.

It is included in the current code, therefore I wrote: "This part needs to be changed".

#194 added methods to declare valid attributes...

Not needed here.

...compliance with doctypes other than HTML5.

This is the current behaviour of the entire component and there is no reason to change that.

@michalbundyra
Copy link
Member

This repository has been closed and moved to laminas/laminas-form; a new issue has been opened at laminas/laminas-form#42.

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

No branches or pull requests

4 participants