A PHP and jQuery form creation and validation library available? [closed]
Original Question
Is there a well tested and preferably mature library out there for creating forms in PHP with both client side (this is where the jQuery comes in) and server side validation?
Ideally the form would either be generated from PHP classes or written as plain HTML and parsed ala Agavi. The correct jQuery hooks would then automatically be created by the library so that the included jQuery client side validation can run.
One of the jobs I do regularly is some variation on the good old contact form and I would like to开发者_运维问答 standardise this work so that I can trot out the same best practice code each time. With this in mind the HTML generated by the PHP classes should be good enough so that extra CSS hooks can be added where needed etc.
Any suggestions gratefully received.
Update
I have been combing through and reviewing the options that I have found and that others have suggested below and at the moment I would rank the projects in the following order for quality from the small amount of testing and research I have done on all of them.
- ValidForm Builder
- jFormer
- HTML_QuickForm2 (if you can get the client side validation working then this should jump higher than jFormer!)
- php-form-builder-class
- Use Symfony! (a whole MVC framework for form rendering and validation is overkill)
I am still unconvinced by any of the options to be honest and I am left wondering why people who embark on these projects do not start with some solid and well tested components. For example I would have thought a combination of:
- Parsley.js Client side validation
- Symfony form component perhaps
- Perhaps with Respect Validation
Would give you a good stable base to work from and produce a nice library on top of tested components.
Also if you are interested in a library that parses your HTML rather than generating the HTML from a PHP class I have found a project called Minacl. Like the Agavi option I mentioned in the original question.
You can use ValidForm Builder. It matches perfect to your requirements: With it you can define Forms with validation rules in PHP, and it generates the Forms with jQuery and client side validation.
Feature List (taken from their site):
- The API generates XHTML Strict 1.0 compliant code.
- Field validation on the client side to minimize traffic overhead.
- Field validation on the server side to enforce validation rules and prevent tempering with the form through SQL injection.
- Client side validation displays inline to improve user satisfaction. No more annoying popups that don't really tell you anything.
- Easy creation of complex form structures.
- Uses the popular jQuery Javascript library for DOM manipulation.
- Completely customizable using CSS.
- Automatic creation of field summaries for form mailers in both HTML and plain text.
- It's open source and therefore completely free (here's the GitHub repo)!
UPDATED for 2017
Agile UI is a PHP UI component library (and successor of atk4.3) including things like:
- Interractive Forms
- CRUD
- Grid
- Menus
- Paginators
- see more at http://ui.agiletoolkit.org/demos/index.php
All components have a consistent and simple PHP interface:
$form = new \atk4\ui\Form();
$layout->add($form);
$form->setModel(new Country($db));
If you prefer CRUD just use \atk4\ui\CRUD
. Agile UI also offers tools for adding custom components.
Comparing with alternative software:
- Outputs HTML for form
$form->render()
or for entire web app. - Automatically styled with Semantic UI
- Server-side validation, displays errors inline.
- Form layouts
- Includes support jQuery UI.
- Stores data through Agile Data, SQL, NoSQL
- Support custom HTML templates
- Open-source and licensed under MIT
An option I have come across is jFormer, but it has a very short history and the PHP code is less than desirable with a massive 3000 line file full of multiple classes. It doesn't take a modular approach.
What exactly are the problems with running client-side validation in HTML_QuickForm2?
It needs a quickform.js file to work. The file is installed with the package but is not automatically included in the form output. I've recently updated the docs to make this issue more obvious.
Also QF2 had a couple of releases since your question, a few JS-related bugs were squashed. Consider giving it another try: we are targeting a stable release Really Soon Now and would love feedback.
Whilst looking through the PEAR repository today I noticed that HTML_QuickForm2 has made a new release that now supports client side validation as well. It does not use jQuery though and they have written their own JS framework for the job.
Maybe you can use the Symfony framework. It provides a Model-View-Controller layer and automatic form generation out of the model. There are many validation options. But I think there is some modification needed to integrate JQuery client-side validation too.
I just stumbled upon a jQuery plugin called jquery-ahm. It moves the validation logic to the server, but executes it at the client with JavaScript. At the bottom of the page you see an example for form validation.
Html markup:
<form id="form" action="/a.form" method="post" class="ahm">
<p>Name</p>
<input name="name" id="name" value="" type="text">
<p>Email</p>
<input name="email" id="email" value="" type="text">
<p>Password</p>
<input name="password" id="password" value="" type="text">
<br><input value="Submit Form" type="submit">
</form>
PHP code:
<?php
// catch errors
foreach (array("name", "email", "password") as $key) {
if (empty($_POST[$key])) {
$error["#$key/addClass"] = "error";
$error["#$key/after"] = "<span class="error">Please enter $key</span>";
}
}
// get response
if (!empty($error)) {
$reset["input.error/removeClass"] = "error";
$reset["span.error/remove"] = "";
$response = $reset + $error; // reset errors before applying new ones
} else {
$response["#form/replaceWith"] = "<strong>Form successfully submitted</strong>";
}
// return response
return $response;
?>
Result:
精彩评论