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 5, 2024
1 parent 2fcf68a commit e83df89
Show file tree
Hide file tree
Showing 44 changed files with 107 additions and 103 deletions.
2 changes: 2 additions & 0 deletions src/Forms/FieldsValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace SilverStripe\Forms;

use SilverStripe\Forms\Validation\Validator;

/**
* Validates the internal state of all fields in the form.
*/
Expand Down
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
4 changes: 3 additions & 1 deletion src/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use SilverStripe\View\AttributesHTML;
use SilverStripe\View\SSViewer;
use SilverStripe\Model\ModelData;
use SilverStripe\Forms\Validation\RequiredFieldsValidator;
use SilverStripe\Forms\Validation\Validator;

/**
* Base class for all forms.
Expand Down Expand Up @@ -297,7 +299,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
2 changes: 1 addition & 1 deletion src/Forms/GridField/GridFieldDetailForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FieldsValidator;
use SilverStripe\Forms\Validator;
use SilverStripe\Forms\Validation\Validator;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject;
use SilverStripe\Model\ModelData;
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
@@ -1,6 +1,6 @@
<?php

namespace SilverStripe\Forms;
namespace SilverStripe\Forms\Validation;

use InvalidArgumentException;
use SilverStripe\Core\Validation\ValidationResult;
Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace SilverStripe\Forms;
namespace SilverStripe\Forms\Validation;

use SilverStripe\Core\ArrayLib;

Expand All @@ -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 @@ -193,7 +193,7 @@ public function removeRequiredField($field)
/**
* Add {@link RequiredField} objects together
*
* @param RequiredFields $requiredFields
* @param RequiredFieldsValidator $requiredFields
* @return $this
*/
public function appendRequiredFields($requiredFields)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace SilverStripe\Forms;
namespace SilverStripe\Forms\Validation;

use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Extensible;
Expand Down 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
2 changes: 1 addition & 1 deletion src/ORM/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FormField;
use SilverStripe\Forms\FormScaffolder;
use SilverStripe\Forms\CompositeValidator;
use SilverStripe\Forms\Validation\CompositeValidator;
use SilverStripe\Forms\FieldsValidator;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
Expand Down
6 changes: 3 additions & 3 deletions src/Security/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use SilverStripe\Admin\SecurityAdmin;
use SilverStripe\Core\Convert;
use SilverStripe\Forms\CompositeValidator;
use SilverStripe\Forms\Validation\CompositeValidator;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
Expand All @@ -23,7 +23,7 @@
use SilverStripe\Forms\HTMLEditor\HTMLEditorConfig;
use SilverStripe\Forms\ListboxField;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\Validation\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/LogoutForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\HiddenField;
use SilverStripe\Forms\Validator;
use SilverStripe\Forms\Validation\Validator;

/**
* Log out form to display to users who arrive at 'Security/logout' without a
Expand Down
4 changes: 2 additions & 2 deletions src/Security/Member.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\TestMailer;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\CompositeValidator;
use SilverStripe\Forms\Validation\CompositeValidator;
use SilverStripe\Forms\ConfirmedPasswordField;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldList;
Expand Down 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\Validation\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\Validation\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\Validation\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\Validation\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\Validation\RequiredFieldsValidator;
use SilverStripe\Forms\TextField;

class CompositeFieldTest extends SapphireTest
Expand Down
32 changes: 16 additions & 16 deletions tests/php/Forms/CompositeValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\Validation\RequiredFieldsValidator;
use SilverStripe\Forms\Tests\ValidatorTest\TestValidator;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\CompositeValidator;
use SilverStripe\Forms\Validation\CompositeValidator;

/**
* @package framework
Expand Down Expand Up @@ -40,8 +40,8 @@ protected function getForm(array $fieldNames = []): Form
public function testAddValidator(): void
{
$compositeValidator = new CompositeValidator();
$compositeValidator->addValidator(new RequiredFields());
$compositeValidator->addValidator(new RequiredFields());
$compositeValidator->addValidator(new RequiredFieldsValidator());
$compositeValidator->addValidator(new RequiredFieldsValidator());

$this->assertCount(2, $compositeValidator->getValidators());
}
Expand Down Expand Up @@ -76,38 +76,38 @@ public function testSetForm(): void
public function testGetValidatorsByType(): void
{
$compositeValidator = new CompositeValidator();
$compositeValidator->addValidator(new RequiredFields());
$compositeValidator->addValidator(new RequiredFieldsValidator());
$compositeValidator->addValidator(new TestValidator());
$compositeValidator->addValidator(new RequiredFields());
$compositeValidator->addValidator(new RequiredFieldsValidator());
$compositeValidator->addValidator(new TestValidator());

$this->assertCount(4, $compositeValidator->getValidators());
$this->assertCount(2, $compositeValidator->getValidatorsByType(RequiredFields::class));
$this->assertCount(2, $compositeValidator->getValidatorsByType(RequiredFieldsValidator::class));
}

public function testRemoveValidatorsByType(): void
{
$compositeValidator = new CompositeValidator();
$compositeValidator->addValidator(new RequiredFields());
$compositeValidator->addValidator(new RequiredFieldsValidator());
$compositeValidator->addValidator(new TestValidator());
$compositeValidator->addValidator(new RequiredFields());
$compositeValidator->addValidator(new RequiredFieldsValidator());
$compositeValidator->addValidator(new TestValidator());

$this->assertCount(4, $compositeValidator->getValidators());

$compositeValidator->removeValidatorsByType(RequiredFields::class);
$compositeValidator->removeValidatorsByType(RequiredFieldsValidator::class);
$this->assertCount(2, $compositeValidator->getValidators());
}

public function testCanBeCached(): void
{
$compositeValidator = new CompositeValidator();
$compositeValidator->addValidator(new RequiredFields());
$compositeValidator->addValidator(new RequiredFieldsValidator());

$this->assertTrue($compositeValidator->canBeCached());

$compositeValidator = new CompositeValidator();
$compositeValidator->addValidator(new RequiredFields(['Foor']));
$compositeValidator->addValidator(new RequiredFieldsValidator(['Foor']));

$this->assertFalse($compositeValidator->canBeCached());
}
Expand All @@ -121,12 +121,12 @@ public function testFieldIsRequired(): void
'Content',
];

$requiredFieldsFirst = new RequiredFields(
$requiredFieldsFirst = new RequiredFieldsValidator(
[
$fieldNames[0],
]
);
$requiredFieldsSecond = new RequiredFields(
$requiredFieldsSecond = new RequiredFieldsValidator(
[
$fieldNames[1],
]
Expand All @@ -147,8 +147,8 @@ public function testValidate(): void
{
$compositeValidator = new CompositeValidator();
// Add two separate validators, each with one required field
$compositeValidator->addValidator(new RequiredFields(['Foo']));
$compositeValidator->addValidator(new RequiredFields(['Bar']));
$compositeValidator->addValidator(new RequiredFieldsValidator(['Foo']));
$compositeValidator->addValidator(new RequiredFieldsValidator(['Bar']));

// Setup a form with the fields/data we're testing (a form is a dependency for validation right now)
// We'll add three empty fields, but only two of them should be required
Expand Down
Loading

0 comments on commit e83df89

Please sign in to comment.