Skip to content

Commit

Permalink
Merge branch '6.0' into 6
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Dec 12, 2024
2 parents 3e62b98 + c6d2b8b commit c1c8a68
Show file tree
Hide file tree
Showing 56 changed files with 413 additions and 355 deletions.
18 changes: 9 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@
"require": {
"php": "^8.3",
"composer-runtime-api": "^2.0",
"composer/installers": "^2.2",
"guzzlehttp/guzzle": "^7.5.0",
"guzzlehttp/psr7": "^2.4.0",
"composer/installers": "^2.3",
"guzzlehttp/guzzle": "^7.9",
"guzzlehttp/psr7": "^2.7",
"embed/embed": "^4.4.7",
"league/csv": "^9.8.0",
"league/csv": "^9.18",
"m1/env": "^2.2.0",
"masterminds/html5": "^2.7.6",
"monolog/monolog": "^3.2.0",
"nikic/php-parser": "^5.1.0",
"psr/container": "^1.1 || ^2.0",
"psr/http-message": "^1",
"masterminds/html5": "^2.9",
"monolog/monolog": "^3.8",
"nikic/php-parser": "^5.3",
"psr/container": "^2.0",
"psr/http-message": "^2.0",
"sebastian/diff": "^6.0",
"sensiolabs/ansi-to-html": "^1.2",
"silverstripe/config": "^3",
Expand Down
37 changes: 0 additions & 37 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
26 changes: 18 additions & 8 deletions 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,8 +299,10 @@ public function __construct(
$this->setName($name);

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

// Form error controls
$this->restoreFormState();
Expand Down Expand Up @@ -1262,17 +1266,23 @@ public function getLegend()
*/
public function validationResult()
{
// Automatically pass if there is no validator, or the clicked button is exempt
$result = ValidationResult::create();
// Automatically pass if 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
7 changes: 2 additions & 5 deletions src/Forms/GridField/GridFieldDetailForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
use SilverStripe\Core\Extensible;
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 Expand Up @@ -147,10 +146,8 @@ public function handleItem($gridField, $request)
if (!$this->getValidator()) {
if ($record->hasMethod('getCMSCompositeValidator')) {
$validator = $record->getCMSCompositeValidator();
} else {
$validator = FieldsValidator::create();
$this->setValidator($validator);
}
$this->setValidator($validator);
}

return $handler->handleRequest($request);
Expand Down
11 changes: 4 additions & 7 deletions src/Forms/GridField/GridFieldExportButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SilverStripe\Forms\GridField;

use League\Csv\Bom;
use League\Csv\Writer;
use LogicException;
use SilverStripe\Control\HTTPRequest;
Expand Down Expand Up @@ -180,8 +181,8 @@ public function generateExportFileData($gridField)
$csvWriter = Writer::createFromFileObject(new \SplTempFileObject());
$csvWriter->setDelimiter($this->getCsvSeparator());
$csvWriter->setEnclosure($this->getCsvEnclosure());
$csvWriter->setNewline("\r\n"); //use windows line endings for compatibility with some csv libraries
$csvWriter->setOutputBOM(Writer::BOM_UTF8);
$csvWriter->setEndOfLine("\r\n"); //use windows line endings for compatibility with some csv libraries
$csvWriter->setOutputBOM(Bom::Utf8);

if (!Config::inst()->get(get_class($this), 'xls_export_disabled')) {
$csvWriter->addFormatter(function (array $row) {
Expand Down Expand Up @@ -269,11 +270,7 @@ public function generateExportFileData($gridField)
}
}

if (method_exists($csvWriter, 'getContent')) {
return $csvWriter->getContent();
}

return (string)$csvWriter;
return $csvWriter->toString();
}

/**
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 @@ -20,7 +20,7 @@
* {
* $compositeValidator = parent::getCMSCompositeValidator();
*
* $compositeValidator->addValidator(RequiredFields::create(['MyRequiredField']));
* $compositeValidator->addValidator(RequiredFieldsValidator::create(['MyRequiredField']));
*
* return $compositeValidator
* }
Expand All @@ -29,9 +29,8 @@
*
* protected function updateCMSCompositeValidator(CompositeValidator $compositeValidator): void
* {
* $compositeValidator->addValidator(RequiredFields::create(['AdditionalContent']));
* $compositeValidator->addValidator(RequiredFieldsValidator::create(['AdditionalContent']));
* }
* @deprecated 5.4.0 Will be renamed to SilverStripe\Forms\Validation\CompositeValidator
*/
class CompositeValidator extends Validator
{
Expand All @@ -47,11 +46,6 @@ class CompositeValidator extends Validator
*/
public function __construct(array $validators = [])
{
Deprecation::noticeWithNoReplacment(
'5.4.0',
'Will be renamed to SilverStripe\\Forms\\Validation\\CompositeValidator',
Deprecation::SCOPE_CLASS
);
$this->validators = array_values($validators ?? []);
parent::__construct();
}
Expand Down Expand Up @@ -106,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 @@ -156,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 @@ -181,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,9 +1,11 @@
<?php

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

use SilverStripe\Core\ArrayLib;
use SilverStripe\Dev\Deprecation;
use SilverStripe\Forms\HasOneRelationFieldInterface;
use SilverStripe\Forms\FileField;
use SilverStripe\Forms\FormField;

/**
* Required Fields allows you to set which fields need to be present before
Expand All @@ -12,10 +14,8 @@
*
* Validation is performed on a field by field basis through
* {@link FormField::validate}.
*
* @deprecated 5.4.0 Will be renamed to SilverStripe\Forms\Validation\RequiredFieldsValidator
*/
class RequiredFields extends Validator
class RequiredFieldsValidator extends Validator
{
/**
* Whether to globally allow whitespace only as a valid value for a required field
Expand All @@ -42,11 +42,6 @@ class RequiredFields extends Validator
*/
public function __construct()
{
Deprecation::noticeWithNoReplacment(
'5.4.0',
'Will be renamed to SilverStripe\\Forms\\Validation\\RequiredFieldsValidator',
Deprecation::SCOPE_CLASS
);
$required = func_get_args();
if (isset($required[0]) && is_array($required[0])) {
$required = $required[0];
Expand Down Expand Up @@ -121,12 +116,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 @@ -232,7 +221,7 @@ public function removeRequiredField($field)
/**
* Add {@link RequiredField} objects together
*
* @param RequiredFields $requiredFields
* @param RequiredFieldsValidator $requiredFields
* @return $this
*/
public function appendRequiredFields($requiredFields)
Expand Down
12 changes: 2 additions & 10 deletions src/Forms/Validator.php → src/Forms/Validation/Validator.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
<?php

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

use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Extensible;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Validation\ValidationResult;
use SilverStripe\Dev\Deprecation;

/**
* This validation class handles all form and custom form validation through the use of Required
* fields. It relies on javascript for client-side validation, and marking fields after server-side
* validation. It acts as a visitor to individual form fields.
*
* @deprecated 5.4.0 Will be renamed to SilverStripe\Forms\Validation\Validator
*/
abstract class Validator
{
Expand All @@ -23,11 +20,6 @@ abstract class Validator

public function __construct()
{
Deprecation::noticeWithNoReplacment(
'5.4.0',
'Will be renamed to SilverStripe\\Forms\\Validation\\Validator',
Deprecation::SCOPE_CLASS
);
$this->resetResult();
}

Expand Down Expand Up @@ -179,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
5 changes: 2 additions & 3 deletions src/ORM/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FormField;
use SilverStripe\Forms\FormScaffolder;
use SilverStripe\Forms\CompositeValidator;
use SilverStripe\Forms\FieldsValidator;
use SilverStripe\Forms\Validation\CompositeValidator;
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
Loading

0 comments on commit c1c8a68

Please sign in to comment.