October: Custom Form Widgets With AJAX Handlers Won't Work As Expected Inside Repeaters

Created on 26 Feb 2018  路  14Comments  路  Source: octobercms/october

Adding a custom form widget, with AJAX handlers, inside a repeater should call that handler upon request. However the response throws an SystemException, even if the said custom widget uses the $this->getEventHandler('onXXX') method to access the handler.

In my case I was using the select2 to implement a custom "relation" style widget. Below is the partial that does the fetching request:

````html

````

And this ends up in a php function at the back end, in the style of:
php public function onFetchResultList() { return [ "results" => $this->forgeQueryResults(strip_tags(input("query"))) ]; }
At rendering, I insert the widget inside a data repeater with name contents_json and I give it a name magazine - for example. In which case $this->getEventHandler('onFetchResultList') renders to formContentsJsonForm1Magazine when the partial is rendered in the form.

Upon ajax requests, October returns the following exception in the response

"A widget with class name 'formContentsJsonForm1Magazine' has not been bound to the controller" on line 513 of /domains/example.domain/httpdocs/modules/backend/classes/Controller.php

I imagine that this should not happen under normal circumstances.


For whom it may concern: A quick workaround to this issue is to add another widget of the same kind at the top level form fields, then give it a name - f.e. magazine_ajax and set it to hidden, just to force the controller to load the widget class in the form. Then instead of using the getEventHandler() method in the partial, hardcode the name that the controller uses to identify that widget, in the javascript. For example, the above $.request code of the partial would turn to

javascript $.request('formMagazineAjax::onFetchResultList') ?>

October build

434

Completed Bug

All 14 comments

Is there any progress on this? Got same issue recently.

@iotch feel free to dig into it further to see if you can find out any more information on the issue.

Getting a similar error too - A widget with class name ... has not been bound to the controller on line 598 ...

I'm using a record finder widget inside a repeater field. The record finder works, until I try to search within it, or sort columns etc.

If I don't use the search/sort, and just select an option, then i can save the record and all is well. Strange thing is, if I go and edit that record then the search/sort works fine on that existing repeater field. Only errors on new fields.

Shame because kinda makes the whole implementation redundant, have looked for alternative solutions, but nothing seems to exist.

@bennothommo could you look into @pavsid's report above?

@pavsid PR #4434 may resolve this issue for you. Would you mind trying that fix out and let me know how you go?

@bennothommo Thanks, but unfortunately i'm getting the same error :( I think the fix only addresses child repeaters though, whereas my case doesn't have a child repeater. Note also that the record finder works ok (shows the 1st page of records) until you try to search, sort, or change page in the record finder list.

@bennothommo this issue probably still exists inside and outside of a child repeater with your fix because it's an issue with a detached formwidget (detached because it's calling from the context of a popup instead of the overall form) trying to call it's AJAX handlers when either the widget nested in a repeater isn't initialized or the alias for referencing that widget is incorrect.

@pavsid Thanks for letting me know.

@LukeTowers thanks for that. Will dig into it further :)

@pavsid Would you mind providing an example of your form as a PR to our test plugin? I think I have an idea as to where the issue may lie, but I would like to test it with a replica of your custom field setup so I can confirm.

@pavsid Would you mind providing an example of your form as a PR to our test plugin? I think I have an idea as to where the issue may lie, but I would like to test it with a replica of your custom field setup so I can confirm.

Sorry @bennothommo I've been away - do you still need this?

@pavsid not a problem. And yes please :)

Hi @bennothommo I've created a PR with my form config in. Sorry if I've done something wrong, it was my first PR ;)

Here's a screengrab of the issue in action...

2019-09-12_17-44-53

Forgot about test-plugin link: https://github.com/octoberrain/test-plugin/pull/78 馃槈

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dunets picture dunets  路  3Comments

m49n picture m49n  路  3Comments

ChVuagniaux picture ChVuagniaux  路  3Comments

mittultechnobrave picture mittultechnobrave  路  3Comments

d3monfiend picture d3monfiend  路  3Comments