I am receiving
TypeError: form is null
in the browser console when I select a custom button added to a form using JToolbarHelper.
I can insert the button to any other component as is and it will submit so I suspect there is something in the way I have created my Form, but after nearly 4 hours I haven't been able to figure it out.
I have tried to create a minimal form for a component (default.php)
defined('_JEXEC') or die;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Factory;
$form = JForm::getInstance('query', JPATH_COMPONENT . '/models/forms/query.xml');
?>
<form
action="<?php echo JRoute::_('index.php?option=com_accentis&view=query'); ?>"
method="post" enctype="multipart/form-data" name="adminForm" id="query-form" >
<?php echo $form->renderFieldset('query'); ?>
<input type="hidden" name="task" value=""/>
<input type="hidden" name="view" value="query" />
<?php echo JHtml::_('form.token'); ?>
</form>
My line to add the custom button is (view.html.php)
JToolbarHelper::custom('query.loadData', "" , "" , 'Load Data', false);
In the console I get this message:
TypeError: form is null 2 core-uncompressed.js:66:4 submitform http://maralinga.test/media/system/js/core-uncompressed.js?0586eb80516116b3723cefdbfa5e045f:66 submitbutton http://maralinga.test/media/system/js/core-uncompressed.js?0586eb80516116b3723cefdbfa5e045f:99 onclick http://maralinga.test/administrator/index.php?option=com_accentis&view=query:1
And around line 66 in the script is where I crash and burn.
* Generic submit form
*
* @param {String} task The given task
* @param {node} form The form element
* @param {bool} validate The form element
*
* @returns {void}
*/
Joomla.submitform = function(task, form, validate) {
if (!form) {
form = document.getElementById('adminForm');
}
if (task) {
form.task.value = task; <===Line 66
}
I am thinking that the form variable is not getting set correctly and when it gets to line 66 it can't do whatever it wants to do.
I suspect it is something with the way I am creating an instance of my form with the $form = JForm::getInstance('query', JPATH_COMPONENT . '/models/forms/query.xml');
or in my form definition params.
And I am not sure if it matters, but this is the form object.
Joomla\CMS\Form\Form Object
(
[data:protected] => Joomla\Registry\Registry Object
(
[data:protected] => stdClass Object
(
)
[initialized:protected] =>
[separator] => .
)
[errors:protected] => Array
(
)
[name:protected] => query
[options:protected] => Array
(
[control] =>
)
[xml:protected] => SimpleXMLElement Object
(
[fieldset] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => query
[label] => COM_ACCENTIS_FIELDSET_QUERY
)
[field] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => searchId
[type] => text
[label] => COM_ACCENTIS_FIELD_SEARCHID_LABEL
[description] => COM_ACCENTIS_FIELD_SEARCHID_DESC
[labelclass] => control-label
)
[0] =>
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => feature
[type] => list
[label] => COM_ACCENTIS_FIELD_FEATURE_LABEL
[description] => COM_ACCENTIS_FIELD_FEATURE_DESC
[labelclass] => control-label
)
[option] => Array
(
[0] => Inventory
[1] => Sales Report
)
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => params
[type] => text
[multiple] => true
[formsource] => administrator/components/com_accentis/models/forms/queryparams.xml
[label] => COM_ACCENTIS_FIELD_PARAMS_LABEL
[description] => COM_ACCENTIS_FIELD_PARAMS_DESC
[labelclass] => control-label
)
[0] =>
)
)
)
)
[repeat] =>
)
<form id="adminForm">
. Currently, you're setting it asadminform
, with a lowercasef
name
attribute that wasadminform
. The ID isquery-form
. You need to either pass the form toJoomla.submitform()
or change the ID toadminForm
.name