PHP code example of silverware / validator

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

    

silverware / validator example snippets


use SilverWare\Validator\Validator;

use SilverWare\Validator\Rules\LengthRule;
use SilverWare\Validator\Rules\PatternRule;
use SilverWare\Validator\Rules\RequiredRule;

public function Form()
{
    $fields  = FieldList::create([
        ...
    ]);
    
    $actions = FieldList::create([
        ...
    ]);
    
    $validator = Validator::create();
    
    // define validator rules here!
    
    $form = Form::create($this, 'Form', $fields, $actions, $validator);
    
    return $form;
}

$validator->setRule(
    'MyFieldName',
    LengthRule::create(10, 20) // this field's value must be 10-20 characters in length
);

$validator->setRules(
    'MyDomainName',
    [
        RequiredRule::create(),  // this field is 

$validator->addRequiredField('MyRequiredField');
$validator->addRequiredField('MyOtherRequiredField', 'Dis field be mandatory, yo!');

$validator->addRequiredFields([
    'ThisFieldIsRequired',
    'SoIsThisField',
    'AndThisField'
]);

$validator->setRule(
    'MyAge',
    MaxRule::create(130)->setMessage(
        'I find it hard to believe you are over 130 years old!'
    )
)

$validator->setClientSide(false);  // disables client-side validation
$validator->setServerSide(false);  // disables server-side validation

$validator->getBackend()->setTriggerOn([
    'focusin',
    'focusout'
]);

$validator->getBackend()->setTriggerOn('focusin focusout');

use SilverWare\Validator\Rules\AlphaNumRule;

$validator->setRule(
    'MyFieldName',
    AlphaNumRule::create()
);

use SilverWare\Validator\Rules\CheckRule;

$validator->setRule(
    'MyCheckboxSet',
    CheckRule::create(2, 4)  // user must select between 2 and 4 options
);

use SilverWare\Validator\Rules\ComparisonRule;

$validator->setRule(
    'MyLesserValue',
    ComparisonRule::create(
        ComparisonRule::GREATER_THAN,
        'MyGreaterValue'
    )
);

use SilverWare\Validator\Rules\DateRule;

$validator->setRule(
    'MyDateField',
    DateRule::create('YYYY-MM-DD')  // ensures an ISO standard date, e.g. 2017-05-12
);

use SilverWare\Validator\Rules\DigitsRule;

$validator->setRule(
    'MyDigitsField',
    DigitsRule::create()
);

use SilverWare\Validator\Rules\DomainRule;

$validator->setRule(
    'MyDomainName',
    DomainRule::create()
);

use SilverWare\Validator\Rules\EmailRule;

$validator->setRule(
    'MyEmailField',
    EmailRule::create()
);

use SilverWare\Validator\Rules\EqualToRule;

$validator->setRule(
    'MyFirstValue',
    EqualToRule::create('MySecondValue')
);

use SilverWare\Validator\Rules\IntegerRule;

$validator->setRule(
    'MyIntField',
    IntegerRule::create()
);

use SilverWare\Validator\Rules\LengthRule;

$validator->setRule(
    'MyFieldName',
    LengthRule::create(20, 40)  // value must be between 20-40 characters in length
);

use SilverWare\Validator\Rules\MaxCheckRule;

$validator->setRule(
    'MyCheckboxSet',
    MaxCheckRule::create(4)  // user can select a maximum of 4 options
);

use SilverWare\Validator\Rules\MaxLengthRule;

$validator->setRule(
    'MyFieldName',
    MaxLengthRule::create(40)  // value must be 40 characters in length or less
);

use SilverWare\Validator\Rules\MaxRule;

$validator->setRule(
    'MyNumberField',
    MaxRule::create(255)  // number value must be 255 or less
);

use SilverWare\Validator\Rules\MaxWordsRule;

$validator->setRule(
    'MyTextArea',
    MaxWordsRule::create(50) // value must be 50 words or less
);

use SilverWare\Validator\Rules\MinCheckRule;

$validator->setRule(
    'MyCheckboxSet',
    MinCheckRule::create(2)  // user must select at least 2 options
);

use SilverWare\Validator\Rules\MinLengthRule;

$validator->setRule(
    'MyFieldName',
    MinLengthRule::create(20)  // value must be at least 20 characters in length
);

use SilverWare\Validator\Rules\MinRule;

$validator->setRule(
    'MyNumberField',
    MinRule::create(42)  // the entered value must be 42 or greater
);

use SilverWare\Validator\Rules\MinWordsRule;

$validator->setRule(
    'MyTextArea',
    MinWordsRule::create(20)  // value must contain at least 20 words
);

use SilverWare\Validator\Rules\NotEqualToRule;

$validator->setRule(
    'MyFirstValue',
    NotEqualToRule::create('MySecondValue')
);

use SilverWare\Validator\Rules\NumberRule;

$validator->setRule(
    'MyNumericValue',
    NumberRule::create()  // value must be numeric
);

use SilverWare\Validator\Rules\PatternRule;

$validator->setRule(
    'MyFieldName',
    PatternRule::create('/ware$/')  // ensures the entered string ends with 'ware'
);

use SilverWare\Validator\Rules\RangeRule;

$validator->setRule(
    'MyNumericValue',
    RangeRule::create(1, 5)  // value must be between 1 and 5
);

use SilverWare\Validator\Rules\RemoteRule;

$rule = RemoteRule::create(
    $url,  // url to call
    $params,  // request parameters, e.g. ['myVar' => 123]
    $options, // options for request, e.g. ['type' => 'POST']
    $remoteValidator  // remote validator to use, 'default' or 'reverse'
);

$validator->setRule(
    'MyUserName',
    RemoteRule::create(
        $this->Link('checkusername')  // controller method
    )->setMessage('That username is unavailable')
);

$validator->setRule(
    'MyUserName',
    RemoteRule::create(
        $this->Link('checkusername')
    )->setRemoteValidator('reverse')
);

use SilverWare\Validator\Rules\RequiredRule;

$validator->setRule(
    'MyRequiredField',
    RequiredRule::create()
);

use SilverWare\Validator\Rules\URLRule;

$validator->setRule(
    'MyURL',
    URLRule::create()
);

use SilverWare\Validator\Rules\WordsRule;

$validator->setRule(
    'MyTextArea',
    WordsRule::create(10, 25)  // user must enter between 10 and 25 words
);