Formit2resource problem

Hi, I build a site where a front-end user has the ability to post ads and edit them (or «delete» them).

I am using formit2resource, resource2formit. The problem is that recently after some update I made, formit2resource doesn't save the current resource but creates a new one!

For example, if I am savine a resource with id 200 after I edit this resource and post them (even If I make changes or not), it creates one new resource (201).

I am so stuck and I need to find a solution soon. If someone can help me...

Below is my code:

[[!FormIt? 
    &hooks=`formit2resource,redirect` 
    &preHooks=`resource2formit`
    &resource2formitfields=`template,resource_id,Active,Name,pagetitle,Paratiriseis,Sex,Race,Age,Weight,Pedigree,Microchip,Passport,Area,Phone,Email,content,IMAGE,IMAGE-2,IMAGE-3`
    &resId=`[[~[[*id]]]]`
    &redirectTo=`106`
    &validate=`
     Name:required
     pagetitle:required,
     Sex:required,
     Race:required,
     Age:required,
     Phone:required,
     Email:required,
     Pedigree:required,
     Area:required`
    &vTextRequired=`*Απαραίτητο πεδίο`
    &submitVar=`dog_input`
]]

<form class="form" action="[[~[[*id]]]]" method="post" name="dog_input" enctype='multipart/form-data' >
    <input name="resource_id" type="hidden" value="[[!+fi.id]]" />
    <input id="pagetitle" name="pagetitle" type="hidden" value="[[!+fi.Name]]" />  
    <input id="parent" name="parent" type="hidden" value="219" />  
    <input id="template" name="template" type="hidden" value="23" /> 
    <input id="published" name="published" type="hidden" value="1" /> 
    <input id="hidemenu" name="hidemenu" type="hidden" value="1" /> 
    <input id="active" name="active" type="hidden" value="YES" /> 
    <div id="column_all">
    <div class="column_dog2">
    <div class="column_dog">
    <label for="Name"><span class="error2">Όνομα σκύλου [[!+fi.error.Name]]</span></label>
    <div class="neces"></div>
    <input id="Name" class='input_4' name="Name" type="text" value="[[!+fi.Name]]" placeholder="ΟΝΟΜΑ ΣΚΥΛΟΥ"/>
    <label for="Sex"><span class="error2">Φύλλο [[!+fi.error.Sex]]</span></label>
    <div class="neces2"></div><select name="Sex" class="fancy_3">
            <option value="" [[!+fi.Sex:FormItIsSelected=``]]>ΕΠΙΛΕΞΤΕ ΦΥΛΟ</option>
            <option value="ΑΡΣΕΝΙΚΟ"  [[!+fi.Sex:FormItIsSelected=`ΑΡΣΕΝΙΚΟ`]]>ΑΡΣΕΝΙΚΟ</option>
            <option value="ΘΗΛΥΚΟ"  [[!+fi.Sex:FormItIsSelected=`ΘΗΛΥΚΟ`]]>ΘΗΛΥΚΟ</option>
    </select>
    <label for="Race"><span class="error2">Ράτσα [[!+fi.error.Race]]</span></label>
    <div class="neces2"></div><select name="Race" class="fancy_3">
           [[$RATSES_CLEAN]]
    </select>
    <label for="Age"><span class="error2">Ηλικία [[!+fi.error.Age]]</span></label>
    <div class="neces2"></div><select name="Age" class="fancy_3">
                    <option value="" [[!+fi.Age:FormItIsSelected=``]]>ΗΛΙΚΙΑ</option>
                    <option value="1"  [[!+fi.Age:FormItIsSelected=`1`]]>1</option>
                    <option value="2"  [[!+fi.Age:FormItIsSelected=`2`]]>2</option>
                    <option value="3"  [[!+fi.Age:FormItIsSelected=`3`]]>3</option>
                    <option value="4"  [[!+fi.Age:FormItIsSelected=`4`]]>4</option>
                    <option value="5"  [[!+fi.Age:FormItIsSelected=`5`]]>5</option>
                    <option value="6"  [[!+fi.Age:FormItIsSelected=`6`]]>6</option>
                    <option value="7"  [[!+fi.Age:FormItIsSelected=`7`]]>7</option>
                    <option value="8"  [[!+fi.Age:FormItIsSelected=`8`]]>8</option>
                    <option value="9"  [[!+fi.Age:FormItIsSelected=`9`]]>9</option>
                    <option value="10" [[!+fi.Age:FormItIsSelected=`10`]]>10</option>
                  </select>
     
     <label for="Weight"><span class="error2">Βάρος [[!+fi.error.Weight]]</span></label>
    <div class="not_neces"></div><input id="Weight" class='input_4' name="Weight" type="text" value="[[+fi.Weight]]" placeholder="ΒΑΡΟΣ"/>
    
     </div>
    <div class="column_dog">
    
    <label for="Pedigree"><span class="error2">Pedigree [[!+fi.error.Pedigree]]</span></label>
    <div class="neces2"></div><select name="Pedigree" class="fancy_3">
            <option value="" [[!+fi.Pedigree:FormItIsSelected=``]]>PEDIGREE</option>
            <option value="ΝΑΙ"  [[!+fi.Pedigree:FormItIsSelected=`ΝΑΙ`]]>ΝΑΙ</option>
            <option value="ΟΧΙ"  [[!+fi.Pedigree:FormItIsSelected=`ΟΧΙ`]]>ΟΧΙ</option>
    </select>

    <label for="Microchip"><span class="error2">Microchip [[!+fi.error.Microchip]]</span></label>
    <div class="not_neces"></div><select name="Microchip" class="fancy_3">
            <option value="" [[!+fi.Microchip:FormItIsSelected=``]]>MICROCHIP</option>
            <option value="ΝΑΙ"  [[!+fi.Microchip:FormItIsSelected=`ΝΑΙ`]]>ΝΑΙ</option>
            <option value="ΟΧΙ"  [[!+fi.Microchip:FormItIsSelected=`ΟΧΙ`]]>ΟΧΙ</option>
    </select>
     
    <label for="Passport"><span class="error2">Διαβατήριο [[!+fi.error.Passport]]</span></label>
   <div class="not_neces"></div><select name="Passport" class="fancy_3">
            <option value="" [[!+fi.Passport:FormItIsSelected=``]]>PASSPORT</option>
            <option value="ΝΑΙ"  [[!+fi.Passport:FormItIsSelected=`ΝΑΙ`]]>ΝΑΙ</option>
            <option value="ΟΧΙ"  [[!+fi.Passport:FormItIsSelected=`ΟΧΙ`]]>ΟΧΙ</option>
    </select>
     
     <label for="Phone"><span class="error2">Τηλέφωνο επικοινωνίας[[!+fi.error.Phone]]</span></label>
    <div class="neces"></div><input id="Phone" class='input_4' name="Phone:required" type="text" value="[[+fi.Phone]]" placeholder="ΤΗΛΕΦΩΝΟ"/>
    
    <label for="Email"><span class="error2">Email [[!+fi.error.Email]]</span></label>
    <div class="neces"></div><input id="Email" class='input_4' name="Email" type="text" value="[[+fi.Email]]" placeholder="E-MAIL"/>
    </div>
    <div class="column_dog2">
    <label for="Email"><span class="error2">Παρατηρήσεις [[!+fi.error.Paratiriseis]]</span></label>
    <textarea id="Paratiriseis" cols="89" rows="7" placeholder="Παρατηρήσεις" name="Paratiriseis">[[+fi.Paratiriseis]]</textarea>
    </div>
    </div>
    <div class="column_dog">
    <label for="Area"><span class="error2">Νομός [[!+fi.error.Area]]</span></label>
    <div class="neces2"></div><select name="Area" class="fancy_3">
                      <option value="" [[!+fi.Area:FormItIsSelected=``]]>ΕΠΙΛΕΞΤΕ ΝΟMO</option>
                      <option value="%" [[!+fi.Area:FormItIsSelected=`%`]]>ΕΜΦΑΝΙΣΗ ΟΛΩΝ</option>
                      <option value="ΑΙΤΩΛΟΑΚΑΡΝΑΝΙΑΣ"  [[!+fi.Area:FormItIsSelected=`ΑΙΤΩΛΟΑΚΑΡΝΑΝΙΑΣ`]]>ΑΙΤΩΛΟΑΚΑΡΝΑΝΙΑΣ</option>
                      <option value="ΑΡΓΟΛΙΔΑΣ"  [[!+fi.Area:FormItIsSelected=`ΑΡΓΟΛΙΔΑΣ`]]>ΑΡΓΟΛΙΔΑΣ</option>
                      <option value="ΑΡΚΑΔΙΑΣ"  [[!+fi.Area:FormItIsSelected=`ΑΡΚΑΔΙΑΣ`]]>ΑΡΚΑΔΙΑΣ</option>
                      <option value="ΑΡΤΑΣ"  [[!+fi.Area:FormItIsSelected=`ΑΡΤΑΣ`]]>ΑΡΤΑΣ</option>
                      <option value="ΑΤΤΙΚΗΣ"  [[!+fi.Area:FormItIsSelected=`ΑΤΤΙΚΗΣ`]]>ΑΤΤΙΚΗΣ</option>
                      <option value="ΑΧΑΪΑΣ"  [[!+fi.Area:FormItIsSelected=`ΑΧΑΪΑΣ`]]>ΑΧΑΪΑΣ</option>
                      <option value="ΒΟΙΩΤΙΑΣ"  [[!+fi.Area:FormItIsSelected=`ΒΟΙΩΤΙΑΣ`]]>ΒΟΙΩΤΙΑΣ</option>
                      <option value="ΓΡΕΒΕΝΩΝ"  [[!+fi.Area:FormItIsSelected=`ΓΡΕΒΕΝΩΝ`]]>ΓΡΕΒΕΝΩΝ</option>
                      <option value="ΔΡΑΜΑΣ"  [[!+fi.Area:FormItIsSelected=`ΔΡΑΜΑΣ`]]>ΔΡΑΜΑΣ</option>
                      <option value="ΔΩΔΕΚΑΝΗΣΟΥ"  [[!+fi.Area:FormItIsSelected=`ΔΩΔΕΚΑΝΗΣΟΥ`]]>ΔΩΔΕΚΑΝΗΣΟΥ</option>
                      <option value="ΕΒΡΟΥ"  [[!+fi.Area:FormItIsSelected=`ΕΒΡΟΥ`]]>ΕΒΡΟΥ</option>
                      <option value="ΕΥΒΟΙΑΣ"  [[!+fi.Area:FormItIsSelected=`ΕΥΒΟΙΑΣ`]]>ΕΥΒΟΙΑΣ</option>
                      <option value="ΕΥΡΥΤΑΝΙΑΣ"  [[!+fi.Area:FormItIsSelected=`ΕΥΡΥΤΑΝΙΑΣ`]]>ΕΥΡΥΤΑΝΙΑΣ</option>
                      <option value="ΖΑΚΥΝΘΟΥ"  [[!+fi.Area:FormItIsSelected=`ΖΑΚΥΝΘΟΥ`]]>ΖΑΚΥΝΘΟΥ</option>
                      <option value="ΗΛΕΙΑΣ"  [[!+fi.Area:FormItIsSelected=`ΗΛΕΙΑΣ`]]>ΗΛΕΙΑΣ</option>
                      <option value="ΗΜΑΘΙΑΣ"  [[!+fi.Area:FormItIsSelected=`ΗΜΑΘΙΑΣ`]]>ΗΜΑΘΙΑΣ</option>
                      <option value="ΗΡΑΚΛΕΙΟΥ"  [[!+fi.Area:FormItIsSelected=`ΗΡΑΚΛΕΙΟΥ`]]>ΗΡΑΚΛΕΙΟΥ</option>
                      <option value="ΘΕΣΠΡΩΤΙΑΣ"  [[!+fi.Area:FormItIsSelected=`ΘΕΣΠΡΩΤΙΑΣ`]]>ΘΕΣΠΡΩΤΙΑΣ</option>
                      <option value="ΘΕΣΣΑΛΟΝΙΚΗΣ"  [[!+fi.Area:FormItIsSelected=`ΘΕΣΣΑΛΟΝΙΚΗΣ`]]>ΘΕΣΣΑΛΟΝΙΚΗΣ</option>
                      <option value="ΙΩΑΝΝΙΝΩΝ"  [[!+fi.Area:FormItIsSelected=`ΙΩΑΝΝΙΝΩΝ`]]>ΙΩΑΝΝΙΝΩΝ</option>
                      <option value="ΚΑΒΑΛΑΣ"  [[!+fi.Area:FormItIsSelected=`ΚΑΒΑΛΑΣ`]]>ΚΑΒΑΛΑΣ</option>
                      <option value="ΚΑΡΔΙΤΣΗΣ"  [[!+fi.Area:FormItIsSelected=`ΚΑΡΔΙΤΣΗΣ`]]>ΚΑΡΔΙΤΣΗΣ</option>
                      <option value="ΚΑΣΤΟΡΙΑΣ"  [[!+fi.Area:FormItIsSelected=`ΚΑΣΤΟΡΙΑΣ`]]>ΚΑΣΤΟΡΙΑΣ</option>
                      <option value="ΚΕΡΚΥΡΑΣ"  [[!+fi.Area:FormItIsSelected=`ΚΕΡΚΥΡΑΣ`]]>ΚΕΡΚΥΡΑΣ</option>
                      <option value="ΚΕΦΑΛΛΗΝΙΑΣ"  [[!+fi.Area:FormItIsSelected=`ΚΕΦΑΛΛΗΝΙΑΣ`]]>ΚΕΦΑΛΛΗΝΙΑΣ</option>
                      <option value="ΚΙΛΚΙΣ"  [[!+fi.Area:FormItIsSelected=`ΚΙΛΚΙΣ`]]>ΚΙΛΚΙΣ</option>
                      <option value="ΚΟΖΑΝΗΣ"  [[!+fi.Area:FormItIsSelected=`ΚΟΖΑΝΗΣ`]]>ΚΟΖΑΝΗΣ</option>
                      <option value="ΚΟΡΙΝΘΙΑΣ"  [[!+fi.Area:FormItIsSelected=`ΚΟΡΙΝΘΙΑΣ`]]>ΚΟΡΙΝΘΙΑΣ</option>
                      <option value="ΚΥΚΛΑΔΩΝ"  [[!+fi.Area:FormItIsSelected=`ΚΥΚΛΑΔΩΝ`]]>ΚΥΚΛΑΔΩΝ</option>
                      <option value="ΛΑΚΩΝΙΑΣ"  [[!+fi.Area:FormItIsSelected=`ΛΑΚΩΝΙΑΣ`]]>ΛΑΚΩΝΙΑΣ</option>
                      <option value="ΛΑΡΙΣΑΣ"  [[!+fi.Area:FormItIsSelected=`ΛΑΡΙΣΑΣ`]]>ΛΑΡΙΣΑΣ</option>
                      <option value="ΛΑΣΙΘΙΟΥ"  [[!+fi.Area:FormItIsSelected=`ΛΑΣΙΘΙΟΥ<`]]>ΛΑΣΙΘΙΟΥ</option>
                      <option value="ΛΕΣΒΟΥ"  [[!+fi.Area:FormItIsSelected=`ΛΕΣΒΟΥ`]]>ΛΕΣΒΟΥ</option>
                      <option value="ΛΕΥΚΑΔΟΣ"  [[!+fi.Area:FormItIsSelected=`ΛΕΥΚΑΔΟΣ`]]>ΛΕΥΚΑΔΟΣ</option>
                      <option value="ΜΑΓΝΗΣΙΑΣ"  [[!+fi.Area:FormItIsSelected=`ΜΑΓΝΗΣΙΑΣ`]]>ΜΑΓΝΗΣΙΑΣ</option>
                      <option value="ΜΕΣΣΗΝΙΑΣ"  [[!+fi.Area:FormItIsSelected=`ΜΕΣΣΗΝΙΑΣ`]]>ΜΕΣΣΗΝΙΑΣ</option>
                      <option value="ΞΑΝΘΗΣ"  [[!+fi.Area:FormItIsSelected=`ΞΑΝΘΗΣ`]]>ΞΑΝΘΗΣ</option>
                      <option value="ΠΕΛΛΑΣ"  [[!+fi.Area:FormItIsSelected=`ΠΕΛΛΑΣ`]]>ΠΕΛΛΑΣ</option>
                      <option value="ΠΙΕΡΙΑΣ"  [[!+fi.Area:FormItIsSelected=`ΠΙΕΡΙΑΣ`]]>ΠΙΕΡΙΑΣ</option>
                      <option value="ΠΡΕΒΕΖΗΣ"  [[!+fi.Area:FormItIsSelected=`ΠΡΕΒΕΖΗΣ`]]>ΠΡΕΒΕΖΗΣ</option>
                      <option value="ΡΕΘΥΜΝΗΣ"  [[!+fi.Area:FormItIsSelected=`ΡΕΘΥΜΝΗΣ`]]>ΡΕΘΥΜΝΗΣ</option>
                      <option value="ΡΟΔΟΠΗΣ"  [[!+fi.Area:FormItIsSelected=`ΡΟΔΟΠΗΣ`]]>ΡΟΔΟΠΗΣ</option>
                      <option value="ΣΑΜΟΥ"  [[!+fi.Area:FormItIsSelected=`ΣΑΜΟΥ`]]>ΣΑΜΟΥ</option>
                      <option value="ΣΕΡΡΩΝ"  [[!+fi.Area:FormItIsSelected=`ΣΕΡΡΩΝ`]]>ΣΕΡΡΩΝ</option>
                      <option value="ΤΡΙΚΑΛΩΝ"  [[!+fi.Area:FormItIsSelected=`ΤΡΙΚΑΛΩΝ`]]>ΤΡΙΚΑΛΩΝ</option>
                      <option value="ΦΘΙΩΤΙΔΑΣ"  [[!+fi.Area:FormItIsSelected=`ΦΘΙΩΤΙΔΑΣ`]]>ΦΘΙΩΤΙΔΑΣ</option>
                      <option value="ΦΛΩΡΙΝΗΣ"  [[!+fi.Area:FormItIsSelected=`ΦΛΩΡΙΝΗΣ`]]>ΦΛΩΡΙΝΗΣ</option>
                      <option value="ΦΩΚΙΔΑΣ"  [[!+fi.Area:FormItIsSelected=`ΦΩΚΙΔΑΣ`]]>ΦΩΚΙΔΑΣ</option>
                      <option value="ΧΑΛΚΙΔΙΚΗΣ"  [[!+fi.Area:FormItIsSelected=`ΧΑΛΚΙΔΙΚΗΣ`]]>ΧΑΛΚΙΔΙΚΗΣ</option>
                      <option value="ΧΑΝΙΩΝ"  [[!+fi.Area:FormItIsSelected=`ΧΑΝΙΩΝ`]]>ΧΑΝΙΩΝ</option>
                      <option value="ΧΙΟΥ"  [[!+fi.Area:FormItIsSelected=`ΧΙΟΥ`]]>ΧΙΟΥ</option>
                  </select>
    
    
    <label for="Email"><span class="error2">Φωτογραφίες (αρχεία jpg, gif, png)[[+fi.error.nomination_file]]</span></label>
    <div class="photo_input">
    <input id="IMAGE" class="custom-file-input" name="IMAGE" type="file" value="[[!+fi.IMAGE]]" maxlength="100000" />
    <div id="ad_image_small" ><img src="[[+fi.IMAGE:phpthumbof=`w=115&h=90&zc=t`]]" /></div>
    </div>
    <div class="photo_input">
    <input id="IMAGE" class="custom-file-input" name="IMAGE-2" type="file" value="[[!+fi.IMAGE-2]]" maxlength="100000" />
    <div id="ad_image_small" ><img src="[[+fi.IMAGE-2:phpthumbof=`w=115&h=90&zc=t`]]" /></div>
    </div>
    <div class="photo_input">
    <input id="IMAGE" class="custom-file-input" name="IMAGE-3" type="file" value="[[!+fi.IMAGE-3]]" maxlength="100000" />
    <div id="ad_image_small" ><img src="[[+fi.IMAGE-3:phpthumbof=`w=115&h=90&zc=t`]]" /></div>
    </div>
    </span>
    </div>
    </div>
    <p>[[!+fi.error.error_message]]</p>
        <input type="submit" id="button_7" name="dog_input" value="Αποθήκευση αλλαγών"/>
