PHP code example of krubio / perfect-validation

1. Go to this page and download the library: Download krubio/perfect-validation 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/ */

    

krubio / perfect-validation example snippets


class Validator {
    private ValidationStrategy $strategy;

    public function __construct(ValidationStrategy $strategy);
    public function setValidationStrategy(ValidationStrategy $strategy): void;
    public function validateData($data): mixed;
    public function validateDataWithErrors($data): array;
}

// Create a validator with a specific validation strategy
$validator = new Validator($ValidationStrategy);

// Perform data validation
$data = $_POST; // Example data to be validated
$result = $validator->validateData($data);

// Perform data validation with errors
$resultWithErrors = $validator->validateDataWithErrors($data);

// Check the validation result
if ($result === true) {
    echo "Validation passed! The data is valid.";
} elseif (isset($resultWithErrors['errors'])) {
    echo "Validation failed! Errors: " . implode(', ', $resultWithErrors['errors']);
} else {
    echo "Validation failed! Please check the data for errors.";
}