Last active
September 18, 2020 17:28
-
-
Save mauricius/cfc2ac322127773069d5 to your computer and use it in GitHub Desktop.
Simple Class for building a Bootstrap-compatible Contact Form for ProcessWire
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ContactForm | |
{ | |
var $form; | |
var $to = '[email protected]'; | |
var $toName = 'Destination Name'; | |
public function __construct() | |
{ | |
$this->templates = wire('templates'); | |
$this->input = wire('input'); | |
$this->sanitizer = wire('sanitizer'); | |
$this->pages = wire('pages'); | |
$this->modules = wire('modules'); | |
} | |
/** | |
* Build the Contact Form | |
*/ | |
public function buildForm() | |
{ | |
$form = $this->modules->get("InputfieldForm"); | |
// Set the Form markup matching Bootstrap framework | |
// We don't use labels, we just relies on the placeholder | |
$form->setMarkup([ | |
'list' => "{out}", | |
'item' => "\n\t<div class='form-group' {attrs}>\n{out}\n\t</div>", | |
'item_label' => "", | |
]); | |
$form->method = "POST"; | |
$form->action = "./"; | |
$form->class = "form"; | |
// Name field | |
$singleField = $this->modules->get('InputfieldText'); | |
$singleField->attr('id+name', 'name'); | |
$singleField->attr('class','form-control'); | |
$singleField->placeholder = __("Name"); | |
$singleField->required = true; | |
$form->add($singleField); | |
// Email field | |
$singleField = $this->modules->get("InputfieldEmail"); | |
$singleField->attr('id+name','email'); | |
$singleField->attr('class','form-control'); | |
$singleField->placeholder = "Email"; | |
$singleField->required = true; | |
$form->add($singleField); | |
// HONEYPOT email field | |
$honeyField = $this->modules->get("InputfieldEmail"); | |
$honeyField->label = "Anti-Spam field! Dear screenreader user. Please leave this field out since it serves as spam detection."; | |
$honeyField->attr('placeholder', 'Leave this field empty! Spam-protection!'); | |
$honeyField->attr('id+name','hp_email'); | |
$honeyField->required = 0; | |
$form->add($honeyField); | |
// Subject | |
$singleField = $this->modules->get('InputfieldText'); | |
$singleField->attr('id+name', 'subject'); | |
$singleField->attr('class','form-control'); | |
$singleField->placeholder = __("Subject"); | |
$singleField->required = true; | |
$form->add($singleField); | |
// Message | |
$singleField = $this->modules->get('InputfieldTextarea'); | |
$singleField->attr('id+name', 'message'); | |
$singleField->attr('class','form-control'); | |
$singleField->placeholder = __("Message"); | |
$singleField->required = true; | |
$form->add($singleField); | |
// Submit button | |
$submit = $this->modules->get("InputfieldSubmit"); | |
$submit->name = "submit"; | |
$submit->value = __("Send"); | |
$submit->attr('class','btn btn-info'); | |
$form->add($submit); | |
return $form; | |
} | |
/** | |
* Send the email | |
*/ | |
public function sendMail($input) | |
{ | |
$wireMail = new WireMail(); | |
$wireMail->to($this->to); | |
$wireMail->from($input['email']); | |
$wireMail->toName($this->toName); | |
$wireMail->fromName($input['name']); | |
$wireMail->subject($input['subject']); | |
$wireMail->body($input['message']); | |
$numSent = $wireMail->send(); | |
if ($numSent > 0) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
} |
@marcioellobo check out this thread https://processwire.com/talk/topic/2089-create-simple-forms-using-api/ it will provide all the answers that you need.
Thank you, so much!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for that. Could you explain how do I link this code to my form? Sorry for the simple question.