Pvalidate: Validation Builder

We can build a validator step by step by adding one rule at a time, and then finally parse data.

Examples

Here are a few examples of using the rules individually to parse and validate data-

Required Validation

To validate if a field is required, use the Required rule. This example demonstrates how to validate a null value, which should fail the validation.

<?php

require_once "vendor/autoload.php";

use Webhkp\Pvalidate\ValidationBuilder;

$validation = ValidationBuilder::required()->gt(11)->gte(200)->range(min: 1, max: 1000)->safeParse(11);


var_dump($validation->isValid());
var_dump($validation->getErrors());
//var_dump($validation->getMessages());

Output:

bool(false)
array(2) {
  ["gt"]=>
  array(3) {
    ["value"]=>
    int(11)
    ["valid"]=>
    bool(false)
    ["errors"]=>
    array(1) {
      ["min"]=>
      string(26) " should be larger than  11"
    }
  }
  ["gte"]=>
  array(3) {
    ["value"]=>
    int(11)
    ["valid"]=>
    bool(false)
    ["errors"]=>
    array(1) {
      ["min"]=>
      string(38) " should be larger than or equal to 200"
    }
  }
}

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.