</form>


Formit2resource
<?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);
    }
 
   //we need to parse the title into the alias
   if($field == 'pagetitle'){
      //replace spaces with -
      $alias = preg_replace('/[\' \']/' , '-' , $value);
 
      //remove non alpha and a common injection string 
      $alias = preg_replace('/[^a-z0-9\.\x0B\-]/i' , '' , $alias);
 
                                   //this is the standard revo regexp
                                   // \0\x0B\t\n\r\f\a&=+%#<>"~:`@\?\[\]\{\}\|\^'\
   }
}
 // now set the alias
 $doc->set('alias', $alias);
  
$doc->set('template', $template);
$isnew = $doc->isNew();
 
if ($doc->save()) {
    if ($isnew) {
        $doc->set('alias', $doc->get('alias' . '-' . $doc->get('id')));
        $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();
    }
}

$url = $modx->makeUrl(84);

////////////formit2file part ////////////////////////////////////

// initialize output;
$output = true;
// valid extensions
$ext_array = array('jpg', 'jpeg', 'gif', 'png');
  
// create unique path for this form submission
//$uploadpath = 'assets/img-annonceurs/';
  
// you can create some logic to automatically
// generate some type of folder structure here.
// the path that you specify will automatically
// be created by the script if it doesn't already
// exist.
  
