Open-Source StellarWP Library Simplifies Data Validation

StellarWP just published its latest open-source library on GitHub: an end-to-end, ready-to-deploy PHP validation library. WordPress developers can use the library to enhance data validation (and clean up stored data) from forms. The library’s extendable list of rules makes it easy to validate valuable user input, no matter what type of information you’re collecting.

Jason Adams, GiveWP manager of development, built the library. I spoke with him to learn the key details of its backstory. Jason also provided helpful pointers on how WordPress users can make the most of the library on their own sites.

Why We Built It

The GiveWP team is currently at work on GiveWP 3.0, which includes re-imagining how forms work in the plugin.

“Our system of validation needed to be flexible and powerful,” Jason says. That meant letting users dynamically add and remove fields on their forms.

But forms can be tricky, and it’s not enough to merely put your input fields on a page and hope for the best; it’s important to validate the information those fields collect.

“Forms aren’t always obvious,” Jason says. “It really means any kind of interaction with the application wherein you’re providing information—a user profile, search filters, preferences, donations, and so forth.”

That includes managing forms from two sides: First, the browser side, where users need to receive validation to know if a field is required or if they’ve entered information incorrectly. Then on the site admin side, where form data needs to be validated once it’s sent to the server. Historically, the process of ensuring this validation on both sides could be cumbersome.

“The act of defining how things need to be validated is often just reinvented every time it’s needed, with varying levels of quality,” Jason says.

The validation library provides a way to define validation constraints on the server while also sending those rules to the browser where the user interacts. It provides both front-end and server-side validation without any extra work.

As Jason built a solid validation library for GiveWP, the project grew into an opportunity to solve a common issue for developers across WordPress.

“I started out just building a system for our needs but quickly realized I was building something generic that could stand on its own feet and be reused across projects,” he says.

How it Works

The goal of the library is simple:

“It takes in a set of data and a set of rules for that data, and outputs one of two things,” Jason says. “A set of translatable error messages if any data was invalid, and a set of trusted data if all the validations pass.”

The library comes with a predefined set of common rules. As users need more rules, it’s easy to add new rules that are clear and declarative.

Why Use it On Your Site?

For starters, the validation library can save developers from needing to reinvent the wheel for form validation.

There are a few other key benefits:

  • It provides a high-quality, consistent method for validating data in any context.
  • It’s easy to apply to a new or existing system.
  • Creating new rules is simple.
  • Defining rules for a set of data is easy to read and write.

A Sample Rule

What does a rule from the library look like in practice? Here’s one example:

’email’ => [‘required’, ’email’, ‘max:255’]

“It’s really easy to see that the email data is required, must use the email format, and can only be as long as 255 characters,” Jason says.

Sharing Resources Beyond StellarWP

This latest library is a continuation of the StellarWP team’s mission to raise the bar for engineering in WordPress. As GiveWP adopts the library, we look forward to using it across our other brands and sharing additional resources and best practices with the greater WordPress community.

Ready to try the library yourself? Check it out on GitHub.

You might
also like

Similar Posts