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

[Hydrator][ZF2] Dynamically adding new objects #263

Closed
JordyvA opened this issue May 29, 2013 · 1 comment
Closed

[Hydrator][ZF2] Dynamically adding new objects #263

JordyvA opened this issue May 29, 2013 · 1 comment

Comments

@JordyvA
Copy link

JordyvA commented May 29, 2013

I’ve a problem with my collections. I’ve followed the example tutorial (https://github.com/doctrine/DoctrineModule/blob/master/docs/hydrator.md#a-complete-example-using-zendform) and have succesfully made a Blogpost and Tag entity. In my controller I’ve an add action and edit action. My add and edit views:

echo $this->formElement($blogpost->get('addTag'));
$tag = $blogpost->get('tags');

?>
<fieldset id="tagFieldset">
    <?php
        echo $this->formCollection($tag);
    ?>
</fieldset>

The view has a button for adding tags dynamically (like in this example: http://zf2.readthedocs.org/en/latest/modules/zend.form.collections.html#adding-new-elements-dynamically), which works fine in the edit-action. When adding a new blogpost with 2 tags at the same time, it only adds 1 tag in the database (the count option is set to 1. When I increase this number, it is able to add tags according to the option). When I edit that blogpost and add 2 tags (total of three), 2 tags are added to the database.

When debugging the addTags() function, the parameter holds only 1 Tag object at the add-action. In the edit-action it holds all the new tags that have been added (in my case, 2 new tags).

My blogpostFieldset look likes:

$this->add(array(
    'name' => 'addTag',
    'type' => 'button',
    'options' => array(
        'label' => 'Add more tags',
    ),
    'attributes' => array(
        'id' => 'addTag'
    )
));

$tagFieldset = new TagFieldset($serviceManager);
$this->add(array(
    'type'    => 'Zend\Form\Element\Collection',
    'name'    => 'tags',
    'options' => array(
        'count'           => 1,
        'target_element' => $tagFieldset,
        'should_create_template' => true,
        'allow_add' => true,
    )
));

And my javascript:

$('#addTag').click(function(e) {
        var currentCount = $('form > #tagFieldset > label').length;
        alert(currentCount);
        //Set datatemplate (generated by the arraycollection)
        var template = $('form > #tagFieldset > span').data('template');

        //Replace the template to other addressfieldset
        template = template.replace(/__index__/g, currentCount);

        //Set new fieldset
        $('form > #tagFieldset').append(template);
    });

It renders my templates correctly. Works fine in my edit-action, but my add-action only add 1 tag. I’ve no idea why. Maybe that the add-action doesn’t recognize the dynamically created fields?

@Ocramius
Copy link
Member

I'm closing this - we're unable to provide support on such very localized questions.

Please insulate the problem first, then report it as issue once confirmed and when you have a simplified example that does not involve view helpers and js.

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

2 participants