Skip to content

Commit

Permalink
Add: Validations
Browse files Browse the repository at this point in the history
  • Loading branch information
rizwan3d3 committed Sep 28, 2023
1 parent 8ac9551 commit 68f37c7
Show file tree
Hide file tree
Showing 5 changed files with 454 additions and 1 deletion.
44 changes: 44 additions & 0 deletions .github/workflows/releases.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Release

on:
push:
tags:
- '*'
jobs:
build:
permissions: write-all
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

# Archive the package
- name: Create archive
uses: vimtor/action-zip@v1
with:
files: ./
dest: ValidationMyPhp.zip

# Create the release: https://github.com/actions/create-release
- name: Create release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
draft: false
prerelease: false

# Upload release asset: https://github.com/actions/upload-release-asset
- name: Update release asset
id: upload-release-asset
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }} # This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps
asset_path: ./ValidationMyPhp.zip
asset_name: ValidationMyPhp.zip
asset_content_type: application/zip
91 changes: 90 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,90 @@
# ValidationMyPhp
# ValidationMyPhp
## Introduction

The `Validation` class is a PHP utility for performing data validation and error handling. It is designed to validate user-provided data against a set of rules and return error messages when validation fails.

## Usage

### Installation

#### Composer

You can easily install the `Validation` class and its dependencies using Composer. If you haven't already installed Composer globally, you can do so by following the instructions on the [Composer website](https://getcomposer.org/download/).

Once Composer is installed, you can add the `rizwan3d/SharpRISCV` package to your project by running the following command in your project's root directory:

```bash
composer require rizwan3d/SharpRISCV
```

### Initialization

To use the `Validation` class, you first need to initialize it and set up your database connection parameters if needed. Here's an example of how to do it:

```php
use ValidationMyPhp\Validation;

Validation::$DB_HOST = '127.0.0.1';
Validation::$DB_NAME = 'database';
Validation::$DB_PASSWORD = '';
Validation::$DB_USER = 'root';

$validation = new Validation();
```

### Data Validation

Once the `Validation` object is created, you can validate data using the `validate` method. You need to provide the data to validate, validation rules, and optional custom error messages. Here's an example of how to validate data:

```php
$data = [
'firstname' => '',
'username' => '33158413',
'address' => 'This is my address',
'zipcode' => '1',
'email' => 'jo@',
'password' => 'test123',
'password2' => 'test',
];

$fields = [
'firstname' => 'required | max:255',
'lastname' => 'required| max: 255', // Note: 'lastname' field is missing in the data
'address' => 'required | min: 10, max:255',
'zipcode' => 'between: 5,6',
'username' => 'required | alphanumeric | between: 3,255 | unique: users,username',
'password' => 'required | secure',
'password2' => 'required | same:password'
];

$errors = $validation->validate($data, $fields, [
'required' => 'The %s is required',
'password2' => ['same'=> 'Please enter the same password again']
]);

print_r($errors);
```

In the example above, the `validate` method will return an array of error messages for fields that failed validation.

### Validation Rules

You can specify various validation rules for each field in the `fields` array. Here are some common validation rules:

- `required`: The field must not be empty.
- `max:X`: The field's length must not exceed X characters.
- `min:X`: The field's length must be at least X characters.
- `between:X,Y`: The field's length must be between X and Y characters.
- `alphanumeric`: The field must contain only alphanumeric characters.
- `unique:table,column`: Check if the field value is unique in the specified database table and column.
- `email`: Validate if the field is a valid email address.
- `secure`: Validate if the field contains a secure password (custom rule).
- `same:field_name`: Validate if the field is the same as another field (e.g., password confirmation).

### Custom Error Messages

You can provide custom error messages for each validation rule. In the example, custom error messages are defined in the third argument of the `validate` method. You can use `%s` as a placeholder for the field name in error messages.

## Conclusion

The `Validation` class simplifies the process of validating user input data in PHP applications. It allows you to define validation rules, apply them to data, and retrieve error messages for fields that fail validation. This helps improve the security and reliability of your application by ensuring that user input meets your criteria.
20 changes: 20 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "rizwan3d/ValidationMyPhp",
"description": "PHP validation library",
"type": "library",
"require": {
"php": ">=7.1"
},
"license": "MIT",
"authors": [
{
"name": "Muhammad Rizwan",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"ValidationMyPhp\\": "src/ValidationMyPhp/"
}
}
}
44 changes: 44 additions & 0 deletions example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

<?php

require_once('src/ValidationMyPhp/Validation.php');

use ValidationMyPhp\Validation;

Validation::$DB_HOST = '127.0.0.1';
Validation::$DB_NAME = 'database';
Validation::$DB_PASSWORD = '';
Validation::$DB_USER = 'root';

$validation = new Validation();

$data = [
'firstname' => '',
'username' => '33158413',
'address' => 'This is my address',
'zipcode' => '1',
'email' => 'jo@',
'password' => 'test123',
'password2' => 'test',
];



$fields = [
'firstname' => 'required | max:255',
'lastname' => 'required, max: 255',
'address' => 'required | min: 10, max:255',
'zipcode' => 'between: 5,6',
'username' => 'required | alphanumeric | between: 3,255 | unique: users,username',
'email' => 'required | email | unique: users,email',
'password' => 'required | secure',
'password2' => 'required | same:password'
];


$errors = $validation->validate($data, $fields, [
'required' => 'The %s is required',
'password2' => ['same'=> 'Please enter the same password again']]
);

print_r($errors);
Loading

0 comments on commit 68f37c7

Please sign in to comment.