Recommend this page to a friend! |
Classes of Edward Paul | Flexible PHP Validation Library | README.md | Download |
|
DownloadFlexible PHP Validation ClassProvides several different approaches to validate your application's incoming data InstallationYou can install the package via composer:
Basic Usage
Writing The Validation LogicWe pass the field we intend to validate into the
Validation Ruleswe pass the desired validation rules into the
Displaying The Validation ErrorsSo, what if the incoming parameters do not pass the given validation rules? The errors will be able in the
Validation Error CheckYou may also use the
Customizing The Error KeyIf you can enter full_name as the key to be validated and required is set, your error message comes out in this format
Validating ArraysValidating array doesn't have to be a pain. You may use "dot notation" to validate attributes within an array. For example, if the incoming request contains an array field, you may validate it like so:
You may also validate each element of an array. For example, to validate that each e-mail in a given array input field is unique, you may do the following:
Custom Validation RulesYou may wish to specify some of your own rules. One method of registering custom validation rules is extending the Rule Class Once the custom rule class has been created, we are ready to define its behavior. A rule object contains two methods: passes and message. The passes method receives the field, value, and data, and should return true or false depending on whether the value is valid or not. The message method should return the validation error message that should be used when validation fails:
Once the rule has been defined, you may attach it to a validator by passing an instance of the rule object with your other validation rules:
Available Validation RulesBelow is a list of all available validation rules and their function Required :The field under validation must be present in the input data and not empty. Email :The field under validation must be formatted as an e-mail address Max :The field under validation must be less than or equal to a maximum value
RequiredWith :The field under validation must be present and not empty only if all of the other specified fields are present.
Between :The field under validation must have a size between the given min and max
Optional :The field under validation may be null. This is particularly useful when validating primitive such as strings and integers that can contain null values.
Image :The file under validation must be an image (jpeg, png, bmp, gif, svg, or webp), the image
Same :The given field must match the field under validation.
Confirmed :The field under validation must have a matching field of foo_confirmation. For example, if the field under validation is password, a matching password_confirmation field must be present in the input. Numeric :The field under validation must be numeric. NoteI intend to keep adding more rules to the package but If you have any additional rules you will like me to add to this, you can reach out to me or open an issue in that regard. How can I thank you?Why not star the github repo? I'd love the attention! Why not share the link for this repository on Twitter or HackerNews? Spread the word! Don't forget to follow me on twitter || or on medium Thanks! Edward Paul. ChangelogPlease see CHANGELOG for more information what has changed recently. ContributingPlease see CONTRIBUTING for details. Bug & FeaturesIf you have spotted any bugs, or would like to request additional features from the library, please file an issue via the Issue Tracker on the project's Github page: https://github.com/infinitypaul/php-validator/issues. LicenseThe MIT License (MIT). Please see License File for more information. |