Menu

Simple Field Validation
Setup vield validation with Leaf's simple validation tests

Intro

Leaf v1.5.0 has introduced a new Leaf Form module which is the replacement for Simple Validation from v1.2.0 which was discontinued in v1.3.0. Leaf Form contains methods to handle input from the user...

To use Leaf Form, you simply have to import it:

$form = new Leaf\Core\Form;

Leaf Form Methods

sanitizeInput

sanitizeInput offers basic security for input data, i.e. sanitizing input against SQL injection.

$username = $form->sanitizeInput($username);

isEmpty

isEmpty checks a field to see if it's empty. If it's empty, it adds an error to Leaf Form's errors. It takes in 3 parameters: the data to test, the name of that data and the error message if it's empty(optional).

$form->isEmpty($username, "username"); // error will be: This field is required $form->isEmpty($username, "username", "Username is required");

isNull

isNull checks a field to see if it's null. If it's null, it adds an error to Leaf Form's errors. It takes in 3 parameters: the data to test, the name of that data and the error message if it's empty(optional).

$form->isNull($username, "username"); // error will be: This field cannot be null $form->isNull($username, "username", "Username can't be null");

Validate new in v1.5.0

Leaf now provides a much simpler way to validate a parameter using Leaf Forms. In one line, you can create a bunch of rules to validate a parameter with. Validate simply makes sure that the passed selected parameters pass these validation tests. It's never been simpler😎

Parameters which fail the form validation are saved in the form's errors which can be accessed with errors()

$form->validate([ "username" => "validUsername", "email" => "email", "password" => "required" ]);

This is a list of all supported validate rules

  • required
  • number
  • textOnly
  • validUsername
  • email
  • NoSpaces
Note that these rules aren't case-sensitive, so you can write the however you want, as long as the spelling is the same

errors

Remember we talked about Leaf Form errors? Leaf Form holds errors for all failed tests, you get all these errors back with returnErrors

$form->isNull($username, "username", "Username can't be null"); $errors = $form->errors(); return $errors;