PHPackages                             silverware/validator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. silverware/validator

ActiveSilverstripe-vendormodule[Validation &amp; Sanitization](/categories/validation)

silverware/validator
====================

SilverWare Validator Module.

1.2.0(7y ago)711.7k6[4 issues](https://github.com/praxisnetau/silverware-validator/issues)[1 PRs](https://github.com/praxisnetau/silverware-validator/pulls)4BSD-3-ClausePHPPHP &gt;=5.6.0

Since May 12Pushed 1y ago1 watchersCompare

[ Source](https://github.com/praxisnetau/silverware-validator)[ Packagist](https://packagist.org/packages/silverware/validator)[ Docs](https://github.com/praxisnetau/silverware-validator)[ RSS](/packages/silverware-validator/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (7)Dependencies (2)Versions (11)Used By (4)

SilverWare Validator
====================

[](#silverware-validator)

[![Latest Stable Version](https://camo.githubusercontent.com/1dbdb42858398cbe13b1050892034eb599016204c0a67703995902e7abd38d51/68747470733a2f2f706f7365722e707567782e6f72672f73696c766572776172652f76616c696461746f722f762f737461626c65)](https://packagist.org/packages/silverware/validator)[![Latest Unstable Version](https://camo.githubusercontent.com/9272c3e04d97168f32ae38239cb5b2f8de940ed219ccfb321910f50662abb753/68747470733a2f2f706f7365722e707567782e6f72672f73696c766572776172652f76616c696461746f722f762f756e737461626c65)](https://packagist.org/packages/silverware/validator)[![License](https://camo.githubusercontent.com/293f1f88d10e85ca34c6a14c785f78f2bdc44797f07c1106aec1d68adc06e253/68747470733a2f2f706f7365722e707567782e6f72672f73696c766572776172652f76616c696461746f722f6c6963656e7365)](https://packagist.org/packages/silverware/validator)

A form validation module for [SilverStripe v4](https://github.com/silverstripe/silverstripe-framework) which supports alternative client-side backends and customisable validation rules. Ships with [Parsley.js](http://parsleyjs.org) as the default validation backend. Configured out-of-the-box for use with [Bootstrap v4](https://v4-alpha.getbootstrap.com) forms.

Contents
--------

[](#contents)

- [Requirements](#requirements)
- [Installation](#installation)
- [Configuration](#configuration)
- [Usage](#usage)
- [Rules](#rules)
- [Issues](#issues)
- [To-Do](#to-do)
- [Contribution](#contribution)
- [Attribution](#attribution)
- [Maintainers](#maintainers)
- [License](#license)

Requirements
------------

[](#requirements)

- [SilverStripe Framework v4](https://github.com/silverstripe/silverstripe-framework)
- [Bootstrap v4](https://v4-alpha.getbootstrap.com) (to use with default configuration)

Installation
------------

[](#installation)

Installation is via [Composer](https://getcomposer.org):

```
$ composer require silverware/validator

```

Configuration
-------------

[](#configuration)

As with all SilverStripe modules, configuration is via YAML. The SilverStripe dependency injector is used to configure the validator backend. The module ships with a backend that uses [Parsley.js](http://parsleyjs.org):

```
SilverStripe\Core\Injector\Injector:
  ValidatorBackend:
    class: SilverWare\Validator\Backends\ParsleyBackend
```

The module has been designed to be as "backend agnostic" as possible. While Parsley.js is very powerful and should cater for most validation needs, you could certainly roll your own backend implementation and integrate it into the validator frontend using configuration.

Usage
-----

[](#usage)

To make use of the validator in your app, simply `use` the validator class in the header of your code:

```
use SilverWare\Validator\Validator;
```

You will also need to `use` the validator rule classes you wish to apply to your form, for example:

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

Create a validator instance in your controller method that returns the `Form`, for example:

```
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;
}
```

Now you can define your validator rules using the methods shown below.

### Validator Rules

[](#validator-rules)

SilverWare Validator ships with many pre-built rules which you can use straight away to validate your forms. These rules support both client-side and server-side validation. Included with SilverWare Validator are the following rule classes:

- [`AlphaNumRule`](#alphanumrule)
- [`CheckRule`](#checkrule)
- [`ComparisonRule`](#comparisonrule)
- [`DateRule`](#daterule)
- [`DigitsRule`](#digitsrule)
- [`DomainRule`](#domainrule)
- [`EmailRule`](#emailrule)
- [`EqualToRule`](#equaltorule)
- [`IntegerRule`](#integerrule)
- [`LengthRule`](#lengthrule)
- [`MaxCheckRule`](#maxcheckrule)
- [`MaxLengthRule`](#maxlengthrule)
- [`MaxRule`](#maxrule)
- [`MaxWordsRule`](#maxwordsrule)
- [`MinCheckRule`](#mincheckrule)
- [`MinLengthRule`](#minlengthrule)
- [`MinRule`](#minrule)
- [`MinWordsRule`](#minwordsrule)
- [`NotEqualToRule`](#notequaltorule)
- [`NumberRule`](#numberrule)
- [`PatternRule`](#patternrule)
- [`RangeRule`](#rangerule)
- [`RemoteRule`](#remoterule)
- [`RequiredRule`](#requiredrule)
- [`URLRule`](#urlrule)
- [`WordsRule`](#wordsrule)

Each of these rules will be covered in greater detail below.

### Setting Rules

[](#setting-rules)

To set a rule for a particular form field, use the `setRule` method, and pass the field name and the rule instance. For example:

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

To set more than one rule for a field, you can use the `setRules` method and pass an array of rule instances:

```
$validator->setRules(
    'MyDomainName',
    [
        RequiredRule::create(),  // this field is required
        DomainRule::create()     // this field must be a valid domain name
    ]
);
```

### Required Fields

[](#required-fields)

Since fields are often required for almost any form, shortcut methods have been provided to define required fields. Use `addRequiredField` with the field name and optional custom message parameter:

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

To add required fields in bulk, use `addRequiredFields` with an array of field names:

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

### Custom Messages

[](#custom-messages)

All rule classes come with a default message which is shown to the user when the rule validation fails. You can define your own custom messages by using the `setMessage` method on a rule, for example:

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

### Disabling Validation

[](#disabling-validation)

By default, SilverWare Validator performs both client-side and server-side validation. While it's recommended to leave both methods of validation enabled, if you need to disable either, you can use the following methods:

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

### Trigger Events

[](#trigger-events)

The out-of-the-box validator [Parsley.js](http://parsleyjs.org) is set to trigger form validation upon field change by default. You can customise when you'd like validation to be triggered by using the `setTriggerOn` method on the backend, for example:

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

You can also simply pass the trigger events as a string:

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

Parsley.js supports triggering by any of the standard [jQuery events](http://api.jquery.com/category/events).

Rules
-----

[](#rules)

### AlphaNumRule

[](#alphanumrule)

```
use SilverWare\Validator\Rules\AlphaNumRule;
```

Tests that the value is an alphanumeric string, containing only basic letters, numbers, and the underscore character.

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

### CheckRule

[](#checkrule)

```
use SilverWare\Validator\Rules\CheckRule;
```

Used for a `OptionsetField` such as `CheckboxSetField`. Ensures that the user chooses a certain number of options, between the given `$min` and `$max` parameters.

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

### ComparisonRule

[](#comparisonrule)

```
use SilverWare\Validator\Rules\ComparisonRule;
```

Compares the value of one field to another, using the specified comparison type:

- `ComparisonRule::LESS_THAN `
- `ComparisonRule::LESS_THAN_OR_EQUAL`
- `ComparisonRule::GREATER_THAN`
- `ComparisonRule::GREATER_THAN_OR_EQUAL`

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

### DateRule

[](#daterule)

```
use SilverWare\Validator\Rules\DateRule;
```

Ensures that the entered value is a date of the specified format. The rule constructor supports two arguments. The first is the required date format using the client-side [Moment.js](https://momentjs.com) specification.

The optional second argument specifies the server-side PHP date format, however the rule will convert the client-side format appropriately if you omit the second argument.

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

### DigitsRule

[](#digitsrule)

```
use SilverWare\Validator\Rules\DigitsRule;
```

Ensures only digits are entered, i.e. the numbers 0-9 only.

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

### DomainRule

[](#domainrule)

```
use SilverWare\Validator\Rules\DomainRule;
```

Tests that the entered value is a valid domain name. Supports the new TLD domains and also `localhost`.

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

### EmailRule

[](#emailrule)

```
use SilverWare\Validator\Rules\EmailRule;
```

Ensures that the value is a valid email address.

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

### EqualToRule

[](#equaltorule)

```
use SilverWare\Validator\Rules\EqualToRule;
```

Ensures that the value of one field is equal to the value of another.

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

### IntegerRule

[](#integerrule)

```
use SilverWare\Validator\Rules\IntegerRule;
```

Tests that the entered value is a valid positive or negative integer.

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

### LengthRule

[](#lengthrule)

```
use SilverWare\Validator\Rules\LengthRule;
```

Ensures that the length of the entered value is between the specified `$min` and `$max` range.

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

### MaxCheckRule

[](#maxcheckrule)

```
use SilverWare\Validator\Rules\MaxCheckRule;
```

Used for a `OptionsetField` such as `CheckboxSetField`. Specifies that the user may only select a maximum number of options.

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

### MaxLengthRule

[](#maxlengthrule)

```
use SilverWare\Validator\Rules\MaxLengthRule;
```

Ensures that the entered value is of a maximum length.

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

### MaxRule

[](#maxrule)

```
use SilverWare\Validator\Rules\MaxRule;
```

Tests that a numeric value is less than or equal to the specified amount.

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

### MaxWordsRule

[](#maxwordsrule)

```
use SilverWare\Validator\Rules\MaxWordsRule;
```

Ensures that the value is of the specified number of words or less.

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

### MinCheckRule

[](#mincheckrule)

```
use SilverWare\Validator\Rules\MinCheckRule;
```

Used for a `OptionsetField` such as `CheckboxSetField`. Ensures that the user selects a minimum number of options.

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

### MinLengthRule

[](#minlengthrule)

```
use SilverWare\Validator\Rules\MinLengthRule;
```

Ensures that the entered value is of a minimum length.

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

### MinRule

[](#minrule)

```
use SilverWare\Validator\Rules\MinRule;
```

Tests that the numeric value is greater than or equal to the specified amount.

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

### MinWordsRule

[](#minwordsrule)

```
use SilverWare\Validator\Rules\MinWordsRule;
```

Ensures that the entered value is of the specified number of words or greater.

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

### NotEqualToRule

[](#notequaltorule)

```
use SilverWare\Validator\Rules\NotEqualToRule;
```

Ensures that the value of one field is *NOT* equal to the value of another.

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

### NumberRule

[](#numberrule)

```
use SilverWare\Validator\Rules\NumberRule;
```

Tests that the entered value is numeric, i.e. an integer or a floating-point number.

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

### PatternRule

[](#patternrule)

```
use SilverWare\Validator\Rules\PatternRule;
```

Uses a regular expression to test the entered value. Ensure that the format of your regular expression works in both JavaScript, and also PHP `preg_match` so that both client and server validation functions correctly.

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

### RangeRule

[](#rangerule)

```
use SilverWare\Validator\Rules\RangeRule;
```

Specifies a number range that the entered value must be between.

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

### RemoteRule

[](#remoterule)

```
use SilverWare\Validator\Rules\RemoteRule;
```

Tests the value on the client-side using an Ajax request, and uses [Guzzle](https://github.com/guzzle/guzzle) to test the value when submitted to the server.

The following arguments are permitted for the rule constructor:

```
$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'
);
```

For example, you might use the `RemoteRule` to check if a username is available:

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

The server will receive the name of the field and it's value as parameters, i.e. `?MyUserName=sallybloggs`. By default, if the `RemoteRule` receives an HTTP status code of between 200-299, it is considered to be valid. Anything else will return an invalid result.

If you'd like to reverse this behavior, i.e. 200-299 is considered to be invalid, you can use "reverse" mode:

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

For more information on using remote validation, see the [Parsley.js documentation](http://parsleyjs.org/doc/index.html#remote).

### RequiredRule

[](#requiredrule)

```
use SilverWare\Validator\Rules\RequiredRule;
```

Defines the specified field as a required (mandatory) field that must be completed by the user.

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

### URLRule

[](#urlrule)

```
use SilverWare\Validator\Rules\URLRule;
```

Ensures that the entered value is a valid URL.

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

### WordsRule

[](#wordsrule)

```
use SilverWare\Validator\Rules\WordsRule;
```

Ensures that the entered value is between `$min` and `$max` words.

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

Issues
------

[](#issues)

Please use the [GitHub issue tracker](https://github.com/praxisnetau/silverware-validator/issues) for bug reports and feature requests.

To-Do
-----

[](#to-do)

- Tests

Contribution
------------

[](#contribution)

Your contributions are gladly welcomed to help make this project better. Please see [contributing](CONTRIBUTING.md) for more information.

Attribution
-----------

[](#attribution)

- Makes use of [Parsley.js](http://parsleyjs.org) by [Guillaume Potier](https://github.com/guillaumepotier).
- Makes use of [Moment.js](https://momentjs.com) by [Iskren Ivov Chernev](https://github.com/ichernev) and others.
- Makes use of [Guzzle](https://github.com/guzzle/guzzle) by [Michael Dowling](https://github.com/mtdowling) and others.
- Makes use of [Bootstrap](https://v4-alpha.getbootstrap.com) by the [Bootstrap Authors](https://github.com/twbs/bootstrap/graphs/contributors)and [Twitter, Inc](https://twitter.com).
- Inspired by [SilverStripe ZenValidator](https://github.com/sheadawson/silverstripe-zenvalidator) by [Shea Dawson](https://github.com/sheadawson).

Maintainers
-----------

[](#maintainers)

[![Colin Tucker](https://avatars3.githubusercontent.com/u/1853705?s=144)](https://github.com/colintucker)[![Praxis Interactive](https://avatars2.githubusercontent.com/u/1782612?s=144)](https://www.praxis.net.au)[Colin Tucker](https://github.com/colintucker)[Praxis Interactive](https://www.praxis.net.au)License
-------

[](#license)

[BSD-3-Clause](LICENSE.md) © Praxis Interactive

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance15

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~44 days

Recently: every ~71 days

Total

10

Last Release

2898d ago

Major Versions

0.1.2 → 1.0.02017-06-15

### Community

Maintainers

![](https://www.gravatar.com/avatar/f5d47a59cd183aa3f599c2b66dfce7c79c249c2eacb749f0ac8c38c4ca273d14?d=identicon)[PraxisNetAu](/maintainers/PraxisNetAu)

---

Top Contributors

[![colintucker](https://avatars.githubusercontent.com/u/1853705?v=4)](https://github.com/colintucker "colintucker (17 commits)")

---

Tags

formsparsleysilverstripe-4silverwarevalidationvalidatorvalidatorvalidationsilverstripeparsleyFormssilverware

### Embed Badge

![Health badge](/badges/silverware-validator/health.svg)

```
[![Health](https://phpackages.com/badges/silverware-validator/health.svg)](https://phpackages.com/packages/silverware-validator)
```

###  Alternatives

[sheadawson/silverstripe-zenvalidator

Faster, easier client and server-side form validation for SilverStripe

5778.4k2](/packages/sheadawson-silverstripe-zenvalidator)[wixel/gump

A fast, extensible &amp; stand-alone PHP input validation class that allows you to validate any data.

1.2k1.3M30](/packages/wixel-gump)[phpexperts/datatype-validator

An easy to use data type validator (both strict and fuzzy).

141.1M2](/packages/phpexperts-datatype-validator)[dragon-code/card-number

Generation and verification of card numbers using Luhn's algorithm.

6512.8k](/packages/dragon-code-card-number)[articus/data-transfer

Library for merging source data to destination data only if destination data remains valid after that

1030.4k4](/packages/articus-data-transfer)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
