PHP Classes
elePHPant
Icontem

PHP Validation One Library: Fetch and validate forms and request values

Recommend this page to a friend!
  Info   View files Example   View files View files (26)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2019-06-13 (5 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 95 All time: 9,306 This week: 313Up
Version License PHP version Categories
validationone 1.0GNU Lesser Genera...5HTTP, PHP 5, Validation
Description Author

This package can fetch and validate forms and request values.

It can take rules and other types of details using a chained interface for defining the values to be validated that can have been submitted to the server via forms or parameters of links that were clicked by the users.

The parameters for each submitted value can for instance the identifier of the value, type of value, conditions to compare the submitted value with accepted values, if the default value will be restored in case the submitted value is invalid, etc..

A separate class handles the validation error message lists for each submitted value.

  Performance   Level  
Name: Jorge Castro <contact>
Classes: 14 packages by
Country: Chile Chile
Age: 43
All time rank: 19327 in Chile Chile
Week rank: 55 Up1 in Chile Chile Up
Innovation award
Innovation award
Nominee: 7x

Winner: 1x

 

Details

ValidationOne

It's a PHP library for fetch and validate fields and store messages in different containers(including error, warning, info, and success) depending on the conditions.

Build Status Packagist Total Downloads [Maintenance]() [composer]() [php]() [php]() [CocoaPods]()

Examples

Examples

Tutorial Form and Table with PHP

diagram example It is an example of functionality. A normal example is more complex, even if it's only a few lines of code.

ValidationOne

Let's say we want to validate a value an input value (get) called "id", we could do the next things:

  • the default value is the text "ERROR"
  • the type of the value is an integer, so it must returns an integer. It also could be an integer,decimal,string,date and boolean
  • we add a condition, the value must be equals (eq) to 10. If fails then it returns a message (as error)
  • we add another condition, if the value must be equals (eq) to 30. If fails then it returns an info (not an error)
  • If the operation fails then it returns the default value.
  • And finally, we obtain the "id" from $_GET (parameter url).
$val=new ValidationOne();

$r = $val->def('ERROR')
    ->type('integer')
    ->condition("eq", "It's not equals to 10", 10)
    ->condition("eq", "It's not equals to 30 (info)", 30, 'info')
    ->ifFailThenDefault()
    ->get('id'); // <-- end of the chain

But, where is the error?. It's in messagelist

var_dump($val->messagelist->allArray()) // here we show all messages of any kind of type. 

However, we could also show a message by type (error, warning..) and only message by specific identifier.

var_dump($val->messageList->get('id')->allErrorOrWarning())) // All error or warning contained in the key "id".

Why the messages are store in some structure?. Is it not easy to simply return the error? .

The answer is a form. Le't say we have a form with 3 fields. If one of them fails, then the error must be visible for each field separately. Also the whole form could have it's own message.

condition ($type, $message="", $value=null, $level='error')

  • @param string $type

    number:req,eq,ne,gt,lt,gte,lte,between string:req,eq,ne,minlen,maxlen,betweenlen,notnull date:req,eq,ne,gt,lt,gte,lte,between> boolean:req,eq,ne,true,false function: fn.static.Class.methodstatic fn.global.function fn.object.Class.method where object is a global $object fn.class.Class.method fn.class.\namespace\Class.method

  • @param string $message

    Message could uses the next variables '%field','%realfield','%value','%comp','%first','%second'

  • @param null $value
  • @param string $level (error,warning,info,success)
  • @return ValidationOne

MessageList

MessageList is a list of containers of messages. It's aimed for convenience, so it features many methods to access of the information in different ways.

Messages are cataloged as follow

| id | Description | Example | |---------|----------------------------------------------------------------------|---------------------------------------| | error | The message is an error and it must be solved. It is a show stopper. | Database is down | | warning | The message is a warning that maybe it could be ignored. | The registry was stored but with warnings | | info | The message is an information | Log is stored | | success | The message is a succesful operation | Order Accepted | |

Sometimes, both errors are warning are considered as equals. So the system allows to read an error or warning.

Error has always the priority, then warning, info and success. If you want to read the first message, then it starts searching for errors.

You can obtain a message as an array of objects of the type MessageItem, as an array of string, or as an a single string (first message)

Pipeline

  • Input value, it could come from set()/post()/get()/request()/getFile()
  • What if the value doesn't exist?
  • *

version list

  • 2019-03-08 1.14 Added getFile() to upload a file.
  • 2018-12-15 1.13 Added phpunit and travis.
  • 2018-10-29 1.12 getFile now it's available via ValidationOne()
  • 2018-10-22 1.11 Some fixes. Now isEmpty is called isMissing
  • 2018-10-22 1.10 New Features
  • * Added ValidationInputOne, now the fetchs are done by this class (SRP principle)
  • * Added a fix with the input, when the value expected is an array but it's returned a single value
  • 2018-10-15 1.9 Added some extra features
  • 2018-10-15 1.8 Some fixes and phpdocs, a new example
  • 2018-10-15 1.7 Added method addMessage() in ValidationOne. Now ErrorItem/ErrorList is called MessageItem and MessageList
  • 2018-10-06 1.5 added method first() in MessageItem
  • 2018-10-03 1.4 added defaultNatural()
  • 2018-10-02 1.3 basicvalidation() was deleted. It was restored.
  • 2018-10-02 1.2 array() is now isArray()
  • 2018-09-30 1.1 Some fixes
  • 2018-09-29 1.0 first version

todo

  • More examples
  • Documentation

Note

It's distributed as dual license, as lgpl-v3 and commercial.

You can use freely in your close source project. However, if you change this library, then the changes must be disclosed.

  Files folder image Files  
File Role Description
Files folder imageexamples (13 files, 1 directory)
Files folder imagelib (5 files)
Files folder imagetests (2 files)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  examples  
File Role Description
Files folder imagedocs (1 file)
  Accessible without login Plain text file benchmarkglobal.php Aux. Auxiliary script
  Accessible without login Plain text file common.php Example Example script
  Accessible without login Plain text file examplefile.php Example Example script
  Plain text file Someclass.php Class Class source
  Plain text file testarrayget.php Class Class source
  Accessible without login Plain text file testarrayget2.php Example Example script
  Accessible without login Plain text file testarrayset.php Example Example script
  Accessible without login Plain text file testContinueOnError.php Example Example script
  Plain text file testfetch.php Class Class source
  Accessible without login Plain text file testglobal.php Aux. Auxiliary script
  Plain text file testval1.php Class Class source
  Plain text file testval2.php Class Class source
  Plain text file validaimage.php Class Class source

  Files folder image Files  /  examples  /  docs  
File Role Description
  Accessible without login Image file DiagramExample.jpg Data Auxiliary data

  Files folder image Files  /  lib  
File Role Description
  Plain text file MessageItem.php Class Class source
  Plain text file MessageList.php Class Class source
  Plain text file ValidationInputOne.php Class Class source
  Plain text file ValidationItem.php Class Class source
  Plain text file ValidationOne.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Accessible without login Plain text file bootstrap.php Example Example script
  Plain text file DaoOneTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:95
This week:0
All time:9,306
This week:313Up