Process Form
The
ProcessFormclass is being deprecated, please use the newFxForminstead.
The ProcessForm class gives you a few helpful methods to validate and process forms on the backend. To setup frontend validation, you will need to add look at recent projects JS files and do it yourself for each form. (Will hopefully get frontend stuff in here eventually)
It is recommended that you process forms using the WP-API and have a custom endpoint to handle forms. You can then simple init the class and check each form with one function.
Example
$form_fields = [
['name', 'string', true],
['phone', 'string', false],
['email', 'email', true],
['message', 'string', true],
['agreeToTerms', 'bool', true],
];
$process_form = new ProcessForm($form, $form_fields);
// In this example we check the admin options for an email setting
// so we can notify the admin on successful submissions.
$to = get_opts('email');
if ($to) {
$process_form->set_options([
'to' => [$to],
'subject' => 'Website Contact Submission'
]);
}
$process_form->submit($form_data);
$response_data = $process_form->response();
// Do whatever you want to do with the response data
Walkthrough
To setup a new instance, create a new object and pass in the form name and the inputs:
$process_form = new ProcessForm($form: string, $form_fields: array);
Defining fields
The fields array should be structured like so:
$form_fields = [
[name: string, type: string, required: bool],
...
];
Where each array is an input. The type parameter can be either string, number or bool and is used to convert the data to the correct type.
Submitting the form
After setting up the form, you are good to submit:
$process_form->submit($form_data);
Confirmation emails
To send confirmation emails after a successful submission, you need to set the to and subject options before process. So just after you create the new form, use the set_options method.
$process_form = new ProcessForm($form, $form_fields);
$process_form->set_options(array(
'to' => array('youremail@email.com'),
'subject' => 'Contact Form Submission'
));
Getting the response
To get the response of the form:
$response_data = $process_form->response();
You can then return the response however you like. By default the response is JSON encoded, to get the raw response you just need to change the setting after construction. You may want to do this if you are handling the response in PHP.
$process_form = new ProcessForm($form, $form_fields);
$process_form::$encode_response = false;
Extending
It is possible to extend the ProcessForm class to do more than a simple email.
Below is an example on how, if successful, save the form data in a custom database.
class SQLForm extends ProcessForm {
// This is fired after submit regardless of the result
protected function on_submit($success) {
if (!$success) {
return;
}
global $wpdb;
$sql_data = $this->prepare_data_for_database();
$submit_data = $wpdb->insert(
DB_TABLE,
$sql_data
);
if (!$submit_data) {
$this->data['success'] = false;
}
}
// Helper function to setup data for SQL
// You may need to match column names or format the data
// NOTE: Input values have already been cleaned by the sanitize_input() method prior
private function prepare_data_for_database() {
$date = date('Y-m-d H:i:s');
$form = $this::$form_name;
$name = $this::$form_data['name'];
$email = $this::$form_data['email'];
$phone = $this::$form_data['phone'];
return array(
'date' => $date,
'name' => $name,
'email' => $email,
'phone' => $phone,
'form' => $form,
);
}
}