- Notifications
You must be signed in to change notification settings - Fork1
Validation at it's best - variables, forms and files.
License
phrenotype/helga
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This is a library for validation with easy customization of error messages. It validates everything from variables to files. It is not limited to form validation.
It offers the surest way to safely upload files (especially images), even if they are improperly stored or included directly.
It has a clean free of unneccesary clutters.
Additionally, there are no external dependencies, apart from php, of course :).
composer require helga/helga
The most basic use is to validate single values.
<?phpusefunctionHelga\validate;$v =validate("chase")->withRules(['required','minLen:5','maxLen:10']);if($v->passes()){echo"Validation passed.";}else{var_dump($v->errors());}
<?phpusefunctionHelga\validate;;$v =validate("paul@gmail.com")->withRules(['email']);if($v->fails()){var_dump($v->errors());}else{echo"Validation passed()";}
For multiple values, pass an associative array like so :
<?phpusefunctionHelga\validate;$values = ['name' =>'','age' =>34,'address' =>'#5 Cool street','email' =>'fake@gmail']$rules = ['name' => ['required','minLen:4','alpha'],'age' => ['required','integer'],'address' => ['alnum'],'email' => ['email']]$v =validate($values)->withRules($rules);if($v->passes()){// Do something}else{// Do something else}
The variable$values
above could be any associative array, including super globals.
A complete list of the rule directives can be found here
You will notice that the method->errors()
is used to retrieve error messages. This is okay if you are only validating one value.
When validating multiple values, the errors come back as an associative array, with the key being the the name of the field, and the value being an array of all the errors associated with the field.
To get a flattened list of errors with dealing with multiple fields, use the->flatErrors
method.
By default, it only flattens and returns the first error for each field. Passing booleanfalse
as an argument will flatten all the errors.
<?phpusefunctionHelga\validate;$values = ['name' =>'ab1','age' =>'233yy','address' =>'#5 Cool street','email' =>'fake@gmail']$rules = ['name' => ['required','minLen:4','alpha'],'age' => ['required','integer'],'address' => ['alnum','minLen:5','maxLen:15'],'email' => ['email','alpha']]$v =validate($values)->withRules($rules);var_dump($v->flatErrors());var_dump($v->flatErrors(false));
The first thing to notice is the syntax of a rule.
directive[:arguments][:customMessage]
The only required field is the directive, likerequired
,min
,fileImage
e.t.c.
The arguments are only required if the directive requires arguments. For instanceinteger
,email
orurl
do not require arguments, butminLen
,max
ormin
require arguments.
Finally, you can pass a custom error message as a third argument.
For directives without arguments i.e single directives
<?phpusefunctionHelga\validate;$v =validate("c")->withRules(['integer::It must be a number','alpha::It must be an alphabet' ]);var_dump($v->errors());
For directives with arguments
<?phpusefunctionHelga\validate;$v =validate("c")->withRules(['minLen:4:Cannot be less than four','regex:/^\d/:Failed to match' ]);var_dump($v->errors());
Email :dev@paulrobert.xyz
Happy validation :)
About
Validation at it's best - variables, forms and files.