Jump to content

Using file inputfield on frontend: reset value


Robin S
 Share

Recommended Posts

I'm trying Soma's gist for uploading a file from the frontend using the core File inputfield.

After submit the file uploads just fine and I get the success message, but I also get an error when the form is re-rendered.

Warning: filemtime(): stat failed for .../site/assets/files/1228/test-image.jpg in ...\wire\core\Pagefile.php on line 324

I believe the inputfield is trying to display information relating to the previously uploaded file, like it does in Admin. The reason this file can't be found at that location is because page 1228 is the page where the form is, whereas the file has been inserted into a different page.

There's a line in the gist that looks like it's intended to solve this:

// reset value before rendering the form again to prevent PW errors trying render files (as in admin)
$form->get("myimages")->value = ''; // reset field value

But maybe this isn't the right way to reset the value of a File inputfield?

Edit: actually, the form isn't re-rendered on a successful submit, so not sure what is causing the submitted file to be looked for in this wrong location. :unsure:

Link to comment
Share on other sites

Found some helpful discussion related to this issue here.

The value of the inputfield must be set to a Pageimages object for an existing page. By default $page is used (which isn't what is wanted for this example) but it can be changed to a different page.

In the context of Soma's gist the fix is to add the following at the end of the "success" section:

$form->get("myimages")->attr("value", new Pageimages($uploadpage));

So the complete success section is

if(!count($form->getErrors())){
    // if no error occured
    // create new page and save values
    $uploadpage = new Page();
    $uploadpage->template = "upload-entry";
    $uploadpage->parent = $pages->get("/upload-api/");
    // add title/name and make it unique with time and uniqid
    $uploadpage->title = date("d-m-Y H:i:s") . " - " . uniqid();
    $uploadpage->addStatus(Page::statusUnpublished);
    $uploadpage->save();
    // save uploaded files to new page and remove temp files
    $files = explode("|",$form->get("myimages")->value);
    foreach($files as $file){
        if($file && file_exists($upload_path . $file)){
            $uploadpage->myimages->add($upload_path . $file);
            unlink($upload_path . $file);
        }
    }
    $uploadpage->save();
    $form->get("myimages")->attr("value", new Pageimages($uploadpage));
    $sent = true;
    // or better do a redirect here before showing thank you text.
}
  • Like 2
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...