Skip to content

coderavine/sparta

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sparta Logo

Build Status Latest Stable Version Total Downloads License #Introduction

Sparta is a simple, elegant, and easy to use stand-alone PHP library for effortless inputs validation. Sparta uses simple, straightforward validation techniques that attempt to take the pain out of development by easing common validation tasks used in the majority of web projects. If you are looking for implementing reliable, effective, quick validations and a library that can be extended easily to fit your needs, then Sparta is definitely for you.

#Installation

You can install and update Sparta library using Composer:

composer require coderavine/sparta

#Usage Sparta validation can be used in two different ways. Below are various examples that explain both:

  • You can simply use a validator class to validate a certain type of data. For example, if you have a date and you want to ensure that only a date and nothing else is provided by end users, then you can do something as follows:
<?php
use Sparta\Validators\Date; 
	    
$dateValidator = new Date();  
if(!$dateValidator->isValid('2007-01-01')){
	//Error messages can be simply retrieved using "errors" method
	$errors = $dateValidator->errors();
}
  • Another alternative way is to use the Validation class that accepts the data to be validated along with a list of defined rules for each attributes and let it do the heavy lifting for you. A simple example that explains this is given below:

Let us assume that we have an account registration option in our web-based application and we want to enforce the below rules for the username field:

  1. It must be a required field
  2. It must only contain alphabetic characters
  3. It must have a minimum and maximum length of 12 and 50 respectively

To achieve this, we first need to define our validation rules for the username as follows:

<?php
	//We define our rules for the username field as specified above
	$rules = [
		'username' => 'required|alpha|min:12|max:50',
	];

After that, we need to ensure that the username field content is available in our user's input collection:

<?php

$data = [
	//This could be coming from $_POST, $_GET,etc.,
	'username => 'JohnDoe', 
];

Then, we can simply run the validation by passing both data and rules to the Validation object to handle the validation for us as follows:

<?php
use Sparta\Validation;
    
$validation = new Validation($data,$rules);
if(!$validation->isValid()){
	//Get error messages and manipulate them however you want
	$validation->getErrors();
 }

#Documentation Please refer to the Library Documentation for more information.

#Contributions

Contributing your PHP love to Sparta is always more than welcome via a Pull Request. Please refer to Contribution Section

#License The Sparta Validation is open-sourced library licensed under the MIT license