Skip to content
This repository has been archived by the owner on Mar 17, 2023. It is now read-only.

Problem with manually setting value #34

Open
andrzejdziekonski opened this issue Sep 22, 2014 · 8 comments
Open

Problem with manually setting value #34

andrzejdziekonski opened this issue Sep 22, 2014 · 8 comments

Comments

@andrzejdziekonski
Copy link

When i try to manually (javascript depending on some other user actions in my form view) set entity value in my view via:

[...]
$('.zenstruck-ajax-entity').select2('data',{id:foo, text:bar});

after saving from i get:
Warning: Illegal string offset 'id' in [...]/vendor/zenstruck/form-bundle/Zenstruck/Bundle/FormBundle/Form/Type/AjaxEntityType.php line 63

@andrzejdziekonski
Copy link
Author

I found out it happens only when i trigger click event on 'Add' button (opening collection subform)

$('.form-collection-add').trigger('click');

then i set the value (as posted above) and after saving form i get the error.

@kbond
Copy link
Owner

kbond commented Sep 22, 2014

I will look into this. Is there a zenstruck_ajax_entity form type in your subform?

@andrzejdziekonski
Copy link
Author

Yes, of course. My declaration:

->add('example', 'zenstruck_ajax_entity', array(
                'placeholder' => '...',
                'label' => '...',
                'multiple' => false,
                'class' => 'MyBundle:Entity',
                'property' => 'title',
                'url' => $this->ajax_route,
                'required' => false))

setting it up works like a charm but there must be some major difference between triggering click action on 'Add' button and just clicking it (in this usual way everything is fine).

To make my problem 100% clear. I got some selectable component and depending on selects (multiple) i 'open' desired amount of collection subforms and fill it with data. So i:

  1. trigger click action on '.form-collection-add' btn
$('.form-collection-add').trigger('click');
  1. fill subform fields with my custom data (including passing id+text to select2 components)
//after passing other values to subform
$('#formId .zenstruck-ajax-entity').select2('data',{id:foo, text:bar});

Everything looks OK (filling subforms with data), selected entity on select2 looks like i did it on my own but saving form fails. Do u got any suggestions?

@kbond
Copy link
Owner

kbond commented Sep 22, 2014

I am going to try and re-create.

@kbond
Copy link
Owner

kbond commented Sep 23, 2014

Hmm, I just tried re-creating your issue and couldn't.

My recreation is here: https://github.com/kbond/sandbox/tree/form-34

I have an Article entity that can have one or more Link entity's. The Link entity has an zenstruck_ajax_entity for an Author entity.

What I tested:

  1. When editing an Article, for the the "Links" subform, I clicked "Add"
  2. In the chrome console, I entered $('#s2id_sandbox_appbundle_articletype_links_2_author').select2('data', { id: 1, text: 'Kevin' }) (An Author exists with id=1, name=kevin
  3. The select2 updated correctly
  4. Click "Save"
  5. Success

@andrzejdziekonski
Copy link
Author

Could you in step 1. do: $('#YourFormId .form-collection-add).trigger('click') ? When manualy i click 'Add' there is no problem for me as well. The thing i want to achieve is opening subform via javascript

@kbond
Copy link
Owner

kbond commented Sep 23, 2014

Ok, I tried that but it still worked as expected.

@kbond
Copy link
Owner

kbond commented Sep 23, 2014

Could you do a var_dump of $view->vars['value'] on the first line of this function: https://github.com/kbond/ZenstruckFormBundle/blob/master/Form/Type/AjaxEntityType.php#L47

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

No branches or pull requests

2 participants