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

ObjectSelect with "multiple" #215

Closed
ThaDafinser opened this issue Apr 15, 2013 · 16 comments
Closed

ObjectSelect with "multiple" #215

ThaDafinser opened this issue Apr 15, 2013 · 16 comments

Comments

@ThaDafinser
Copy link
Contributor

Hello,

the ObjectSelect works generally well with "multiple" select. (but i don't know if it's tested already)
You can add or remove multiple relations very well.

But when you want to unselect all selected in the form, where won't be a submit of the select field.
With no POST doctrine don't remove the relations.

Test case: Create a entry with one or more relation. Try to edit this entry and remove there all relations -> it won't remove the relations.

$this->add(array(
    'type' => 'DoctrineModule\Form\Element\ObjectSelect',
    'name' => 'modules',
    'attributes' => array(
        'id' => 'selectModules',
        'multiple' => true,
        'style' => 'width: 100%',

        'data-placeholder' => 'Modules'
    ),
    'options' => array(
        'object_manager' => $entityManager,
        'target_class' => 'LispMachine\Entity\Module',
        'property' => 'name'
    )
));
@Ocramius
Copy link
Member

Seems more like a hydrator problem to me. What is the data passed to the hydrator?

@ThaDafinser
Copy link
Contributor Author

@Ocramius When all selections are removed there won't be a submit of this field.

Create post of the complete form:

csrfToken 092889b838e323987de9f2c20db8736f
event[durationMinutes] 22
event[endDate] 2013-05-12 10:00:00
event[entry][description] asdfsadf
event[entry][id]
event[entry][language]
event[entry][modules][] 568
event[entry][tags][] 14
event[startDate] 2013-05-15 10:00:00

Update post -> remove all modules (unselect the selection from create)

csrfToken 3d7ed5904e54786c98cb08df6323bd15
event[durationMinutes] 22
event[endDate] 2013-05-12 10:00:00
event[entry][description] asdfsadf
event[entry][id] 7edbe08c-a65d-11e2-bfe3-005056967662
event[entry][language] de
event[entry][tags][] 14
event[startDate] 2013-05-15 10:00:00

The thing is that when using "single" there is always a post send, but not with multiple.

@Ocramius
Copy link
Member

@ThaDafinser I think this is a ZF2 issue that was solved recently. Can you please check?

@ThaDafinser
Copy link
Contributor Author

I'll try to check if i find it.

@Ocramius
Copy link
Member

News?

@ThaDafinser
Copy link
Contributor Author

@Ocramius
Sorry for the delay....what i found out:
Where is a bug in ZF2 (but it's more about the empty_option):
zendframework/zendframework#2761

In my case i dont use the empty_option, so this does not apply. The problem with empty_option is, that it will be an option and i don't won't this on a multi select....

The problem is only about the "mutliple" elements, single elements are fine (because single elements are always submitted). The POST is like written above, empty when nothing is selected.

The main problem is for the DoctrineObject to find out if the element is displayed/rendered on the form, when it's rendered and the POST is empty, associations could be removed....

But currently the DoctrineObject known nothing about the form elements .... :-/
And if you automatically would remove "not submitted" associations it would be VERY DANGEROUS....

Do you maybe got any idea?

@Ocramius
Copy link
Member

@ThaDafinser there's strategies to disallow removal - and no, the hydrator can't know of the form at all.

@ThaDafinser
Copy link
Contributor Author

@Ocramius this is my current "workaround" for each collection:

When the POST is empty and the collection is not, i just clear it.

//EDIT
$data = $request->getPost();
$dataEntry = $data['event']['entry'];
$entry = $event->getEntry();

if (count($entry->getTags()) > 0 && ! isset($dataEntry['tags']) && $form->get('event')->get('entry')->get('tags') !== null) {
    $entry->getTags()->clear();
}
$entityManager->flush();

The only way to automate this, would be a dependency to the form -> so the strategy know about which elements are set (and possibly not send).

//EDIT know the 3rd if, checks if the elements is around (if not, don't remove all relations...)

gencer pushed a commit to gencer/DoctrineModule that referenced this issue Apr 11, 2014
gencer pushed a commit to gencer/DoctrineModule that referenced this issue Apr 11, 2014
gencer pushed a commit to gencer/DoctrineModule that referenced this issue Apr 11, 2014
The global shard settings are configured through the normal connection
settings.
Refs doctrine#215
@Ocramius
Copy link
Member

@bakura10 is this supposed to be closed by bakura10/zf2@469ddd8 ?

@Ocramius Ocramius reopened this Apr 17, 2014
@Ocramius
Copy link
Member

Re-opening. @ThaDafinser I'm poking you again for updates just to see if @bakura10's PR actually changed anything.

@bakura10
Copy link
Member

@Ocramius , I'm not sure to understand. as far as I remember I'm not the author of the commit that was merged (well, I'm actually sure, I've never used multiple selects).

I don't remember exactly how Select mapped to Doctrine hydrator, but as far as I remember, you're right, that's a hydrator issue, and the collection must be cleared. I suppose the hydrator calls "removeElements" with the elements. So if the given array is empty, it should result in a clear in entity land. But once again, it's been a while I didn't use that :/.

@elchristo
Copy link

Hi, same behaviour here with ObjectMultiCheckbox and no element selected.
I've tried to use the "unselected_value" option (bakura10/zf2@469ddd8) which still writes a default value to the database. Instead I'd like to clear the collection and delete these table rows.

@bakura10 I think as well it's rather a problem of the hydrator.
I've figured out that the issue appears when the POST data contains an empty string for the multiple select element (when nothing selected). When I simply change this to an empty array everything is working fine.
Maybe an empty string should by simply interpretated as an empty array for the multiple select elements ?

@rootkowsky
Copy link

    /**
     * @var ArrayCollection
     * 
     * @ORM\ManyToMany(targetEntity="Posts\Entity\Tag", inversedBy="posts")
     * @ORM\JoinTable(name="post_tag",
     * joinColumns={@ORM\JoinColumn(name="post_id", referencedColumnName="id")},
     * inverseJoinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="id")}
     * )
     * @Annotation\Type("DoctrineModule\Form\Element\ObjectSelect")
     * @Annotation\Attributes({"multiple":true, "required":false})
     * @Annotation\Required(false)
     * @Annotation\AllowEmpty
     * @Annotation\Options({
     * "label":"Tags:",
     * "target_class":"Posts\Entity\Tag",
     * "required":"false"
     * })
     */
    protected $tags;

Works for me. Just type:

"required":"false"

in "Options" / "options"

@PoetikDragon
Copy link

Don't know why this is closed when its still an issue

@TomHAnderson
Copy link
Member

@PoetikDragon the author closed it.

@ThaDafinser
Copy link
Contributor Author

For me it's not an issue anymore / i don't need it anymore.

If you still need it, just open a new issue and refer to this one.

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

7 participants