Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Validation at it's best - variables, forms and files.

License

NotificationsYou must be signed in to change notification settings

phrenotype/helga

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

github starslicensecontributorscode size

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 :).

Install

composer require helga/helga

Usage

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

Retrieving Error Messages

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));

Anatomy Of A Directive

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.

Custom Error Messages

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());

Contact

Email :dev@paulrobert.xyz

In Summary...

Happy validation :)

About

Validation at it's best - variables, forms and files.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp