Alpha Validator

This validator can be used to ensure that a given value contains only alphabetical characters.

Back to Validators List

Supported Options

The Alpha validator supports the below option:

  • allow_whitespace: determines if whitespace characters are allowed or not. This is an optional parameter and the default value is false.

Usage

There are two different ways in which all Sparta validators can be used. See below examples for more explanations:

Example #1:

You can simply instantiate the Alpha validator and pass to it the data that you want to validate.

<?php
use Sparta\Validators\Alpha;

$validator = Alpha();

if(!$validator->isValid('coderavine')){
    //handle errors
}

By default, whitespace characters are not accepted as they are not part of the alphabet. However, this can be enabled by passing the allow_whitespace option either through the validator constructor:

$validator = Alpha(['allow_whitespace' => true]);

Or it can be configured via the validator setter method enableWhitespace:

$validator->enableWhitespace();

Example #2:

You can build your validation rules and pass it to the Validation object to handle as shown below:

<?php
use Sparta\Validation;

$rules = [
    'username' => 'alpha:allow_whitespace',
];

$validation = new Validation($data, $rules);
if(!$validation->isValid()){
    //Get errors by using the method $validation->getErrors(); 
}

In case of validation failure, error messages can be retrieved using the getErrors method.

Back to Validators List