We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 49407
    • 159 Posts
    I had a problem with this before but I solved it by removing the href from my link that submits the form. I tried replacing the <a> with a submit button, but it's still not invoking the hook.

    I hope this amounts to something very simple.

    Here is my custom hook snippet...

    $modx->log(modX::LOG_LEVEL_ERROR, 'THIS HOOK IS BEING INVOKED!', '', 'chronicleThis Hook');
    
    $prefix = 'xyz_';
    $packagename = 'chronicleThis';
    $classname = 'Grows';
    
    $formFields = $hook->getValues();
    
    if ( isset( $formFields['grow_id'] )) {
        $modx->log(modX::LOG_LEVEL_ERROR, 'grow_id: '.$formFields['grow_id'], '', 'chronicleThis Hook');
        $where = array( 'grow_id', $formFields['grow_id'] );
        unset( $formFields['grow_id'] );
    }
    
    $packagepath = $modx->getOption($packagename . '.core_path', NULL, $modx->getOption('core_path') . 'components/' . $packagename . '/');
    $modelpath = $packagepath . 'model/';
    
    $modx->addPackage($packagename, $modelpath, $prefix);
    
    if ( is_array( $where )) {
        $dataobject = $modx->getObject($classname, $where);
        if (empty($dataobject)) {
            $dataobject = $modx->newObject($classname);
        }
    } else {
        $dataobject = $modx->newObject($classname);
    }
    
    if (!is_object($dataobject) || !($dataobject instanceof xPDOObject)) {
        $errorMsg = 'Failed to create object of type: ' . $classname;
        $hook->addError('error_message', $errorMsg);
        $modx->log(modX::LOG_LEVEL_ERROR, $errorMsg, '', 'chronicleThis Hook');
        //return false;
    }
    
    foreach ($formFields as $field => $value) {
        if (!in_array($field, $removeFields)) {
            if (in_array($field, $arrayFields)) {
                switch ($arrayFormat) {
                    case 'json':
                        $value = json_encode($value);
                        break;
                    case 'csv' :
                    default :
                        $value = implode(',', $value);
                        break;
                }
            }
            $dataobject->set($field, $value);
        }
    }
    
    if (!$dataobject->save()) {
        $errorMsg = 'Failed to save object of type: ' . $classname;
        $hook->addError('error_message', $errorMsg);
        $modx->log(modX::LOG_LEVEL_ERROR, $errorMsg, '', 'chronicleThis Hook');
        return false;
    }
    return true;
    


    and here is the chunk with the FormIt call...

    [[!FormIt? 
        &hooks=`chronicleThis`
    ]]
    
    <form name="form" id="form" action="[[~[[*id]]]]" method="post" class="form">
        <input type="hidden" name="owner_id" id="owner_id" value="[[!+modx.user.id]]">
        [[!+grow.id.field]]
        <li class="fieldOuterBox">
            <label for="title" class="inputLabel">Title</label>
            <input type="text" name="title" id="title" class="txtInput" value="[[!+grow.title]]" />
            <span class="applyError">[[!+fi.error.title]]</span>
        </li>
    
        <li class="fieldOuterBox">
            <label for="description" class="inputLabel">Description</label>
            <textarea cols="40" rows="3" name="description" id="description" class="txtInput">[[!+grow.description]]</textarea>
            <span class="applyError">[[!+fi.error.description]]</span>
        </li>
    
        <li class="fieldOuterBox">
            <label for="image" class="inputLabel">Leader Image</label>
            <input type="text" name="leader_url" id="leader_url" value="[[!+grow.leader]]" />
            <a name="submit" class="btnSmall" onclick="leaderUploadPopup();" href>Select Image</a>
            <br class="clear" />
            <label for="image" class="inputLabel"> </label>
            <img src="[[!+grow.leader]]">
            <span class="applyError">[[!+fi.error.tags]]</span>
        </li>
    
        <li class="fieldOuterBox">
            <label for="tags" class="inputLabel">Tags</label>
            <input type="text" name="tags" id="tags" class="txtInput" value="[[!+grow.tags]]" />
            <span class="applyError">[[!+fi.error.tags]]</span>
        </li>
    
        <li class="fieldOuterBox">
            <label for="publish_on" class="inputLabel">Publish On (optional)</label>
            <input type="text" name="publish_on" id="publish_on" class="txtInput" value="[[!+grow.publish.date]]" />
            <span class="applyError">[[!+fi.error.publish_on]]</span>
        </li>
    
        <li class="fieldOuterBox">
            <label for="publish" class="inputLabel"> </label>
            <input type="checkbox" name="published" id="published" class="checkboxInput" value="1" [[!+grow.published]]/>
            <span class="">Published</span>
        </li>
    
        <li class="fieldOuterBox vWrapper">
            <label for="submit" class="inputLabel"> </label>
            <a name="submit" class="btnSmall" onclick="form.submit();">Save Grow</a>
        </li>
    
    </form>
    
    <div class="cropBox">
        <div>
            
        </div>
    </div>
    
    <script>
        $( "#publish_on" ).datepicker({
            inline: true
        });
    </script>
    


    A list of things that would cause FormIt fail to process the hook (that I currently know of):

    1) A previous hook failing. [There are no other hooks in the case so this can't be a cause.]
    2) The <a> tag has an href in it. [There is not an href in my <a> tag this time.]
    3) The hook is not called by the proper name. [This has been checked over and over. Not a cause this time.]

    Please add any other causes you know of because I am not sure there are any. Caching? I flushed the cache and am not caching my snippets or chunks. I make it a habit to always use !.

    I've been staring at this code for about 4 hours now. Maybe a second set of eyes can see something I am missing.

    This question has been answered by whitebyte. See the first response.

    [ed. note: aaronkent last edited this post 9 years, 4 months ago.]
      • 49529
      • 196 Posts
      <a name="submit" class="btnSmall" onclick="form.submit();">Save Grow</a>

      Why this? Try to make standart submission
      <input type="submit">

      and check this out
        • 49407
        • 159 Posts
        Quote from: whitebyte at Dec 24, 2014, 03:22 PM
        <a name="submit" class="btnSmall" onclick="form.submit();">Save Grow</a>

        Why this? Try to make standart submission
        <input type="submit">

        and check this out

        whitebyte, I have tried it with a submit button. It still does not process the hook. I mentioned this in my original post.

        Quote from: aaronkent at Dec 24, 2014, 03:00 PM
        I had a problem with this before but I solved it by removing the href from my link that submits the form. I tried replacing the <a> with a submit button, but it's still not invoking the hook.

        In fact, I am not using an <a> tag at all until I get FormIt to invoke the hook with a regular submit button, however, it should not be a problem because I am triggering the form submit when I do use the <a> tag and it works with an <a> tag on all my other forms.

        The cause must be something else.
          • 49529
          • 196 Posts
          I thought by "button" you meant
          <button>Submit</button>
          ,
          not input type=submit

          Is this the only form on the page? You may try to add &submitVar parameter to your FormIt call to be sure that Formit will process the proper form.
          Does is process any hooks at all? Will it redirect if you call it with "redirect" built-in hook, for example?
            • 49407
            • 159 Posts
            No, whitebyte, it's not processing any hooks at all.

            I tried this...

            [[!FormIt?
                &hooks=`redirect`
                &redirectTo=`1`
            ]]
            
            <form name="form" id="form" action="[[~[[*id]]]]" method="post" class="form">
                <input type="hidden" name="owner_id" id="owner_id" value="[[!+modx.user.id]]">
                [[!+grow.id.field]]
                <li class="fieldOuterBox">
                    <label for="title" class="inputLabel">Title</label>
                    <input type="text" name="title" id="title" class="txtInput" value="[[!+grow.title]]" />
                    <span class="applyError">[[!+fi.error.title]]</span>
                </li>
            
                <li class="fieldOuterBox">
                    <label for="description" class="inputLabel">Description</label>
                    <textarea cols="40" rows="3" name="description" id="description" class="txtInput">[[!+grow.description]]</textarea>
                    <span class="applyError">[[!+fi.error.description]]</span>
                </li>
            
                <li class="fieldOuterBox">
                    <label for="image" class="inputLabel">Leader Image</label>
                    <input type="text" name="leader_url" id="leader_url" value="[[!+grow.leader]]" />
                    <a name="submit" class="btnSmall" onclick="leaderUploadPopup();" href>Select Image</a>
                    <br class="clear" />
                    <label for="image" class="inputLabel"> </label>
                    <img src="[[!+grow.leader]]">
                    <span class="applyError">[[!+fi.error.tags]]</span>
                </li>
            
                <li class="fieldOuterBox">
                    <label for="tags" class="inputLabel">Tags</label>
                    <input type="text" name="tags" id="tags" class="txtInput" value="[[!+grow.tags]]" />
                    <span class="applyError">[[!+fi.error.tags]]</span>
                </li>
            
                <li class="fieldOuterBox">
                    <label for="publish_on" class="inputLabel">Publish On (optional)</label>
                    <input type="text" name="publish_on" id="publish_on" class="txtInput" value="[[!+grow.publish.date]]" />
                    <span class="applyError">[[!+fi.error.publish_on]]</span>
                </li>
            
                <li class="fieldOuterBox">
                    <label for="publish" class="inputLabel"> </label>
                    <input type="checkbox" name="published" id="published" class="checkboxInput" value="1" [[!+grow.published]]/>
                    <span class="">Published</span>
                </li>
            
                <li class="fieldOuterBox vWrapper">
                    <label for="submit" class="inputLabel"> </label>
                    <input type="submit" name="submit" id="submit" value="Save Grow" />
                </li>
            
            </form>
            
            <script>
                $( "#publish_on" ).datepicker({
                    inline: true
                });
            </script>
            


            Not redirecting. Not processing hooks at all.

            Yes, this is the only form on the page.

            How can I test the &submitVar parameter? Do I just set it to the value of a field in my form or what?
              • 49529
              • 196 Posts
              How can I test the &submitVar parameter?


              You should add a name to you submitting input or button:
              <input type="submit" name="somename">

              , and add
              &submitVar=`somename`
              

              to yuor formit call.

              You may also double-check if Formit extra installed at all.
                • 49407
                • 159 Posts
                I set &submitVar=`form` because that's the name of my form.

                Still not processing the built-in redirect hook or my custom hook.

                I reinstalled formit also and still it is ignoring the hooks.
                  • 49529
                  • 196 Posts
                  According to FormIt manual (http://rtfm.modx.com/extras/revo/formit), submitVar should be set for submitting element, not form itself, though I didn't test it. You may try to add some very minimal form to a new blank page and test if FormIt works at all. I'm pretty sure that the problem isn't with your particular hook, but with form submitting in general.

                  Any records in MODX error log AND php error log?
                    • 49407
                    • 159 Posts
                    No, whitebyte, no errors in PHP log or in the MODX error logs.

                    I changed the submitVar to &submitVar=`Save Grow` which is the value of the submit input. Still not processing the hook, or maybe not processing the submit. I am not sure what is happening besides absolutely nothing.

                    FormIt is working on other pages just fine as it's supposed to. No reason to build yet another form to test FormIt.

                    I think the form is not submitting but I can't see a reason for that at it's current configuration.

                    I have a valid submit input, inside the form. Only this single form on the page.

                    • discuss.answer
                      • 49529
                      • 196 Posts
                      It looks really strange. Could you provide a link to your site?