jinjie/silverstripe-valitron

Better SilverStripe DataObject Validation With Valitron

Installs: 270

Dependents: 3

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 0

Open Issues: 0

Type:silverstripe-vendormodule

v2.0.2 2019-01-02 04:36 UTC

This package is auto-updated.

Last update: 2024-10-29 04:04:07 UTC


README

A very simple validation that make use of Valitron. Provides a helper that add error messages to form fields.

use SilverStripe\ORM\DataObject;
use SwiftDevLabs\Valitron\Helper;

class MyDataObject extends DataObject {
    // Validates DataObject
    public function validate()
    {
        $result = parent::validate();

        $v = new \Valitron\Validator([
            'Field1'    => $this->Field1,
            'Field2'    => $this->Field2,
        ]);

        // Find more rules at https://github.com/vlucas/valitron
        $v->rule('required', [
            'Field1',
            'Feild2',
        ]);

        return Helper::validate($v, $result);
    }
}