PHP code example of madesimple / php-form-validator

1. Go to this page and download the library: Download madesimple/php-form-validator 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/ */

    

madesimple / php-form-validator example snippets


// Rules Set
$rulesSet = [
    'username'         => 'ed-with:lastName|max-str-len:255',
    'lastName'         => 'likes.*.rating|max-str-len:50'
    'likes.*.stars'    => ' 'postCode' => 'AB12 3CD',
    ]
    'likes' => [
        [
            'item'  => 'php-form-validator',
            'stars' => 5
        ]
    ]
];

// Add the rule to the validator
$validator = new Validator;
$validator->addRule('timezone', function (Validator $validator, array $data, $pattern, $rule) {
    foreach ($validator->getValues($data, $pattern) as $attribute => $value) {
        if (null === $value) {
            continue;
        }
        if (in_array($value, listTimezones())) {
            continue;
        }

        $validator->addError($attribute, $rule);
    }
});
$validator->setRuleMessage('timezone', ':attribute must be an timezone');


// Validate using the new rule
$rules = [
    'timezone' => 'present|timezone',
];
$validator->validate($_POST, $rules);

// Add the rule to the validator
$validator = new Validator;
$validator->addRule('model-exists', function (Validator $validator, array $data, $pattern, $rule, $array $parameters) {
    // Connect to database
    $db = getDbInstance();
    list($model, $property) = $parameters;

    foreach ($validator->getValues($data, $pattern) as $attribute => $value) {
        if (null === $value) {
            continue;
        }
        if (doesModelExist($db, $model, $property, $value)) {
            continue;
        }

        $validator->addError($attribute, $rule, [
            ':model' => $model
        ]);
    }
});
$validator->setRuleMessage('model-exists', ':attribute must be an existing :model');


// Validate using the new rule
$rules = [
    'uuid' => '

// Add the rule to the validator
$validator = new Validator;
$validator->addRule('complex', function (Validator $validator, array $data, $pattern, $rule, $array $parameters) {
    foreach ($validator->getValues($data, $pattern) as $attribute => $value) {
        if (null === $value) {
            continue;
        }
        $rules['type'] = 'in:alpha,beta';
        switch ($value['type']) {
            case 'alpha':
                $rules['shared_field'] = 'is:int';
                $rules['alpha_specific_field'] = 'is:int';
                break;

            case 'beta':
                $rules['shared_field'] = 'in:blue,green';
                $rules['beta_specific_field'] = 'is:int';
                break;
        }

        // Apply the type specific rules to this part of the data
        $validator->validate($value, $rules, $attribute);
    }
});
// No need to define a rule message as only sub-rules can generate errors

// Example usage:
$rulesSet = [
    'field' => 'present',
];

// Example Usage
$rulesSet = [
    'field' => '

// Example Usage
$rulesSet = [
    'field0' => '

// Example Usage
$rulesSet = [
    'field0' => '

// Example Usage
$rulesSet = [
    'field0' => 'pha,beta'
];

// Example Usage
$rulesSet = [
    'field0' => 'pha,beta'
];

// Example Usage
$rulesSet = [
    'field0' => '

// Example Usage
$rulesSet = [
    'field0' => 'equals:field1',
    'field1' => '

// Example Usage
$rulesSet = [
    'field0' => 'not-equals:field1',
    'field1' => '

// Example Usage
$rulesSet = [
    'field0' => 'identical:field1',
    'field1' => '

// Example Usage
$rulesSet = [
    'field0' => 'not-identical:field1',
    'field1' => '

// Example Usage
$rulesSet = [
    'field' => 'in:apple,pear,orange'
];

// Example Usage
$rulesSet = [
    'field' => 'not-in:apple,pear,orange'
];

// Example Usage
$rulesSet = [
    'field' => 'contains:apple,pear,orange'
];

// Example Usage
$rulesSet = [
    'field' => 'contains-only:apple,pear,orange'
];

// Example Usage
$rulesSet = [
    'field' => 'min-arr-count:1'
];

// Example Usage
$rulesSet = [
    'field' => 'max-arr-count:5'
];

// Example Usage
$rulesSet = [
    'field' => 'min:1'
];

// Example Usage
$rulesSet = [
    'field' => 'max:5'
];

// Example Usage
$rulesSet = [
    'field0' => 'greater-than:field1'
];

// Example Usage
$rulesSet = [
    'field0' => 'less-than:field1'
];

// Example Usage
$rulesSet = [
    'field' => 'alpha'
];

// Example Usage
$rulesSet = [
    'field' => 'alpha-numeric'
];

// Example Usage
$rulesSet = [
    'field' => 'min-str-len:3'
];

// Example Usage
$rulesSet = [
    'field' => 'max-str-len:50'
];

// Example Usage
$rulesSet = [
    'field' => 'str-len:10'
];

// Example Usage
$rulesSet = [
    'field' => 'human-name'
];

// Example Usage
$rulesSet = [
    'field' => 'is:numeric' // any basic PHP type (must have corresponding is_<type> method)
];

// Example Usage
$rulesSet = [
    'field' => 'email'
];

// Example Usage
$rulesSet = [
    'field' => 'date' // defaults to 'Y-m-d'
];

// Example Usage
$rulesSet = [
    'field' => 'url'
];

// Example Usage
$rulesSet = [
    'field' => 'uuid'
];

// Example Usage
$rulesSet = [
    'field' => 'card-number'
];

// Example Usage
$rulesSet = [
    'field' => ['regex:/^[Ff]oobar[1!]+$/']
];

// Example Usage
$rulesSet = [
    'field' => ['not-regex:/^[abc]{1,3}\W+$/i']
];

"madesimple/php-form-validator": "^2.8"
shell
composer