// EXAMPLE:
// this would put all file uploads into a new,
// unique folder every day.
$uploadpath = 'assets/images/users/'.date('Y-m-d').'/';
  
// get full path to unique folder
$target_path = $modx->config['base_path'] . $uploadpath;
  
// get uploaded file names:
$submittedfiles = array_keys($_FILES);
  
// loop through files
foreach ($submittedfiles as $sf) {

    // Get Filename and make sure its good.
    $filename = basename( $_FILES[$sf]['name'] );
  
    // Get file's extension
    $ext = pathinfo($filename, PATHINFO_EXTENSION);
    $ext = mb_strtolower($ext); // case insensitive
  
    // is the file name empty (no file uploaded)
    if($filename != '') {
          
        // is this the right type of file?
        if(in_array($ext, $ext_array)) {
      
            // change name to: resource_id-input_name.ext
            $filename = $doc->get('id').'-'.$sf.'.'.$ext;


            // full path to new file
            $myTarget = $target_path . $filename;
			//URL path
			$urlPath = $uploadpath . $filename;
              
            // create directory to move file into if it doesn't exist
            if(!is_dir($target_path)) {
                mkdir($target_path, 0755, true);
                } else {
                $modx->log(modX::LOG_LEVEL_ERROR, 'Upload:Path already exists' );    
                }

            // is the file moved to the proper folder successfully?
            if(move_uploaded_file($_FILES[$sf]['tmp_name'], $myTarget)) {
              // place file path to tv
             if (!$doc->setTVValue($sf, $urlPath)) $modx->log(xPDO::LOG_LEVEL_ERROR, 'Upload:Saving TV error');

                // set the permissions on the file
                if (!chmod($myTarget, 0644)) { /*some debug function*/ }

            } else {
                // File not uploaded
                $errorMsg = 'There was a problem uploading the file.';
                $hook->addError($sf, $errorMsg);
                $output = false; // generate submission error
            }
          
        } else {
            // File type not allowed
            $errorMsg = 'Type of file not allowed.';
            $hook->addError($sf, $errorMsg);
            $output = false; // generate submission error
        }
      
    // if no file, don't error, but return blank
    } else {
       // $hook->setValue($sf, '');  // dont need that
	   $doc->setTVValue($sf, '');    // insert empty value to TV
    }
  
}

