Strange new problem. When I edit a resource, with the folowing Formit, it saves as a new resource instead of updating the current one!!! I also get this error message at error log:
(ERROR @ /index.php) modResource: Attempt to set NOT NULL field template to NULL
HTML
[[!FormIt?
&hooks=`spam,formit2resource,redirect`
&preHooks=`resource2formit`
&resource2formitfields=`parent,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=`
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="nospam:blank" type="hidden" />
<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>
<br>
<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="ΒΑΡΟΣ"/>
<br>
</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>
<br>
<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>
<br>
<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="ΤΗΛΕΦΩΝΟ"/>
<br>
<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>
<br>
<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>
<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;
[ed. note: gordonas last edited this post 8 years, 11 months ago.]