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 have created the schema. I'm using the following code as a hook for FormIt...

    $prefix = 'xyz_';
    $packagename = 'chroniclethis';
    $classname = 'Grows';
    
    $packagepath = $modx->getOption($packagename . '.core_path', NULL, $modx->getOption('core_path') . 'components/' . $packagename . '/');
    $modelpath = $packagepath . 'model/';
    
    $modx->addPackage($packagename, $modelpath, $prefix);
    
    $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;
    }
    
    $formFields = $hook->getValues();
    
    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;


    Here is the chunk...

    [[!FormIt? &hooks=`chronicleThis`]]
    
    <form name="form" action="[[~[[*id]]]]" method="post" class="form">
        <input type="hidden" name="owner_id" value="[[!+modx.user.id]]">
        <li class="fieldOuterBox">
            <label for="title" class="inputLabel">Title</label>
            <input type="text" name="title" id="title" class="txtInput" value="[[!+fi.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">[[!+fi.description]]</textarea>
            <span class="applyError">[[!+fi.error.description]]</span>
        </li>
    
        <li class="fieldOuterBox">
            <label for="image" class="inputLabel">Leader Image</label>
            <a name="submit" class="btnSmall" onclick="leaderUploadPopup();" href>Select Image</a>
            <br class="clear" />
            <label for="image" class="inputLabel"> </label>
            <img class="hidden">
            <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="[[!+fi.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="[[!+fi.publish_on]]" />
            <span class="applyError">[[!+fi.error.publish_on]]</span>
        </li>
    
        <li class="fieldOuterBox">
            <label for="publish" class="inputLabel"> </label>
            <input type="checkbox" name="publish" id="publish" class="checkboxInput" value="1"/>
            <span class="">Published</span>
            <span class="applyError">[[!+fi.error.publish]]</span>
        </li>
    
        <li class="fieldOuterBox vWrapper">
            <label for="submit" class="inputLabel"> </label>
            <a name="submit" class="btnSmall" onclick="form.submit();" href>Save Grow</a>
        </li>
    
    </form>
    
    <div class="cropBox">
        <div>
            <form>
                
            </form>
        </div>
    </div>
    
    <script>
        $( "#publish_on" ).datepicker({
            inline: true
        });
    </script>


    No error is being logged. Nothing is being inserted in the database.

    How could there not be an error if the form is not being inserted into the database?

    I have checked all the field names several times and they are all matching the database fields.

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

    [ed. note: aaronkent last edited this post 9 years, 5 months ago.]
      • 3749
      • 24,544 Posts
      My guess is that your problem is here and all fields are failing one of these tests, possibly due to the prefixes:

      foreach ($formFields as $field => $value) {
          if (!in_array($field, $removeFields)) {
              if (in_array($field, $arrayFields)) {


      If I'm right, you would not be getting an error message because the object is saved successfully but without the field set.

      I would try something like this for starters:

      foreach ($formFields as $field => $value) {
          echo '<br><br>FIELD: ' . $field . ' --  VALUE: ' . $value;
          if (!in_array($field, $removeFields)) {
              echo '<br> ---- Passed Test 1';
              if (in_array($field, $arrayFields)) {
                  echo '<br> ---- Passed Test 2';
      
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 49407
        • 159 Posts
        Well, I tried echoing success for each step, even copypasta your code there. Just sends me to resource 1 like it was doing before. I don't have any redirects hooked and there are still no errors either in modx or in the php error logs.

        This is truly a wtf moment. I can't see any reason it would even be redirecting to resource 1. I am using the [[~[[*id]]]] as the form action. Which is how you're supposed to do it, right? Maybe I should be using http://domain/[[~[[*id]]]] as the form action?

          • 49407
          • 159 Posts
          Ok now I'm really stumped. I changed the echoes to...

          foreach ($formFields as $field => $value) {
              $modx->log(modX::LOG_LEVEL_ERROR, 'FIELD: ' . $field . ' --  VALUE: ' . $value, '', 'chronicleThis Hook');
              if (!in_array($field, $removeFields)) {
                  $modx->log(modX::LOG_LEVEL_ERROR, '---- Passed Test 1', '', 'chronicleThis Hook');
                  if (in_array($field, $arrayFields)) {
                      $modx->log(modX::LOG_LEVEL_ERROR, '---- Passed Test 2', '', 'chronicleThis Hook');


          No output on the modx error logs.

          That means that the hook isn't being hooked or the foreach loop is not being processed.

          I have the hook name absolutely correct.

          If it were failing this...

          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;
          }


          I would be getting error output.

          This makes no logical sense to me. Everyone I have look at the code says it should work. Could it be a bug in modx? If so, how could you even know what that bug is without any output? This is crazy.
            • 49407
            • 159 Posts
            Quote from: BobRay at Dec 19, 2014, 08:07 PM
            My guess is that your problem is here and all fields are failing one of these tests, possibly due to the prefixes:

            What prefixes are you referring to? Only the table has a prefix, not the fields.
              • 49529
              • 196 Posts
              aaronkent, redirect to resource 1 may occur on 404 or 503 errors (if you didn't change them in settings). Try to check your firebug (or what you use) after redirect.
                • 3749
                • 24,544 Posts
                The placeholder prefixes, but that wouldn't prevent the hook from executing. Is this the only hook?
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                  • 49407
                  • 159 Posts
                  Yes, Bob, this is the only hook. I still can't get any different result. It's acting like the hook isn't firing but it did when I used it to create the schema.
                    • 3749
                    • 24,544 Posts
                    Is the chunk tag used to insert the chunk cached or uncached?

                    Where is the chunk tag (i.e., template, TV, resource content field)?
                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting
                      • 49407
                      • 159 Posts
                      Neither the snippet or the chunk are cached. Both are called with [[! and the chunk is called from a resource that is published.