Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

darth is a very simple procedural data validation tool for PHP.

branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

README.md

darth

Build Status Coverage Status

"I find your lack of validation... disturbing." -- Darth Validator

darth is a very simple procedural data validation tool for PHP.

Just create a stack of rules, and apply them to your data hash.

Currently, the only supported validation types are:

  • required
  • email
  • confirmation
  • regex
  • custom callbacks

Check the example below and the source for reference.

Example

The example below shows all that you can do with this.

<?php
require __DIR__."/darth.php";

use noodlehaus\darth as darth;

# create a stack of rules to apply
$rules = array(
  darth\rule('required', 'username', 'Username is empty'),
  darth\rule('regex', 'username', '/^[a-z0-9_]{4,}$/i', 'Username is invalid'),
  darth\rule('required', 'email', 'Email is empty'),
  darth\rule('email', 'email', 'Email is invalid'),
  darth\rule(
    'confirmed',
    'password',
    'password_confirmation',
    'Password is invalid or not confirmed'
  ),
  darth\rule(
    'regex',
    'age',
    '/^[0-9]+$/',
    'Age is invalid'
  ),
  darth\rule(
    'custom',
    'role',
    function ($role) { return $role == 'sith'; },
    'Come to the dark side!'
  )
);

# sample data that needs validation
$data_fail = array(
  'username' => '',
  'email' => '',
  'role' => 'developer',
  'password' => 'abc',
  'password_confirmation' => '123'
);

# apply the rules, and get the errors
$errors = darth\apply($rules, $data_fail);

var_dump($errors);

Output will be:

array(5) {
  ["username"]=>
  array(2) {
    [0]=>
    string(17) "Username is empty"
    [1]=>
    string(19) "Username is invalid"
  }
  ["email"]=>
  array(2) {
    [0]=>
    string(14) "Email is empty"
    [1]=>
    string(16) "Email is invalid"
  }
  ["password"]=>
  array(1) {
    [0]=>
    string(36) "Password is invalid or not confirmed"
  }
  ["age"]=>
  array(1) {
    [0]=>
    string(14) "Age is invalid"
  }
  ["role"]=>
  array(1) {
    [0]=>
    string(22) "Come to the dark side!"
  }
}

License

MIT http://noodlehaus.mit-license.org

Something went wrong with that request. Please try again.