Git Product home page Git Product logo

form-validation's Introduction

FormValidate

PHP Form Validation Class

Loosely patterned on the interface from the Codeigniter form validation library, but written from scratch in PHP 5.3+. No dependencies. Includes a full set of unit tests in PhpUnit format.

Originally written to use with various Micro-frameworks as they (generally) don't have this included and I couldn't find a no-dependencies library that I liked that someone else had written.

I didn't include tests like "is_unique" that introduce awkward dependencies. These are very easy to add using closure callbacks and then they can be tailored exactly to the application.

Basic usage:

<?php
namespace Test; //whatever this namespace is
use \Libraries as Lib; //FormValidation lives in "Libraries" as standard

//pull in the Class for testing
define('FCPATH', TRUE); //defeat check to prevent direct script execution
require 'FormValidate.php';

//create validator
$v = new Lib\FormValidate();
//add field(s) - params are field name, an optional alias, an optional list of rules
//with the "|" separator.
$v->add_field('name_field','Name','required|alpha');
//add more fields as required...

//the data to validate - any array of key=>value pairs
//(this example will fail)
$data = array('name_field' => '0123');

//run validation and do something with the results
if($v->run($data))
{
 //passed validation - do stuff
 echo "Pass!<br />";
}
else
{
 //failed validation - do other stuff
 $array_of_errors = $v->get_all_errors_array();
 
 //....or per-field with default html tags (returns empty string if no error)
 echo $v->get_error_message('name_field');
}

An example of a custom rule defined as an anon callback looks like:

$v->add_validation_rule('new_test', function($obj,$str){

  $obj->add_error_message('new_test',"The %s field must contain alpha characters only.");
   
  return (bool) preg_match('#^[a-zA-Z]+$#',$str);
  
}); 

form-validation's People

Contributors

nmcgann avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.