Quick Guide
WFV takes an array of arguments that define the rules and error messages for each field in a form.
Typically, this is defined in a themes functions.php
, but it can be anywhere.
This array is then passed into wfv_create( 'my_form', $form_array )
which creates an instance of FormComposite
.
The FormComposite
encapsulates all the parts and features of the API. It will do a lot of work for you :)
Config Structure¶
Basic example:
1 2 3 4 5 6 7 8 9 10 11 12 | <?php $my_form = array( 'first_name' => [ 'label' => 'First Name', 'rules' => 'required' ], 'last_name' => [ 'label' => 'Last Name', 'rules' => 'required' ], ); |
Validation Rules¶
The rules attribute defines the constraint(s) for a field.
You can define multiple rules for a single field by separating each field with a pipe character.
1 2 3 4 5 6 7 8 9 10 11 12 | <?php $my_form = array( 'first_name' => [ 'label' => 'First Name', 'rules' => 'required|alpha_dash|length_max:30' ], 'email' => [ 'label' => 'Email', 'rules' => 'required|email' ], ); |
Error Messages¶
Each rule has a default error message, but you may override them with custom ones.
1 2 3 4 5 6 7 8 9 10 11 12 | <?php $my_form = array( 'first_name' => [ 'label' => 'First Name', 'rules' => 'required|alpha_dash|length_max:30', 'messages' => [ 'required' => 'Please enter your first name', 'alpha_dash' => 'First name can only contain alphabetic characters, dashes, and underscores', ] ] ); |
Activate¶
Pass the array of arguments into the wfv_create() function to get an instance of WFV\FormComposite
.
This will magically turn that boring array into a rich composite object.
Example:
1 2 3 4 5 6 7 8 9 10 | <?php wfv_create( 'my_form', $my_form ); // $my_form is now an instance of FormComposite $my_form->input()->contains('email', 'foo@bar.com'); // false $my_form->input()->is_populated(); // false $my_form->display('email'); // ... |
Note
The instance is passed by reference to the array of arguments. You do not need to assign it to a variable.
$my_form
becomes an instance of FormComposite:
1 | wfv_create( 'form_name', array $my_form ) |
For available methods, see input, populate, and errors
Validation Callbacks¶
WFV triggers an action for pass or fail. Hook into them and handle any logic inside callbacks
Markup a Form¶
1 2 3 4 5 6 7 8 | <form name="contact_form" method="post"> <input name="name" type="text"> <input name="email" type="text"> <textarea name="msg"></textarea> <?php $form->token_fields(); ?> <input type="submit" value="Submit"> </form> |