Download the PHP package sheadawson/silverstripe-zenvalidator without Composer
On this page you can find all versions of the php package sheadawson/silverstripe-zenvalidator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sheadawson/silverstripe-zenvalidator
More information about sheadawson/silverstripe-zenvalidator
Files in sheadawson/silverstripe-zenvalidator
Package silverstripe-zenvalidator
Short Description Faster, easier client and server-side form validation for SilverStripe
License BSD License
Homepage https://github.com/sheadawson/silverstripe-zenvalidator
Informations about the package silverstripe-zenvalidator
SilverStripe ZenValidator
Description
ZenValidator aims to make silverstripe form validation as painless as possible, by allowing configuration of serverside and clientside validation through one simple API. Parsley.js is used for the clientside validation in the frontend.
Installation
composer require sheadawson/silverstripe-zenvalidator
SilverStripe 4 support
This version of the module only supports SilverStripe 4. The current version of the module try as much as possible to remain compatible with previous code. SilverStripe 4 support is pretty much work in progress so you can expect a few issues.
You can also check this other module that uses ParsleyJS as well: https://github.com/praxisnetau/silverware-validator
Future versions of this module will break BC to promote namespaced classes and other improvements.
SilverStripe 3 support
For SilverStripe 3 support, please use branch 1.
Using with modern jQuery
Due to entwine, modern jquery is not supported because of an issue in entwine.js. This module provides an updated version of entwine that drop support for old version of IE (<9).
This module expects that you include your own version of jQuery. However, as a convenience we have included a ZenValidator::globalRequirements helper. This helper also includes the updated version of entwine.
Also, we try to avoid using entwine entirely if possible. You can use the legacy behaviour with
Validation Constraints
Out of the box constraints include:
- required
- value (min, max, range)
- length (min, max, range)
- check (min, max, range)
- type (email, url, number, integer, digits, alphanumeric)
- equalto (equal to the value of another field)
- notequalto (not equal to the value of another field)
- regex
- remote (validate remotely via ajax)
- dimension (image width, height, aspect ratio. CMS only)
Usage examples
Create a form, add ZenValidator.
The following examples demonstrate various ways to add constraints to the above form example.
Required fields
The addRequiredFields() is available for quick and clean adding of required fields.
Required Fields - Basic
Required Fields - Custom Messages
Other Constraints
All other constraints are added via the setConstraint() method. This method takes 2 parameters: $fieldName and $constraint. See examples below.
Value Constraints
Test for number of min, max or between range value
Min
Max
Range
Length Constraints
Test for a min, max or between range length of string
Min
Max
Range
Check Constraints
Test for a min, max or range of elements checked
Min
Max
Range
Type Constraints
The Constraint_type constraint can be used to validate inputs of type email, url, number, integer, digits or alphanum. Pass one of said options as the first parameter into the constructor.
URL
Number
Integer
Digits
Alphanum
Equal To Constraint
Validates that the value is identical to another field's value (useful for password confirmation check).
Not Equal To Constraint
Validates that the value is different from another field's value (useful to avoid duplicates).
Regex validation
Check for a valid hex color, for example…
Remote validation
Validate based on the response from a remote url. Validation is based on the response http status code. A status of 200 will validate, anything else will cause a validation error.
The above example will send an ajax request to my checkusername method on the same controller as the Form. A request var with a key the same as the field name will contain the value to test. So my checkusername method might look something like this:
All arguments/settings for the Constraint_remote constructor:
- $url - The URL to send the validation request to (do not include a query string, use $params)
- $params - An array of request vars to be sent with the request
- $options - An array of key => value options for the validator (eg: ['type' : 'POST', 'dataType' : 'jsonp'])
- $validator - Use a specific remote validator. Default validators are 'default' and 'reverse'
By default, ZenValidator uses a custom async validator that reads error message in your 4xx responses and display them below the invalid field (instead of a generic message "this value seems invalid").
For serverside validation: if a relative url is given the response will be obtained internally using Director::test, otherwise curl will be used to get the response from the remote url.
Setting Custom Messages
Any of the above examples can be configured to display a custom error message. For example:
Bulk setting of constraints
The setConstraint() method is also chainable so you can:
Removing constaints
OR
Customising frontend validation behaviour (Parsley)
It is likely that you may want to have your validation messages displaying in a custom element, with custom classes or any other custom frontend validation behaviour that is configurable with Parsley. In this case, you can set the third parameter ($defaultJS) of Zenvalidator's constructor to false.
Or set globally via yml
This will tell ZenValidator not to initialise with the default settings, and also to add the class "custom-parsley" to the form. You'll then need to add some custom javascript to the page with your own settings, for example:
Warning: $(elem).parsley() can return an array (in which can, attaching subscribe will fail) in case you match multiple forms. Use the following syntax instead if you need to match multiple elements:
See Parsley.js for the full list of configuration settings
CMS Usage
To use ZenValidator in the CMS, simply implement a getCMSValidator() method on your custom Page type or DataObject class
Image Dimension Constraints (CMS only)
You can add constraints to image selection/upload fields to ensure that users are saving images of the correct size or shape, or that the image is of a minimum/maximum width and height to prevent issues with the display of the image in the site.
Note: the validation is run when the page is saved, not at time of image choosing or upload.
Width
Use this to require the width of an image to be a certain number of pixels. Handy if you require images to be an exact size.
Height
Use this to require the height of an image is the specified pixels.
Width and Height
Use this to require both the width and height are the specified pixels.
Ratio
Use this to require images to be a certain shape, for example 6:4 photo, 5:5 square etc. Handy when you need an image to be a particular shape but are relaxed about the size as that might be dealt with by CSS.
Min width
Use this to ensure the width of an image is equal to or greater than the specified pixels, handy to ensure that users don't use images which are too small and might loose quality if stretched by CSS.
Min height
Use this to ensure that the height of an image is equal to or greater than the specified pixels.
Min width and height
Use this to ensure that the width and the height of the image are equal to or greater than the specified pixels.
Max width
Use this to ensure that the width of an image is less than or equal to the specified pixels. Handy to ensure that users don't select images far larger than required, especially if these images have a max-width set by CSS as that would result in a lot of wasted bandwidth.
Max height
Use this to ensure the height of an image is less than or equal to the specified pixels.
Max width and height
Use this to ensure the width and height is of an image does not exceed the specified pixels.
Validation Logic - Conditional Constraints
This feature allows you to specify under what conditions a field should or should not have it's validation constraints applied, based on the value(s) of other fields on the form. The concept borrows heavily from and compliments Uncle Cheese's Display Logic module. Note that no frontend logic is applied, this is backend only. If you use display logic, that will allow you to hide fields that shouldn't be shown/validated on the frontend.
Validation Logic Examples
You can use the following conditional:
- isEqualTo
- isEmpty
- isGreaterThan
- isLessThan
- contains
- isChecked
Extra validators
You can also use extra validators crafted by the community. The module is shipped with the default validators:
- Comparison
- Words
- Date Iso
Javascript and language files are loaded only if you use these validators.
No validation
In some scenarios, you don't want to trigger validation when submitting the form (i.e.: "previous" button in a multi step form). Although this is easy to achieve by yourself, the module provides a standard implementation for doing this.
Instead of using the standard FormAction class, you can use its subclass "FormActionNoValidation". It will prevent the client and server side validation from happening.
Extending
You can create your own validation constraints by subclassing the abstract ZenValidatorConstraint class. For frontend implementation of your custom validator, see Parsley.extend.js. Unfortunately there is no real documentation other than the code itself at this stage so good luck!
For everything else in the frontend (triggers, error classes, error placement, etc) See the Parsley.js documentation
Maintainers
TODO
- Parsley validation in CMS (currently only serverside) (ajax)
- Finish conditional validation ie. only validate constraint if field x value is y, document