We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27716
    • 126 Posts
    Hi Guys,

    I’m using formit2resource for users to create docs from the front end.

    When a logged in web user creates a new doc it throws a 500 error. However when an admin creates a doc it doesnt.

    I have added the parent to webUser resource group.

    Cant understand why its throwing a 500.

    My snippet looks like this:

    <?php
    $doc = $modx->getObject('modResource',array('id'=>$hook->getValue('resource_id')));
    
    if (empty($doc)){
        $doc = $modx->newObject('modResource');
        $doc->set('createdby', $modx->user->get('id'));
    }
    else{
        $doc->set('editedby', $modx->user->get('id'));
    }
    
    $allFormFields = $hook->getValues(); 
    foreach ($allFormFields as $field=>$value)
    {
    
       if ($field !== 'spam' && $field !== 'resource_id'){
             $doc->set($field, $value);
        }
    }
    
    $pagetitle=$doc->get('pagetitle');
    $alias = $doc->cleanAlias($pagetitle);
    $doc->set('alias',$alias);
    $doc->set('published', '1');
    $doc->set('template', '3');
    $doc->set('isfolder', '0');
    $doc->save();
    
    foreach ($allFormFields as $field=>$value)
    {
        if ($tv = $modx->getObject('modTemplateVar', array ('name'=>$field)))
        {
            /* handles checkboxes & multiple selects elements */
            if (is_array($value)) {
                $featureInsert = array();
                while (list($featureValue, $featureItem) = each($value)) {
                    $featureInsert[count($featureInsert)] = $featureItem;
                }
                $value = implode('||',$featureInsert);
            }	
            $tv->setValue($doc->get('id'), $value);
            $tv->save();
        }
    }
    
    return true;


    The form looks like this
    [[!FormIt? &hooks=`spam,formit2resourceVideo,email,redirect` &preHooks=`resource2formit`
       &redirectTo=`257` &resource2formitfields=`parent,pagetitle,content,introtext` &emailTpl=`emailNotifyAnswer` &emailTo=`[email protected]`]]
    
    <h2>Video Details:</h2>
    <p>[[+fi.error.error_message]]</p>
    <form class="form" action="[[~[[*id]]]]" method="post">
        <input name="nospam:blank" type="hidden" />
        <input name="resource_id" type="hidden" value="[[+fi.id]]" />
        <label for="parent">
    <table width="200" border="0">
      <tr>
        <td align="right"><label for="pagetitle">
            Video Title:
            <span class="error">[[+fi.error.pagetitle]]</span>
        </label></td>
        <td><input style="width:270px;" id="pagetitle" name="pagetitle:required" type="text" value="[[+fi.pagetitle]]" />
      </td>
      </tr>
      <tr>
        <td align="right"><label for="introtext">
            Video Description:
            <span class="error">[[+fi.error.introtext]]</span>
        </label></td>
        <td><textarea style="width:280px;margin-left:3px; margin-top:3px;" id="introtext" cols="30" rows="3" name="introtext:required:stripTags">[[+fi.introtext]]</textarea></td>
      </tr>
      <tr>
        <td> </td>
        <td>
    <div class="form-buttons">
            <input style="margin-left:2px;" class="formbutton" type="submit" value="Next:" />
        </div></td>
      </tr>
    </table> 
            <span class="error">[[+fi.error.parent]]</span>
        </label>
        <input style="display:none;" id="parent" name="parent:required" type="text" value="259" />	
    	<br/>
    
        
    
    </form>


    Any guidance appreciated...

    Stuart
      • 27716
      • 126 Posts
      Up you go.
        • 3749
        • 24,544 Posts
        I’m not sure if this is your problem, but if you’re using the current version of FormIt, you need to specify the fields to validators like this and not put them in the form:

        [[!FormIt? &validate=
            `name:required,
             age:required:isNumber,
             department:required:ValidateDepartment`
             &customValidators=`ValidateDepartment=`accounting,sales` `
        ]]
        
          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
          • 27716
          • 126 Posts
          Hi Bob,

          I have a version setup now that is 100% tested and working with an admin user.

          So there is something about web-user accounts that is making it fail.

          Is there any user setting that would have to be set to allow them to create a doc with formit2resource.

          Guidance appreciated.

          Stuart
            • 3749
            • 24,544 Posts
            Yes, that could definitely be the problem. If a snippet performs manager actions (like writing to the DB), users need to have the proper permissions for that.

            I’m not sure, but I think they’ll have to have the correct permissions in both a Context Access ACL entry with a context of ’web’ and a Resource Group Access ACL entry, also with a context of ’web’.

            I’m not sure what permissions are necessary, but save_resource, database (in the context access policy), and save (in the resource group access policy) are probably required.

            Once that’s set up, you may end up hiding the resources from others in the front end, in which case you’ll need to add another context access ACL entry for ’members’ with a policy of load, list, and view.
              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
              • 27716
              • 126 Posts
              Hi Bob,

              Thanks for you help.

              In the end that turned out to be the issue.

              I had created an access policy but hadn’t associated it with the usergroup.

              Now working fine.

              Regards,

              Stuart
                • 3749
                • 24,544 Posts
                I’m glad you got it sorted. Thanks for reporting back. cool
                  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