Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/anandpilania/f3-validator
Validation package for FatFreeFramework.
https://github.com/anandpilania/f3-validator
f3 fat-free-framework fatfree validation-library validator
Last synced: about 1 month ago
JSON representation
Validation package for FatFreeFramework.
- Host: GitHub
- URL: https://github.com/anandpilania/f3-validator
- Owner: AnandPilania
- License: other
- Created: 2017-09-16T18:38:38.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-05-16T09:09:31.000Z (over 3 years ago)
- Last Synced: 2024-11-13T17:08:38.588Z (about 1 month ago)
- Topics: f3, fat-free-framework, fatfree, validation-library, validator
- Language: PHP
- Size: 20.5 KB
- Stars: 5
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
# f3-validator
[![Hits](https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fgithub.com%2FAnandPilania%2Ff3-validator&count_bg=%23FF3863&title_bg=%232C3E50&title=hits&edge_flat=false)](https://hits.seeyoufarm.com)
Easy to use Validation package for Fatfree-framework with using F3 built-in translations. You can also use it directly in the model.
**NOTE:** This package port some classes and ideas from [illuminate/validator](https://github.com/illuminate/validator).
**NOTE:** SAMPLE DICT/LANG FILE IS INCLUDED AS `en.php.sample`, use this for reference.## Usage
- 1: Initializing, passing data and validate:
`$validator = new Validator(
array('name' => 'Anand Pilania', 'email' => '[email protected]'),
array('name' => 'required|min:3|max:60', 'email' => 'required|unique:Models\User|email')
)->validate();`
**NOTE:** You can also pass more data to the `validate` method.
* Another method:
`$validator = Validator::instance()->validate(
array('name' => 'Anand Pilania', 'email' => '[email protected]'),
array('name' => 'required|min:3|max:60', 'email' => 'required|unique:NAMESPACED_MODEL_CLASS_OR_CONFIG_MODEL.PARAM`|email')
);`
- 2: Get validation status (return -> true/false):`$validator->passed();`
- 3: If validation failed, retrieve the failed attribute with formatted messages/errors (return -> array):`$validator->errors();`
## Setup
- `composer require anandpilania/f3-validator`
## Rules### Used without paramater
* `boolean` OR `bool`, `string`, `integer` OR `int`, `array`, `numeric`, `alpha`, `alpha_num`, `alpha_dash`,
* `url`, `nullable`, `json`, `required`, `confirmed`, `active_url`, `email`, `ip`, `ipv4`, `ipv6`, `filled`
### Used with parameters
* `min`, `max`, `size`, `between` - `min:6`, `max:255`, `size:3`, `between:1,3`
* `unique` - `unique:NAMESPACED_MODEL_CLASS_OR_CONFIG_MODEL`
* ...## USING IN MODEL
- RECOMMENDED : Validate user input in controllers is the recommended way### CONTROLLER
`$validator = Validator::instance()->validate($f3->get('POST'), array(
'field' => 'required|CHECK_PARAM' // EG: 'email' => 'required|email|unique:Models\User'
));`
`if(!$validator->passed()) {
return/flash $validator->errors();
}`
## USING IN MODEL
- You can also use this validator directly into your model:
FOR EX: Validatin on `beforesave` trigger: (This example is used with [ikkez/f3-cortex](https://github.com/ikkez/f3-cortex))### Model
`protected $validator;
public function __construct() {
parent::__construct();
$saveHandler = function() {
foreach($this->getFieldConfiguration() as $field => $conf) {
if(isset($conf['validate'])) {
$rules[$field] = $conf['validate'];
$data[$field] = $this->get($field);
if(Validator::instance()->str_contains($conf['validate'], 'confirmed')) {
$confirmation = $field.'_confirmation';
$data[$field.'_confirmation'] = null !== $f3->get('POST.'.$confirmation)?$f3->get('POST.'.$confirmation):$f3->get('GET.'.$confirmation);
}
}
}
$this->validator = Validator::instance()->validate($data, $rules);
return $this->validator->passed();
};
$this->beforesave($saveHandler);
}`### Controller
`$model = new MODEL;
$model->copyFrom($f3->get('POST'), array_keys($model->fieldConf));
$mode->save();
if($model->validator->passed()) {
// NO ERRORS
}else{
// VAIDATION FAILED
$errors = $model->validator->errors();
}`
--
http://about.me/anandpilania### USING UNIQUE VALIDATOR (2 WAYS)
* Simply pass the NAMESPACED_MODEL_CLASS => `unique:Models\User`
* Define in config as =>
INI => `MODEL.USERS = Models\User`
HIVE => $f3->set('MODEL.USERS', 'Models\User');
and use it as `unique:users`