PHP code example of phalcon / incubator-validation

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

    

phalcon / incubator-validation example snippets


use Phalcon\Forms\Element\Hidden;
use Phalcon\Validation\Validator\ReCaptcha;

$reCaptcha = new Hidden('g-recaptcha-response');

$reCaptcha->setLabel('reCAPTCHA')->addValidators(
    [
        new ReCaptcha(
            [
                'message' => 'The captcha is not valid',
                'secret'  => 'your_site_key',
            ]
        ),
    ]
);

$this->add($reCaptcha);

$data['ip'] = $this->request->getPost('ip');

$validation = new \Phalcon\Validation();

$validation->add(
    'ip',
    new \MicheleAngioni\PhalconValidators\IpValidator(
        [
            'message' => 'The IP is not valid.', // Optional
        ]
    )
);

$messages = $validation->validate($data);

if (count($messages)) {
    // Some error occurred, handle messages
}

$data['number'] = $this->request->getPost('number');

$validation = new \Phalcon\Validation();

$validation->add(
    'number',
    new \MicheleAngioni\PhalconValidators\NumericValidator(
        [
            'allowFloat' => true,                                        // Optional, default: false
            'allowSign' => true,                                         // Optional, default: false
            'min' => 2,                                                  // Optional
            'min' => 2,                                                  // Optional
            'max' => 50,                                                 // Optional
            'message' => 'Only numeric (0-9,.) characters are allowed.', // Optional
            'messageMinimum' => 'The value must be at least 2',          // Optional
            'messageMaximum' => 'The value must be lower than 50',       // Optional
        ]
    )
);

$messages = $validation->validate($data);

if (count($messages)) {
    // Some error occurred, handle messages
}

// Validation succeeded without errors

$data['text'] = $this->request->getPost('text');

$validation = new \Phalcon\Validation();

$validation->add(
    'text',
    new \MicheleAngioni\PhalconValidators\AlphaNumericValidator(
        [
            'whiteSpace'     => true,                                            // Optional, default false
            'underscore'     => true,                                            // Optional, default false
            'min'            => 6,                                               // Optional
            'max'            => 30,                                              // Optional
            'message'        => 'Validation failed.',                            // Optional
            'messageMinimum' => 'The value must contain at least 6 characters.', // Optional
            'messageMaximum' => 'The value can contain maximum 30 characters.',  // Optional
        ]
    )
);

$messages = $validation->validate($data);

if (count($messages)) {
    // Some error occurred, handle messages
}

// Validation succeeded without errors

$data['text'] = $this->request->getPost('text');

$validation = new \Phalcon\Validation();

$validation->add(
    'text',
    new \MicheleAngioni\PhalconValidators\AlphaNamesValidator(
        [
            'numbers'        => true,                                            // Optional, default false
            'min'            => 6,                                               // Optional
            'max'            => 30,                                              // Optional
            'message'        => 'Validation failed.',                            // Optional
            'messageMinimum' => 'The value must contain at least 6 characters.', // Optional
            'messageMaximum' => 'The value can contain maximum 30 characters.',  // Optional
        ]
    )
);

$messages = $validation->validate($data);

if (count($messages)) {
    // Some error occurred, handle messages
}

// Validation succeeded without errors

$data['text'] = $this->request->getPost('text');

$validation = new \Phalcon\Validation();

$validation->add(
    'text',
    new \MicheleAngioni\PhalconValidators\AlphaCompleteValidator(
        [
            'allowBackslashes' => true,                                            // Optional
            'allowPipes'       => true,                                            // Optional
            'allowUrlChars'    => true,                                            // Optional
            'min'              => 6,                                               // Optional
            'max'              => 30,                                              // Optional
            'message'          => 'Validation failed.',                            // Optional
            'messageMinimum'   => 'The value must contain at least 6 characters.', // Optional
            'messageMaximum'   => 'The value can contain maximum 30 characters.',  // Optional
        ]
    )
);

$messages = $validation->validate($data);

if (count($messages)) {
    // Some error occurred, handle messages
}

// Validation succeeded without errors