PHP code example of sheadawson / silverstripe-zenvalidator

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

    

sheadawson / silverstripe-zenvalidator example snippets


public function Form(){
    $fields = FieldList::create(array(
        TestField::create('Name'),
        TextField::create('Username'),
        TextField::create('Email'),
        TextField::create('FavoriteColor'),
        TextField::create('Age'),
        TextField::create('Website')
    ));

    $actions = FieldList::create(FormAction::create('submit', 'submit'));

    $validator = ZenValidator::create();

    return Form::create($this, 'Form', $fields, $actions, $validator);
}

$validator->addRequiredFields(array(
    'Name',
    'Email'
));

$validator->addRequiredFields(array(
    'Name'  => 'Please enter your name',
    'Email' => 'Please enter your email'
));

$validator->setConstraint('Age', Constraint_value::create('min', 18));

$validator->setConstraint('Age', Constraint_value::create('max', 25));

$validator->setConstraint('Age', Constraint_value::create('range', 18, 25));

$validator->setConstraint('Username', Constraint_length::create('min', 3));

$validator->setConstraint('Username', Constraint_length::create('max', 5));

$validator->setConstraint('Username', Constraint_length::create('range', 3, 5));

$validator->setConstraint('Options', Constraint_check::create('min', 3));

$validator->setConstraint('Options', Constraint_check::create('max', 5));

$validator->setConstraint('Options', Constraint_check::create('range', 3, 5));

$validator->setConstraint('Email', Constraint_type::create('email'));

$validator->setConstraint('Website', Constraint_type::create('url'));

$validator->setConstraint('Age', Constraint_type::create('number'));

$validator->setConstraint('Age', Constraint_type::create('integer'));

$validator->setConstraint('Age', Constraint_type::create('digits'));

$validator->setConstraint('Username', Constraint_type::create('alphanum'));

$validator->setConstraint('Username', Constraint_equalto::create('Name'));

$validator->setConstraint('Surname', Constraint_notequalto::create('FirstName'));

$validator->setConstraint('FavoriteColor', Constraint_regex::create("/^#(?:[0-9a-fA-F]{3}){1,2}$/"));

$validator->setConstraint('Username', Constraint_remote::create($this->Link('checkusername')));

public function checkusername($request) {
    $username = $request->requestVar('Username');

    // check for existing user with same username
    if (Member::get()->filter('Username', $username)->count()) {
        return $this->httpError(400, 'This member already exists');
    } else {
        return $this->getResponse()->setBody('');
    }
}

$validator->setConstraint(
    'FavoriteColor',
    Constraint_regex::create("/^#(?:[0-9a-fA-F]{3}){1,2}$/")
        ->setMessage('Please enter a valid HEX color code, starting with a #')
);

$validator->setConstraints(array(
    'Name' => 'Age', Constraint_value::create('min', 18),
    'Username' => array(
        Constraint_

$validator
    ->setConstraint('Website', Constraint_type::create('url'))
    ->setConstraint('Content', Constraint_

$validator->removeConstraint(string $fieldName, string $constraintClassname);

$validator->removeConstraints(string $fieldName);

$validator = ZenValidator::create(null, true, false);

public function getCMSValidator(){
    $validator = ZenValidator::create()->setConstraint(
        'Content',
        Constraint_return $validator;
}

$validator->setConstraint('HeroImage', Constraint_dimension::create('width', 100));

$validator->setConstraint('HeroImage', Constraint_dimension::create('height', 150));

$validator->setConstraint('HeroImage', Constraint_dimension::create('width_height', 100, 150));

$validator->setConstraint('HeroImage', Constraint_dimension::create('ratio', 16, 9));

$validator->setConstraint('HeroImage', Constraint_dimension::create('min_width', 50));

$validator->setConstraint('HeroImage', Constraint_dimension::create('min_height', 75));

$validator->setConstraint('HeroImage', Constraint_dimension::create('min_width_height', 50, 75));

$validator->setConstraint('HeroImage', Constraint_dimension::create('max_width', 300));

$validator->setConstraint('HeroImage', Constraint_dimension::create('max_height', 200));

$validator->setConstraint('HeroImage', Constraint_dimension::create('max_width_height', 300, 200));

$country->validateIf('EmailAddress')->isEqualTo('s')->orIf('EmailAddress')->isEqualTo('v');

$field->validateIf('IsChecked')->isEmpty();