We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 15796
    • 24 Posts
    I must be doing something wrong here because I can’t seem to get it to work.
    I just want a page where users can edit a resource.
    In this particular case I want User X to be able to edit page 12.
    so on page 12 I call a certain chunk (via Personalize, but that part works).
    That chunk contains
    [[FormIt? &hooks=`Formit2Resource` &preHooks=`Resource2Formit` &Resource2FormitFields=`parent,pagetitle,content` &resId=`[[~[[*id]]]]`]]
    
    <h2>[[~[[*id]]]]</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">
            Parent:
            <span class="error">[[+fi.error.parent]]</span>
        </label>
        <input id="parent" name="parent:required" type="text" value="[[+fi.parent]]" />	
    	<br/>
        <label for="pagetitle">
            Pagetitle:
            <span class="error">[[+fi.error.pagetitle]]</span>
        </label>
        <input id="pagetitle" name="pagetitle:required" type="text" value="[[+fi.pagetitle]]" />
        <br/>
    	<label for="content">
            Content:
            <span class="error">[[+fi.error.content]]</span>
        </label>
        <textarea id="content" cols="55" rows="7" name="content:required:stripTags">[[+fi.content]]</textarea>
    <br class="clear" />
    <div class="form-buttons">
            <input type="submit" value="Create Resource" />
        </div>
    </form>

    and
    Formit2Resource
    is
    <?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);
        }
    }
    
    
    $doc->set('template', '1');
    $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;

    and Resource2Formit is
    <?php
    if (isset($_GET['resId'])){
        if ($doc=$modx->getObject('modResource',array('id'=>$_GET['resId']))){
            $docarray=$doc->toArray();
            $fields = explode(',',$scriptProperties['Resource2FormitFields']);
            $fields[] = 'id';
    
            foreach ($fields as $field){
           
                if ($doc->getFieldName($field) === null) {
                    /* if field isnt defined, look for TV value */
                    $tvValue = $doc->getTVValue($field);
                    if ($tvValue !== null) {
                        $hook->setValue($field,$tvValue);
                    }
                } else {
                    /* otherwise get field value */
                    $hook->setValue($field,$docarray[$field]);
                }    
            }
        }
    
        $errorMsg = '<pre>'.print_r($docarray,true).'</pre>';  
        $hook->addError('error_message',$errorMsg);  
    }
    return true;

    I do get the form but it’s empty and clicking the Create Resource just empties the form and refreshes the page but nothing gets created.
    • I would presume that like any other dynamic snippet (i.e. one that has to do its thing on every page request, such as form processing) the snippet should be called uncached... or is this not necessary with Formit/Revo?
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 15796
        • 24 Posts
        hm, you’re right. Thanks for pointing that out! wink
        Ok, so now the Create Resource button actually creates a resource.
        But I still don’t get the values in the form...
          • 15796
          • 24 Posts
          Can anyone point me in the correct direction?
          I’m running Formit 1.5.2 rc2 on ModX Revolution 2.0.6-pl (traditional)
            • 15796
            • 24 Posts
            damnit, I’m really stuck here.
            are there any alternative ways? by not using formit for instance?
              • 18453
              • 66 Posts
              Can someone help Kheos out?
              Is it possible to include a subscribe form so that the user that makes the resource can edit its own resource?
                • 15796
                • 24 Posts
                apparantly not
                strange as I assumed these examples would work, but in my case they don’t
                  • 19153
                  • 53 Posts
                  So could the user then upload an image to a tv? Has anyone tried this?

                  I am very interested in making a site where users can upload photos to new resources from the frontend.
                    • 4172
                    • 5,888 Posts
                    Quote from: messyp at Jan 25, 2011, 12:44 PM

                    So could the user then upload an image to a tv? Has anyone tried this?

                    I am very interested in making a site where users can upload photos to new resources from the frontend.

                    did that some times ago. Have todo a research, if you are interested on that.
                      -------------------------------

                      you can buy me a beer, if you like MIGX

                      http://webcmsolutions.de/migx.html

                      Thanks!
                      • 9995
                      • 1,613 Posts
                      Is there anyone with an example where i can log into the site and just edit a page?
                      or make screendump.
                        Evolution user, I like the back-end speed and simplicity smiley