Skip to content

Commit

Permalink
API Rename validator classes
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Dec 3, 2024
1 parent 2fcf68a commit 65789a1
Show file tree
Hide file tree
Showing 44 changed files with 115 additions and 221 deletions.
10 changes: 5 additions & 5 deletions src/Forms/CompositeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* {
* $compositeValidator = parent::getCMSCompositeValidator();
*
* $compositeValidator->addValidator(RequiredFields::create(['MyRequiredField']));
* $compositeValidator->addValidator(RequiredFieldsValidator::create(['MyRequiredField']));
*
* return $compositeValidator
* }
Expand All @@ -28,7 +28,7 @@
*
* protected function updateCMSCompositeValidator(CompositeValidator $compositeValidator): void
* {
* $compositeValidator->addValidator(RequiredFields::create(['AdditionalContent']));
* $compositeValidator->addValidator(RequiredFieldsValidator::create(['AdditionalContent']));
* }
*/
class CompositeValidator extends Validator
Expand Down Expand Up @@ -100,7 +100,7 @@ public function validate()
}

/**
* Note: The existing implementations for the php() method (@see RequiredFields) does not check whether the
* Note: The existing implementations for the php() method (@see RequiredFieldsValidator) does not check whether the
* Validator is enabled or not, and it also does not reset the validation result - so, neither does this.
*
* @param array $data
Expand Down Expand Up @@ -150,7 +150,7 @@ public function getValidators(): array
}

/**
* Return all Validators that match a certain class name. EG: RequiredFields::class
* Return all Validators that match a certain class name. EG: RequiredFieldsValidator::class
*
* The keys for the return array match the keys in the unfiltered array. You cannot assume the keys will be
* sequential or that the first key will be ZERO.
Expand All @@ -175,7 +175,7 @@ public function getValidatorsByType(string $className): array
}

/**
* Remove all Validators that match a certain class name. EG: RequiredFields::class
* Remove all Validators that match a certain class name. EG: RequiredFieldsValidator::class
*
* @param string $className
* @return CompositeValidator
Expand Down
23 changes: 0 additions & 23 deletions src/Forms/FieldsValidator.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Forms/FileField.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* $actions = new FieldList(
* new FormAction('doUpload', 'Upload file')
* );
* $validator = new RequiredFields(['MyName', 'MyFile']);
* $validator = new RequiredFieldsValidator(['MyName', 'MyFile']);
*
* return new Form($this, 'Form', $fields, $actions, $validator);
* }
Expand Down
29 changes: 19 additions & 10 deletions src/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
* Each form needs some form of {@link Validator} to trigger the {@link FormField->validate()} methods for each field.
* You can't disable validator for security reasons, because crucial behaviour like extension checks for file uploads
* depend on it.
* The default validator is an instance of {@link RequiredFields}.
* The default validator is an instance of {@link RequiredFieldsValidator}.
* If you want to enforce serverside-validation to be ignored for a specific {@link FormField},
* you need to subclass it.
*
Expand Down Expand Up @@ -112,9 +112,9 @@ class Form extends ModelData implements HasRequestHandler
protected $name;

/**
* @var Validator
* @var null|Validator
*/
protected $validator;
protected $validator = null;