$modx->cacheManager->refresh(); // suggested by gallenkamp
return $output;


resource2formit
<?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 {
if ($field == "[[+fi.IMAGE]]" & $docarray[$field] !== null) { 
                  $hook->setValue($field,$docarray[$field]);
              }
            }    
        }
    }
  
    //$errorMsg = '<pre>'.print_r($docarray,true).'</pre>';  
    //$hook->addError('error_message',$errorMsg);  
}  
return true;
Poko Loko
10 октября 2015, 21:12
modx.pro
2 783
+1

Комментарии: 3

Василий Наумкин
11 октября 2015, 06:07
1
+1
As I see, resource2formit checks resource id in the $_GET array, but you specifying it in the parameter &resId. And you specifying not an id, but link to current resource.

So, you need to remove &resId from FormIt call and load edit form with an id at the $_GET. For example, your link for create a new resource can be
http://mysite.com/ad/resource
And for update it must be:
http://mysite.com/ad/resource?resId=15

Than you need to fix your snippet resource2formit. Just replace
else {
		if ($field == "[[+fi.IMAGE]]" & $docarray[$field] !== null) { 
			$hook->setValue($field,$docarray[$field]);
		}
	}

to
else {
		$hook->setValue($field,$docarray[$field]);
	}

After this your snippet will set [[!+fi.id]] placeholder and you will be able to update an existing resource.
    Poko Loko
    13 октября 2015, 00:06
    0
    Wow!!! So fast response and solved the problem. Thank you!!!

    Now I am facing the last problem with my form. The file upload procedure. The file upload to a tv works perfect. The problem is when you want to edit the resource.

    Offcourse resource2formit cannot retrieve the image file (or it can?), but that's not the main problem. The problem is that if the user leaves the file field empty, then when he post the form it replaces the tv field with empty entry, so the image is gone!

    What I need is that if you dont upload a new file to the tv, to keep the old one.

    Thanx again for your previous help. Greetings from Greece!

    PS I Pointed by mistake the dislike button trying to hit like. Sorry.
    Авторизуйтесь или зарегистрируйтесь, чтобы оставлять комментарии.
    3