Skip to content

Instantly share code, notes, and snippets.

@jacmaes
Forked from somatonic/inputfieldfile-form.php
Last active July 12, 2023 07:58
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jacmaes/6691946 to your computer and use it in GitHub Desktop.
Save jacmaes/6691946 to your computer and use it in GitHub Desktop.
Processwire: Front-end upload form example using ProcessWire Inputfields #pw
<?php
/**
* Front-end upload form example
* using ProcessWire Inputfields
*/
$sent = false;
$upload_path = $config->paths->assets . "files/.tmp_uploads/";
$form = $modules->get("InputfieldForm");
$form->attr("action", "./");
$form->attr("name", "uploadform");
$field = $modules->get("InputfieldText");
$field->label = "Fullname";
$field->required = 1;
$field->attr("name+id",'fullname');
$form->add($field);
$field = $modules->get("InputfieldEmail");
$field->label = "Email";
$field->required = 1;
$field->attr("name+id",'email');
$form->add($field);
$field = $modules->get("InputfieldTextarea");
$field->label = "Message";
$field->required = 1;
$field->attr("name+id",'message');
$form->add($field);
$field = $modules->get("InputfieldCheckbox");
$field->label = "Newsletter";
$field->required = 0;
$field->attr("name+id",'newsletter_subscribe');
$form->add($field);
$field = $modules->get("InputfieldFile");
$field->label = "Images";
$field->description = "Upload your images maximal 3 files.";
$field->required = 1;
$field->attr("name+id",'images');
$field->destinationPath = $upload_path;
$field->extensions = "jpg jpeg gif png";
$field->maxFiles = 3;
$field->maxFilesize = 2*1024*1024;
$form->add($field);
$field = $modules->get("InputfieldSubmit");
$field->label = "Submit";
$field->attr("name+id",'submit');
$form->add($field);
/**
* form hook example
*
*/
// function hookEmail($event){
// $file = $event->return;
// echo "email value: ". $file->value;
// }
// $form->get("email")->addHookAfter("processInput", null, 'hookEmail');
if($input->post->submit){
// process form input
$form->processInput($input->post);
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->fullname = $sanitizer->text($form->get("fullname")->value);
$uploadpage->email = $sanitizer->email($form->get("email")->value);
$uploadpage->message = $sanitizer->textarea($form->get("message")->value);
$uploadpage->newsletter_subscribe = $sanitizer->textarea($form->get("newsletter_subscribe")->value);
$uploadpage->addStatus(Page::statusUnpublished);
$uploadpage->save();
// save uploaded files to new page and remove temp files
$files = explode("|",$form->get("images")->value);
foreach($files as $file){
if($file && file_exists($upload_path . $file)){
$uploadpage->images->add($upload_path . $file);
unlink($upload_path . $file);
}
}
$uploadpage->save();
$sent = true;
$session->CSRF->resetToken();
} else {
// form incomplete or errors happened
// we remove uploaded files
$files = explode("|",$form->get("images")->value);
foreach($files as $file){
if($file && file_exists($upload_path . $file)){
unlink($upload_path . $file);
}
}
$form->get("images")->value = ''; // reset field value
}
}
if(!$sent){
echo $form->render();
} else {
echo "<p>Form submission succeded. Thanks!</p>";
echo "<p>Page created: $uploadpage->url</p>";
}
.Inputfields,
.Inputfields li {
list-style: none;
margin: 1em 0;
padding: 0;
}
.Inputfields li label {
font-weight: bold;
}
.Inputfields li p {
margin: 0;
}
.Inputfields li p.description {
font-style: italic;
}
.Inputfields textarea,
.Inputfields .InputfieldMaxWidth {
width: 100%;
}
.Inputfields .InputfieldSubmit label {
display: none;
}
.ui-state-error-text {
color: red;
}
/* hide drag upload */
.AjaxUploadDropHere{
display: none;
}
span.ui-state-error{ color: red;}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment