Git Product home page Git Product logo

simple-field-validator's Introduction

simple-field-validator

Installing ? using composer

composer require johnbrightn/simple-field-validator

If you get an error of no class found, run the following command to optimize autoload

composer dump -o

Usage

Mostly used with API where request objects are received as array.

    $userInputs = $request->getParsedBody(); //object received from user/frontend

 //specify required fields, key=>maxLength. value is the maximum required length of every field/key
 
    $requiredFields = ["first_name"=>25, "last_name"=>25, "email"=>60, "username"=>15, "password"=>20];
    
    $value = ValidateFields::validate($requiredFields, $userInputs); //method to validate fields
Method params descriptions
ValidateFields::validate($requiredFields, $requestObject, $customMessage=null) 

parameter description
$requiredFields array - specified fields that you're expecting from the user/front end
$requestObj array - array of fields of key/values from the user/front end
$customMessage optional parameter. array or custom messages to return when validation fails \ \

Example usage

require_once __FILE__ . '/vendor/autoload.php';

use Jbn\Validate\ValidateFields;


$app->post('/register', function (Request $request, Response $response) {

    $userInputs = $request->getParsedBody(); //object received from user/frontend
    
    //if there are optional fields, that do not need validation, do not specify them in the required fields array
   /* $middle_name = null;
    if(isset($request->getParsedBody()['middle_name']))
        $middle_name = $request->getParsedBody()['middle_name'];
   */
    //specified required fields, key=>maxLength. value is the maximum required length of every field/key
    $requiredFields = ["first_name"=>25, "last_name"=>25, "email"=>60, "username"=>15, "password"=>20];
    
    $value = ValidateFields::validate($requiredFields, $userInputs);
   if ($value["error"]) { //if validation fails, return response
        return $response->withJson($value);
    } else {
        //if validation succeeds, u can either use the returned values or the user input values which may include optional fields
        $value; // values retured from the array, which include required fields only
        $userInput //values sent by the user which may include optional fields
        
        ... do other works with the valid fields
        $result = $db->registerUser($value);
        
        return $response->withJson($result);
    }
});

If validation fails, array returned is

//if all required fields are not present
["error"=>true, "message"=> "No field is set"];

//if a required field is empty
["error"=>true, "message"=> "$field_name should not be empty"];

//if a required field has length more than specified
["error"=>true, "message"=> "$field_name should be $maxLength characters or less"];

//email validation. if email address is present, field should be specified as 'email',
["error"=>true, "message"=> "Invalid email address"];

If validation succeeds, returns all the fields together with their values

["error"=>false, $all_fields=>$all_values];

You can specify your custom messages when the value is empty or field length is more than the specified as a third parameter

    $userInputs = $request->getParsedBody();
    
    //specified required fields, key=>maxLength. value is the maximum required length of every field/key
    $requiredFields = ["first_name"=>25, "last_name"=>25, "username"=>15, "password"=>20];
    
    $customMessage = ["Oops! First Name should not be blank and not more than 25 characters", "Enter your last name and not more than 25 characters", "Enter a username of 15 characters or less", "Password should not exceed 20 characters"];
    
    $value = ValidateFields::validate($requiredFields, $userInputs, $customMessage);

simple-field-validator's People

Contributors

johnbrightn avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.