signify-nz / silverstripe-dependentrequiredfields
SilverStripe vendor module providing a validator which allows for fields to be required based on the values of other fields.
Installs: 238
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 9
Forks: 0
Open Issues: 0
Type:silverstripe-vendormodule
Requires
- athari/yalinqo: ^2.4
- silverstripe/framework: ^4.0
- silverstripe/vendor-plugin: ^1.0
Requires (Dev)
- phpunit/phpunit: ^5.7
- squizlabs/php_codesniffer: ^3.0
Suggests
- unclecheese/silverstripe-display-logic: Provides the ability to show/hide fields based on the values of other fields.
This package is not auto-updated.
Last update: 2024-11-01 23:08:21 UTC
README
Dependent Required Fields
The Dependent Required Fields module provides a validator which extends RequiredFields and allows for fields to be required based on the values of other fields.
SearchFilters are used to provide a variety of ways to compare values, depending on what causes the fields to be required. See SilverStripe's documentation on SearchFilters for more about how to use those.
Requirements
Installation
Composer:
composer require signify-nz/silverstripe-dependentrequiredfields
Documentaion
No configuration is required. See Example Usage below for an example of how this validator is used.
Example Usage
In the below example, we have fields with various levels of dependency on whether they are required or not.
AlwaysRequiredField
will be required regardless of the values of other fields.
ExactValueField
will only be required if the value of DependencyField
exactly equals someExactValue
.
StartsWithField
will only be required if the value of DependencyField
starts with the string some
.
<?php public function getCMSValidator() { return DependentRequiredFields::create([ 'AlwaysRequiredField', 'ExactValueField' => ['DependencyField' => 'someExactValue'], 'StartsWithField' => ['DependencyField:StartsWith' => 'some'], ]); }
Additional methods are also provided to add or remove dependently required fields. Note that the field AlwaysRequiredField
from the above example cannot be added or removed using these methods; the addRequiredField
and removeRequiredField
fields provided by RequiredField should be used instead.
$validator->addDependentRequiredField('GreaterThanField', ['DependencyField:GreaterThan' => '10']); $validator->removeDependentRequiredField('ExactValueField');