Giter Club home page Giter Club logo

validator's Introduction

PHP Server Side Form Validation

Build Status Codacy Badge Latest Stable Version Total Downloads Latest Unstable Version License Join the chat at https://gitter.im/azeemhassni/validator

This is a small PHP package that makes it easy to validate forms in your project specially larger forms.

Watch installation and basic usage

Installation Guide:

You can install Validator either via package download from github or via composer install. I encourage you to do the latter:

{ 
  "require": {
    "azi/validator": "1.*"
  }
} 

##Usage to get started

  • require composer autoloader
require 'vendor/autoload.php';
  • Instantiate the Validator class
use azi\Validator;
$v = new Validator();
  • Define Rules for each form field
  $rules = array(
        'name' => 'alpha|required',
        'age'  => 'num|required',
    );
  • Run the validator
$v->validate( $_POST, $rules );
  • check validator for errors, if validation fails redirect back to the form
if ( !$v->passed() ) {
        $v->goBackWithErrors();
    }
  • show validation errors to user
 <label>Name :
      <input type="text" name="name">
 </label>
 <?=  Validator::error('name'); ?>

you can wrap error messages with custom HTML markup

  Validator::error('confirm_password', '<span class="error">:message</span>');

Rules

  • required
  • num
  • alpha
  • alnum
  • email
  • ip
  • url
  • min:number
  • max:number
  • same:field_name
  • array

Custom Expressions & Messages

  • Custom Expressions

you can register your custom RegExp before running the validator

$v->registerExpression( 'cnic', '#^([0-9]{13})$#', 'Invalid CNIC number' );

registerExpression method takes 3 arguments

  • expressionID - unique name for the expression
  • pattern - the RegExp string
  • message [optional] - the error message to be retured if the validation fails
Validator::registerExpression($expressionID , $pattern, $message)
  • Custom Messages

you can also pass a custom error message with each rule

 $rules['full_name'] = "required--Please Enter your name";

Conditional Rules

you can spacify conditional rules for a field

 $rules['age'] = 'if:gender[Male](required|min:2|num)';

Comparison Rules

you can also compare a field with another

  $rules['password'] = 'required|min:8';
  $rules['confirm_password'] = 'same:password';

validator's People

Contributors

azeemhassni avatar farzak avatar khairj avatar gitter-badger avatar

Watchers

 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.