/**
* @see setValidationResponseCallback()
Expand Down Expand Up @@ -274,7 +274,7 @@ class Form extends ModelData implements HasRequestHandler
* @param FieldList $fields All of the fields in the form - a {@link FieldList} of {@link FormField} objects.
* @param FieldList $actions All of the action buttons in the form - a {@link FieldLis} of
* {@link FormAction} objects
* @param Validator|null $validator Override the default validator instance (Default: {@link RequiredFields})
* @param Validator|null $validator Override the default validator instance (Default: {@link RequiredFieldsValidator})
*/
public function __construct(
RequestHandler $controller = null,
Expand All @@ -297,7 +297,7 @@ public function __construct(
$this->setName($name);

// Form validation
$this->validator = ($validator) ? $validator : new RequiredFields();
$this->validator = ($validator) ? $validator : new RequiredFieldsValidator();
$this->validator->setForm($this);

// Form error controls
Expand Down Expand Up @@ -631,7 +631,7 @@ public function transform(FormTransformation $trans)

/**
* Get the {@link Validator} attached to this form.
* @return Validator
* @return null|Validator
*/
public function getValidator()
{
Expand Down Expand Up @@ -1262,17 +1262,26 @@ public function getLegend()
*/
public function validationResult()
{
$result = ValidationResult::create();

// Automatically pass if there is no validator, or the clicked button is exempt
// Note: Soft support here for validation with absent request handler
$handler = $this->getRequestHandler();
$action = $handler ? $handler->buttonClicked() : null;
$validator = $this->getValidator();
if (!$validator || $this->actionIsValidationExempt($action)) {
return ValidationResult::create();
if ($this->actionIsValidationExempt($action)) {
return $result;
}

// Invoke FormField validation
foreach ($this->Fields() as $field) {
$result->combineAnd($field->validate());
}

// Invoke validator
$result = $validator->validate();
$validator = $this->getValidator();
if ($validator) {
$result->combineAnd($validator->validate());
}
$this->loadMessagesFrom($result);
return $result;
}
Expand Down
9 changes: 2 additions & 7 deletions src/Forms/GridField/GridFieldDetailForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use SilverStripe\Core\Extensible;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FieldsValidator;
use SilverStripe\Forms\Validator;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject;
Expand Down Expand Up @@ -144,12 +143,8 @@ public function handleItem($gridField, $request)
}

// if no validator has been set on the GridField then use the Validators from the record.
if (!$this->getValidator()) {
if ($record->hasMethod('getCMSCompositeValidator')) {
$validator = $record->getCMSCompositeValidator();
} else {
$validator = FieldsValidator::create();
}
if (!$this->getValidator() && $record->hasMethod('getCMSCompositeValidator')) {
$validator = $record->getCMSCompositeValidator();
$this->setValidator($validator);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Forms/HasOneRelationFieldInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* Added to form fields whose values are the ID of a has_one relation
* This is used in RequiredFields validation to check if the value is set
* This is used in RequiredFieldsValidator validation to check if the value is set
*/
interface HasOneRelationFieldInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Validation is performed on a field by field basis through
* {@link FormField::validate}.
*/
class RequiredFields extends Validator
class RequiredFieldsValidator extends Validator
{

/**
Expand Down Expand Up @@ -86,12 +86,6 @@ public function php($data)
$valid = true;
$fields = $this->form->Fields();

foreach ($fields as $field) {
$result = $field->validate();
$valid = $result->isValid() && $valid;
$this->result->combineAnd($result);
}

if (!$this->required) {
return $valid;
}
Expand Down Expand Up @@ -193,7 +187,7 @@ public function removeRequiredField($field)
/**
* Add {@link RequiredField} objects together
*
* @param RequiredFields $requiredFields
* @param RequiredFieldsValidator $requiredFields
* @return $this
*/
public function appendRequiredFields($requiredFields)
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public function removeValidation()
/**
* When Validators are set on the form, it can affect whether or not the form cannot be cached.
*
* @see RequiredFields for an example of when you might be able to cache your form.
* @see RequiredFieldsValidator for an example of when you might be able to cache your form.
*
* @return bool
*/
Expand Down
3 changes: 1 addition & 2 deletions src/ORM/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use SilverStripe\Forms\FormField;
use SilverStripe\Forms\FormScaffolder;
use SilverStripe\Forms\CompositeValidator;
use SilverStripe\Forms\FieldsValidator;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
use SilverStripe\Forms\HiddenField;
Expand Down Expand Up @@ -2686,7 +2685,7 @@ public function getCMSActions()
*/
public function getCMSCompositeValidator(): CompositeValidator
{
$compositeValidator = CompositeValidator::create([FieldsValidator::create()]);
$compositeValidator = CompositeValidator::create();

// Support for the old method during the deprecation period
if ($this->hasMethod('getCMSValidator')) {
Expand Down
4 changes: 2 additions & 2 deletions src/Security/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use SilverStripe\Forms\HTMLEditor\HTMLEditorConfig;
use SilverStripe\Forms\ListboxField;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\RequiredFieldsValidator;
use SilverStripe\Forms\Tab;
use SilverStripe\Forms\TabSet;
use SilverStripe\Forms\TextareaField;
Expand Down Expand Up @@ -559,7 +559,7 @@ public function getCMSCompositeValidator(): CompositeValidator
{
$validator = parent::getCMSCompositeValidator();

$validator->addValidator(RequiredFields::create([
$validator->addValidator(RequiredFieldsValidator::create([
'Title'
]));

Expand Down
2 changes: 1 addition & 1 deletion src/Security/Member.php
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ public function getMemberPasswordField()


/**
* Returns the {@link RequiredFields} instance for the Member object. This
* Returns the {@link RequiredFieldsValidator} instance for the Member object. This
* Validator is used when saving a {@link CMSProfileController} or added to
* any form responsible for saving a users data.
*
Expand Down
4 changes: 2 additions & 2 deletions src/Security/MemberAuthenticator/MemberLoginForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use SilverStripe\Forms\HiddenField;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\PasswordField;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\RequiredFieldsValidator;
use SilverStripe\Forms\TextField;
use SilverStripe\Core\Validation\ValidationResult;
use SilverStripe\Security\LoginForm as BaseLoginForm;
Expand Down Expand Up @@ -111,7 +111,7 @@ public function __construct(
if (isset($logoutAction)) {
$this->setFormAction($logoutAction);
}
$this->setValidator(RequiredFields::create(static::config()->get('required_fields')));
$this->setValidator(RequiredFieldsValidator::create(static::config()->get('required_fields')));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Security/Member_Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace SilverStripe\Security;

use SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\RequiredFieldsValidator;

/**
* Member Validator
Expand All @@ -22,7 +22,7 @@
* - Surname
* </code>
*/
class Member_Validator extends RequiredFields
class Member_Validator extends RequiredFieldsValidator
{
/**
* Fields that are required by this validator
Expand Down
4 changes: 2 additions & 2 deletions tests/php/Forms/CheckboxFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use SilverStripe\ORM\DB;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\RequiredFieldsValidator;

class CheckboxFieldTest extends SapphireTest
{
Expand Down Expand Up @@ -200,7 +200,7 @@ public function testNoAriaRequired()
"form",
new FieldList($field),
new FieldList(),
new RequiredFields(["RequiredField"])
new RequiredFieldsValidator(["RequiredField"])
);
$this->assertTrue($field->Required());

Expand Down
4 changes: 2 additions & 2 deletions tests/php/Forms/CheckboxSetFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use SilverStripe\Forms\CheckboxSetField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\RequiredFieldsValidator;
use SilverStripe\Model\ArrayData;

class CheckboxSetFieldTest extends SapphireTest
Expand Down Expand Up @@ -355,7 +355,7 @@ public function testNoAriaRequired()
"form",
new FieldList($field),
new FieldList(),
new RequiredFields(["RequiredField"])
new RequiredFieldsValidator(["RequiredField"])
);
$this->assertTrue($field->Required());

Expand Down
2 changes: 1 addition & 1 deletion tests/php/Forms/CompositeFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use SilverStripe\Forms\CompositeField;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\RequiredFieldsValidator;
use SilverStripe\Forms\TextField;

class CompositeFieldTest extends SapphireTest
Expand Down
Loading

0 comments on commit 65789a1

Please sign in to comment.