signify-nz / silverstripe-dependentrequiredfields

SilverStripe vendor module providing a validator which allows for fields to be required based on the values of other fields.
BSD 3-Clause "New" or "Revised" License
1 stars 0 forks source link

Build Status codecov

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