PHP code example of corneltek / validationkit

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

    

corneltek / validationkit example snippets


$validtor = new PasswordValidator(array( /* options */ ), array( 
    '',
    'max_length_error' => 'The maximum length of password is 24 charactors.'
));

return $this->invalid('

return $this->valid('

$msgs = $validator->getMessages();
foreach( $msgs as $msgId => $msg ) {
    // $msg => ValidationMessage
}

use ValidationKit\EmailValidator;
$validator = new EmailValidator;
if( $validator->validate('[email protected]') ) {
    echo "Success!\n";
} else {
    foreach( $validator->getMessages() as $msgId => $msg ) {
        // $msg is a ValidationMessage object, 
        // which supports __toString() convertion.
        echo $msg . "\n";
    }
}

    use ValidationKit\PatternValidator;
    $validator = new PatternValidator( '#test test test#' );
    $bool = $validator->validate( $value );
    $msgs = $validator->getMessages();

$validator = new ValidationKit\StringLengthValidator(array( 
    'min' => 5, 'max' => 10,
));

    use ValidationKit\StringValidator;
    $validator = new StringValidator(array( 
            'starts_with' => '....' , 
            'ends_with' => ... ,
            'is' => ...,
            'contains' => ...,
            'except' => ...,
        ), array( 
            'invalid' => 'general invalid message',
            'starts_with_error' => 'error message'
        ));
    $bool = $validator->validate( $string );
    $msgs  = $validator->getMessages();
    foreach( $msgs as $msgId => $msg ) {
        echo $msg, "\n";
    }

$validator = new ValidationKit\PasswordValidator(array(
    'max_length' => 24,
    'min_length' => 10,
    ',
    '

    use ValidationKit\RangeValidator;
    $validator = new RangeValidator(array(
        'greater_than' => 100,
        'less_than' => 200,
    ));
    $bool = $validator->validate( 200 );

    $validator = new RangeValidator(array( '>' => 10 , '<' => 200 ));
    $bool = $validator->validate( 10.0 );

namespace YourApp;
use ValidationKit\Validator;

class YourValidator extends Validator
{
    public function validate($val) 
    {
        return $this->valid();
    }
}

namespace YourApp;
use ValidationKit\Validator;

class YourValidator extends Validator
{
    public function getDefaultMessages()
    {
        return array(
            'valid' => 'Its a valid value.',
            'too_large' => 'Your value is too large',
            'too_small' => 'Your value is too small',
        );
    }


    public function validate($val) 
    {
        if( $val > 30 )
            return $this->invalid('too_large');

        if( $val < 10 )
            return $this->invalid('too_small');
        return $this->valid();
    }
}