A simple form submission handler for PHP
Handling form submissions is always a pain in the ass and I keep writing things over and over again. This is a class I think might stick as the default way to build upon – at least for me.
// this is a very simple demo of a contact form
$submission = new Submission(array(
// set the required fields
'required' => array('name', 'email', 'text'),
// make sure that the final data array contains only
// the fields we want to further use.
'keep' => array('name', 'email', 'text'),
// setup a validation event
'validate' => function($self) {
// sanitize data
$self->value('email', filter_var($self->value('email'), FILTER_SANITIZE_EMAIL));
$self->value('name', trim(strip_tags($self->value('name'))));
$self->value('text', trim(strip_tags($self->value('text'))));
// validate the email address
if(!filter_var($self->value('email'), FILTER_VALIDATE_EMAIL)) $self->addInvalid('email');
},
'submit' => function($self) {
// normally you would save something in a database
// here or send data via email.
// you can then trigger an error or…
$self->trigger('success');
},
'success' => function($self) {
// send to the "Thank you" page
header('Location:?thankyou');
},
'error' => function($self) {
$self->alert('Please make sure to fill in all fields correctly!');
}
));
Here's a demo including the Form HTML: http://bastianallgeier.com/submission
Define the request method here. Can be post or get.
If set to true, all form events will only be triggered on Ajax calls.
An array of required fields
An array of fields, you want to keep. Everything else, which is submitted will be excluded from the form data array.
Events are defined as closures, which are available since PHP 5.3. They work like you would expect from Javascript for example:
$submission = new Submission(array(
'success' => function($self) {
// this code is triggered on success
}
));
But be aware that $this
is not available in Closures. That's why the first argument of each event is a reference to the Submission class ($self
)
A filter event. Can be used to filter the data array some more.
A validation event. Validate all incoming data here and raise an error if one of the fields is not valid.
The main submit event, which happens after filter and validate. This can be used to actually do something with the submitted data – save it in a database or send and email or whatever.
A success event. Define here what happens when the form has been submitted successfully.
An error event. You can use this to react on any error during submission. You can even trigger that in all other events, maybe when you can't write data to the db or something like that.
Checks if a given field is required
Checks if a given field is missing
Mark a field as missing
Checks if a given field is invalid
Mark a field as invalid
Checks if a given field is marked as error. If no field is specified, this will serve as error check for the entire form.
This returns an array with all fields, which have been marked as error.
Checks if the form has been submitted
Get the value of a field. This can be used as setter if you pass a value with the second argument.
Get a sanitized version of a field value.
Get the alert message. This can also be used as setter if you pass a value.
This can either get the entire data if you pass no arguments. It can be a getter of a single value if you pass the field name as first argument or it can be a setter if you pass the field name and a value.
This triggers a form event. Arguments can be passed as array with the second argument.
This will return an array of triggered events, which can be used for debugging. You can also pass the event name as first argument to get only that event from the event log.
This will reset all internal arrays. You can use that to empty all form fields after submit.
MIT License - http://www.opensource.org/licenses/mit-license.php