A sneak peek at YAPS Form Fields and Validation

This week I have spent a great deal of time working on the YAPS core, specifically a series of classes for forms, fields and validation.

  • YAPS_Form
  • YAPS_Form_Field
  • YAPS_Form_Validation
The general concepts surrounding these classes is to allow for simple management of forms, fields and validation.

Basic usage of YAPS Form

The base management of forms, where fields are defined and validation is checked. Usage is simple.

$form = new YAPS_Form();

The above code initiates your form. From here you can add fields, add validation to fields, bind submitted values to the form and validate the submitted data.

Adding a field is as simple as calling the addField() method. This method accepts four parameters, the field name, field label, default value and whether or not field validation should cascade.

$form->addField( 'field_name', 'Field label', 'Default value', false );

The above code will add the field ‘field_name’ with a label of ‘Field label’, the default value for this field is ‘Default value’ and validation cascading is turned off, which by default is on.

Once you have added the field, validation rules can be applied to said field. The addRule() method accepts four parameters, the rule name, an array of parameters, a custom error message and whether or not the fields rule is global.

$form['field_name']->addRule( 'rule_name', array( 'params' ), 'custom message', false );

As it stands today only four validation rules exist.

  • required
  • email
  • min_length
  • max_length
  • matches

Additional rules will be added in due course.

Once a form, its fields and their validation rules have been registered the following methods are open for use.

$form->bind( $fields )

Bind field values to form fields, ready for validation and for easy rememberace of entered field values.


Returns the submitted field value, or the default value if no value has been submitted.


Performs validation checks and returns whether the form is valid or not.


Outputs global form errors.


Outputs local form field errors.

Additional information

There is some neat functionality built into the YAPS Form classes, some of which I have outlined below.


While not just for the YAPS Forms, this concept has been applied across the board for all native YAPS plugins.

Any HTML used within YAPS is easily modified, no HTML exists within functions or class methods, instead each bit of template code is located in a seperate template file. This allows for easy adaptation for output. Want to change a template? No problem, simply copy from the YAPS plugin template directory into your own theme directory and modify as you please – yes, it is that simple.

Global and field specific validation rules

When adding a rule to a field you can specify whether the error should be global or local to the field. The main difference to be aware of is that a global rule error is applied to the form itself and can be output as a whole (for example above the form), where as local field errors are applied to said field and can be output inline with the field.


YAPS is coming along well and I still hope to have a beta of the core, YAPS Member and a couple of other plugins by the end of the year.

The demonstration of usage for YAPS Form should show the diversity and expandability of code written for YAPS. While I haven’t gone into detail about the extensability of YAPS, this will come in a later post.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>