Jump to content

InputFieldFile in Forms, it's just not working.


Robguy
 Share

Recommended Posts

Hey guys, 

Love PW except the forms which are grinding my hopes and dreams away.

I'm rendering a form with this call

$images = $modules->get("InputfieldFile");
$images->label = " ";
$images->description = "Upload photo/s (max 2mb)";
$images->attr("name+id", 'myimages');
$images->attr("class", 'images-class');
$images->destinationPath = $this->config->paths->root . 'tmp_uploads/';
$images->extensions = "jpg jpeg gif png";
$images->maxFilesize = 2; // 2mb
$images->maxFiles = 2;
$form->append($images);
 
Now I expect it to render a form that can take two uploads AND can remove one of the uploads. It doesn't seem to work like this.
 
Firstly I needed to modify the output so I created an afterHook to modify this, just removing all the spans basically not important.
 
Further along I decided to create a hook which replaced the ___renderUpload function so that I can play around with the output and how it positions itself.
 
This threw errors at me for calling methods such as 
 
$attrs = $this->getAttributes();
          $this->getAttributesString($attrs)
 
So I gave up on that as well.
 
Now I realised that a possible reason (hopefully, really really hopefully) I can't remove images is because the method __renderList isn't being executed because 
 
$this->value
 
is failing at 
 
if(!$value) return '';
 
inside __renderList
 
Now I just need to ask how can I enable the removal of images and change the output to my own markup.
 
I have thought about moving it to /site/modules and play around with it there but it still won't solve the problem of removing images.
 
Thanks in advance,
Link to comment
Share on other sites

  • 2 weeks later...

I'm not entirely sure I know how to answer all your questions here because it sounds like you may trying to do things with InputfieldFile is may not necessarily be designed for. If that's the case, you may be better off extending it as a new PHP class, or copying its code to new module, rather than trying to achieve these all with hooks. Another thing I'm not sure about is whether you are talking about a front-end or back-end context. If dealing with a front-end context, the ajax uploading will not work. In some cases you may have to disable it on back-end use too (see the commented line below for how to do that). InputfieldFile is really meant to be used in combination with FieldtypeFile and within the page editor. But it is possible to use it outside of that context. Though you will still need to supply it a Page object so it has a known storage location, but the given Page doesn't need to have a files field. Meaning, it can be any Page, even an admin one. Here's an example of how you might use InputfieldFile on it's own:

// page the file will be stored with (doesn't have to have a files field)
$myPage = $pages->get('/path/to/your/page/');

// optionally set the name of existing file(s) that will be present (or leave it blank)
$myFiles = array(
  $myPage->filesManager->path . 'myfile.txt'
);

// file extensions you allow
$myExts = 'pdf csv txt';

// --------------------------------------------
// create the form
$form = wire('modules')->get('InputfieldForm');

// create the files field
$f = wire('modules')->get('InputfieldFile');
$f->name = 'my_files';
$f->label = 'My Files';
$f->extensions = $myExts;
$f->overwrite = true;
// $f->noAjax = true; // uncomment if necessary
$pagefiles = new Pagefiles($myPage); 
foreach($myFiles as $filename) {
  $pagefile = new Pagefile($pagefiles, $filename);
  $pagefiles->add($pagefile);
}
$f->attr('value', $pagefiles);
$form->add($f);

// add a submit button
$f = wire('modules')->get('InputfieldSubmit');
$f->attr('name', 'submit_form');
$form->add($f);

// -------------------------------------------
// process the form
if(wire('input')->post('submit_form')) {
  $form->processInput(wire('input')->post);
  $pagefiles = $form->get('my_files')->value;
  foreach($pagefiles as $pagefile) {
    echo "<p>Your file: $pagefile->url</p>";
  }
}
  • Like 9
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...