2

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] => 
)
  • Try using camel case for your form ID, like so: <form id="adminForm">. Currently, you're setting it as adminform, with a lowercase f – Lodder Aug 15 at 13:36
  • @Lodder. I have tried with adminForm but no change unfortunately. I have also updated the above post with the change, thanks. – Irata Aug 15 at 13:48
  • 2
    Charlie's comment is confusing. It's name attribute that was adminform. The ID is query-form. You need to either pass the form to Joomla.submitform() or change the ID to adminForm. – Sharky Aug 15 at 13:58
  • @Sharky - Ahh yes, my bad. name – Lodder Aug 15 at 14:00
  • @Sharky, that worked, I have both Name and Id as adminForm and it is now working, thank you. I would have bet the house on me having tried that combination at sometime tonight but thinking about it I might have been all lower case. How would I pass the Form name to Joomla.submitform()? as i saw some of the examples had like id="fred-form" which must have been specified somewhere else. If you would like to post your reply as an answer I will accept it. – Irata Aug 15 at 14:28
1

"It's name attribute that was adminform. The ID is query-form. You need to either pass the form to Joomla.submitform() or change the ID to adminForm. " ...@Sharky

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.