File input problem

Hi,

I am using a snippet to give the ability to logged users to post and edit specific type resources via formit.

One of the fields of the formit form is a file input that is uploading as a tv field.

The problem is that when a user updates the resource and leave the file input field blank, when it saves, the blank input overwrite any previous inputs.

All I want is to make the snippet to ignore blank inputs only at the file input field.

If anynone can help. I am really stuck here.

Below is the code I use.

<?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;
Poko Loko
24 november 2015, 19:40
1 244
0

Comments: 4

Василий Наумкин
24 november 2015, 22:54
+1
If I understand you question correctly and your field with file named as «file», you can try to replace
if ($tvValue !== null) {
	$hook->setValue($field,$tvValue);
}
to
if ($field == 'file' && !empty($tvValue)) {
	$hook->setValue($field, $tvValue);
}
else {
	$hook->setValue($field, $tvValue);
}
Looks little ugly but must work.
    Poko Loko
    24 november 2015, 23:19
    0
    My file input is uploading with this formit call:

    <input id="IMAGE" class="custom-file-input" name="IMAGE" type="file" value="[[!+fi.IMAGE]]" maxlength="100000" />
    IMAGE is a tv field.
      Василий Наумкин
      25 november 2015, 06:44
      0
      Then you should try to check this name:
      if ($field == 'IMAGE' && !empty($tvValue)) {
      	$hook->setValue($field, $tvValue);
      }
      else {
      	$hook->setValue($field, $tvValue);
      }
        Poko Loko
        30 november 2015, 10:33
        0
        Good morning,

        sorry for the late answer, but I was away of the office for some days. I tried the code you suggest me, but unfortunatule it didn't work. I tried some other code also, didn't worked either.

        So I changed my approach and I am trying now to find a solution with ajaxupload.

        There are problems with ajaxupload also since it can upload images to resource but I can't retrieve them to the queue when I edit.
Log in or sign in to make new comments.