PHP code example of gravity / dataverify

1. Go to this page and download the library: Download gravity/dataverify library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gravity / dataverify example snippets


use Gravity\DataVerify;

$data = (object)[
    'email' => '[email protected]',
    'age' => 25
];

$dv = new DataVerify($data);
$dv
    ->field('email')->

$dv->field('email')->

$dv->field('user')->e')->>

$dv->field('vat_number')
    ->when('country', '=', 'FR')
    ->then->

// Register reusable validation rules
DataVerify::registerRules('strongPassword')
    ->minLength(12)
    ->containsUpper
    ->containsLower
    ->containsSpecialCharacter;

// Apply to any field
$dv->field('password')->rule('strongPassword');

// Register complete validation schemas
DataVerify::registerSchema('userApi')
    ->field('user')->

class SiretStrategy extends ValidationStrategy {
    public function getName(): string { return 'siret'; }
    protected function handler(mixed $value): bool { /* validation logic */ }
}

DataVerify::global()->register(new SiretStrategy());
$dv->field('siret')->siret; // Available everywhere

// POST /api/users/register
$data = json_decode(file_get_contents('php://input'));

$dv = new DataVerify($data);
$dv
    ->field('email')->e')
        ->when('account_type', '=', 'business')
        ->then-> false)) { // Fail-fast for better performance
    http_response_code(400);
    echo json_encode(['errors' => $dv->getErrors()]);
    exit;
}

// E-commerce checkout form
$dv = new DataVerify($_POST);
$dv
    ->field('shipping_method')->hen->uired->string
    ->field('pickup_store')
        ->when('shipping_method', '=', 'pickup')
        ->then->      echo "<p class='error'>{$error['message']}</p>";
    }
}

// Process CSV import with 10,000 rows
foreach ($csvRows as $row) {
    $dv = new DataVerify($row);
    $dv
        ->field('sku')->rify(batch: false)) { // Stop at first error per row
        $failedRows[] = ['row' => $row, 'errors' => $dv->getErrors()];
        continue; // Skip invalid row
    }
    
    // Process valid row...
}

// SaaS subscription validation
$dv = new DataVerify($subscription);
$dv
    ->field('plan')->in(['card', 'invoice'])
    ->field('card_number')
        ->when('plan', '!=', 'free')
        ->and('payment_method', '=', 'card')
        ->then->uired->int->between(1, 1000);

$dv = new DataVerify($data);
$dv->setLocale('fr'); // Built-in: EN, FR

$dv->field('email')-> adresse email valide"
    echo $dv->getErrors()[0]['message'];
}

// Add custom translations
$dv->addTranslations([
    'validation.siret' => 'El campo {field} debe ser un SIRET válido'
], 'es');

$dv = new DataVerify($data);
$dv
    ->field('name')->e')->int->between(18, 120);

if (!$dv->verify()) {
    print_r($dv->getErrors());
}

$dv->verify();              // Batch mode: all errors
$dv->verify(batch: false);  // Fail-fast: first error only (2x faster)

$dv->field('phone')->string;  // Optional: null OK, if present must be valid
$dv->field('email')->

$dv->field('password')
    ->sage('Password too weak - min 8 characters')
    ->containsUpper->errorMessage('Password must 

// Simple object nesting
$dv->field('user')->        ->subfield('address')->untry')->field('orders')->res (arrays + objects)
// Validates: data.warehouses[0].inventory[1].product.sku
$dv->field('warehouses')->

$data->email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
$data->name = htmlspecialchars($_POST['name'], ENT_QUOTES, 'UTF-8');

$dv = new DataVerify($data);
$dv->field('email')->

// ✅ Correct
foreach ($users as $user) {
    $dv = new DataVerify($user); // New instance
    $dv->field('email')->); // Error: already verified

foreach ($data->items as $item) {
    $dv = new DataVerify($item);
    $dv->field('sku')->     $errors[] = $dv->getErrors();
    }
}