GithubHelp home page GithubHelp logo

hasankaya53 / php-validation-library Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 34 KB

validation library

Home Page: https://packagist.org/packages/luckystar/validation

License: MIT License

PHP 100.00%
input-validation-library php-validation validation

php-validation-library's Introduction

Validation library

This is a lightweight PHP validation library designed to simplify the process of validating input data in web applications. It provides a set of commonly used validation rules such as required fields, maximum and minimum lengths, numeric checks, and more. With easy integration and customizable error messages, it streamlines the validation process, ensuring data integrity and user-friendly error handling.

Rules List

Rule Description Example
required This field is required required
max_length This field must be less than {max_length} characters long max_length[10]
min_length This field must be at least {min_length} characters long min_length[5]
is_numeric This field must contain only numbers is_numeric
valid_email This field must contain a valid email address valid_email
valid_url This field must contain a valid URL valid_url
valid_ip This field must contain a valid IP address valid_ip
min This field must be greater than or equal to {min} min[5]
max This field must be less than or equal to {max} max[10]
date This field must be a valid date date

Other Parameters

Parameter Type Description Example
error_messages Array Custom error messages error_messages => ['required' => 'This field is required']

Error parameters

Parameter Description Example
:field Field name 'error_messages' => [ 'required' => ':field is required', 'max_length' => ':field is too long', 'min_length' => ':field is too short' ]
:number Field Number 'error_messages' => [ 'min' => ':field must be greater than or equal to :number', 'max' => ':field must be less than or equal to :number' ]

set rules...

$rules = [
    'username' => 'rules'...
];

//or ..

$rules = [
    'username' => [
        'rules' => 'required|max_length[10]|min_length[5]',
        'error_messages' => [
            'required' => ':field is required',
            'max_length' => 'Username is too long max: :number',
            'min_length' => 'Username is too short min:  :number'
        ]
    ]
];


$checker = $validation->validateRule($_POST, $rules);

How to install and run the project

Step 1: Install Composer

composer require luckystar/validation

Step 2: First, require the composer autoloader in your script

<?php
require_once 'vendor/autoload.php';

$validation = new LuckyStar\Validation\Validate;

// Add Rules

Step 3: Add Rules and Validate (example)

$rules = [
	'username' => [
		'rules' => 'max_length[3]|min_length[1]'
	],
	'password' => [
		'rules'  => 'max_length[10]|min_length[5]',
		'error_messages' => [
			'max_length' => 'Password is too long',
			'min_length' => 'Password is too short'
		]
	],
	'number' => [
		'rules' => 'is_numeric|max_length[8]|min_length[3]',
		'error_messages' => [
			'is_numeric' => ' Sayısal olmalı ...',
			'max_length' => 'Number is too long',
			'min_length' => 'Number is too short'
		]
	],
	'req' => [
		'rules' => 'required',
		'error_messages' => [
			'required' => 'This field is required'

		]
	]
];

$_POST = ['username' => '12345', 'password' => '12345', 'number' => '123sa45'];



$checker = $validation->validateRule($_POST, $rules);

if ($checker){
	//no errors
	foreach ($validation->getErrors() as $error){
		echo $error . "<br>";
	}
}else{
	echo "No errors";
}

php-validation-library's People

Contributors

hasankaya53 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.