Creating a custom filter (advanced)

If you want to add a new filter you should first check out the files in the folder quform/lib/Quform/Filter/. You will find the code for existing filters which should give you an idea of how to make your own. What you need to do is add a new class in that folder for your new filter, however there are a few things you should note.

  • Your filter must have its own class with a name that begins with Quform_Filter_
  • The name of the file should be the part of the class name after Quform_Filter_ and end with .php
  • Your filter class must implement Quform_Filter_Interface
  • Your filter class must have a method called filter() that takes the given value and returns the filtered value

Naming your filter

Filters have a special naming system in order to add them to an element. If you notice two of the existing filter classes called Quform_Filter_StripTags and Quform_Filter_Trim which are added to element using the names stripTags and trim. The conversion between short name and class name happens automatically. To convert from short name to class name, make the first letter uppercase and add Quform_Filter_ to the beginning. To convert from class name to short name make the first letter lowercase and remove Quform_Filter_ from the beginning. If you made a new filter with the class name Quform_Filter_MyNewFilter you could add it to an element using:

1
$element->addFilters(array('myNewFilter'));
$element->addFilters(array('myNewFilter'));

You can also add the filter to the element by passing in an instance of it:

1
$element->addFilter(new Quform_Filter_MyNewFilter());
$element->addFilter(new Quform_Filter_MyNewFilter());

Still having trouble? Head over to the forums.

Forums