NEW DBField validation

This commit is contained in:
Steve Boyd 2024-09-23 19:03:35 +12:00
parent e2e32317d6
commit 5ab18c6f04
5 changed files with 68 additions and 15 deletions

View File

@ -20,6 +20,8 @@ SilverStripe\Core\Injector\Injector:
class: SilverStripe\ORM\FieldType\DBDecimal
Double:
class: SilverStripe\ORM\FieldType\DBDouble
Email:
class: SilverStripe\ORM\FieldType\DBEmail
Enum:
class: SilverStripe\ORM\FieldType\DBEnum
Float:

View File

@ -2,6 +2,9 @@
namespace SilverStripe\Forms;
use SilverStripe\Core\Validation\ConstraintValidator;
use Symfony\Component\Validator\Constraints;
/**
* Text input field with validation for correct email format according to RFC 2822.
*/
@ -18,32 +21,26 @@ class EmailField extends TextField
}
/**
* Validates for RFC 2822 compliant email addresses.
*
* @see http://www.regular-expressions.info/email.html
* @see http://www.ietf.org/rfc/rfc2822.txt
*
* @param Validator $validator
*
* @return string
*/
public function validate($validator)
{
$result = true;
$this->value = trim($this->value ?? '');
$pattern = '^[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$';
$result = true;
$message = _t('SilverStripe\\Forms\\EmailField.VALIDATION', 'Please enter an email address');
$validationResult = ConstraintValidator::validate(
$this->value,
new Constraints\Email(message: $message)
);
// Escape delimiter characters.
$safePattern = str_replace('/', '\\/', $pattern ?? '');
if ($this->value && !preg_match('/' . $safePattern . '/i', $this->value ?? '')) {
if (!$validationResult->isValid()) {
$validator->validationError(
$this->name,
_t('SilverStripe\\Forms\\EmailField.VALIDATION', 'Please enter an email address'),
'validation'
$validationResult->getMessages()[0]['message'],
);
$result = false;
}

View File

@ -1230,7 +1230,12 @@ class DataObject extends ModelData implements DataObjectInterface, i18nEntityPro
public function validate()
{
$result = ValidationResult::create();
$this->extend('updateValidate', $result);
$specs = static::getSchema()->fieldSpecs(static::class);
foreach (array_keys($specs) as $fieldName) {
$dbField = $this->dbObject($fieldName);
$result->combineAnd($dbField->validate());
}
$this->extend('updateValidate', $validationResult);
return $result;
}

View File

@ -0,0 +1,43 @@
<?php
namespace SilverStripe\ORM\FieldType;
use SilverStripe\ORM\FieldType\DBVarchar;
use Symfony\Component\Validator\Constraints;
use SilverStripe\Core\Validation\ConstraintValidator;
use SilverStripe\Core\Validation\ValidationResult;
use SilverStripe\Forms\EmailField;
use SilverStripe\Forms\NullableField;
use SilverStripe\Forms\FormField;
class DBEmail extends DBVarchar
{
public function validate(): ValidationResult
{
// https://symfony.com/doc/current/reference/constraints/Email.html
$result = parent::validate();
$message = _t('SilverStripe\\Forms\\EmailField.VALIDATION', 'Please enter an email address');
$result = $result->combineAnd(
ConstraintValidator::validate(
$this->getValue(),
new Constraints\Email(message: $message),
$this->getName()
)
);
$this->extend('updateValidate', $result);
return $result;
}
public function scaffoldFormField(?string $title = null, array $params = []): ?FormField
{
// Set field with appropriate size
$field = EmailField::create($this->name, $title);
$field->setMaxLength($this->getSize());
// Allow the user to select if it's null instead of automatically assuming empty string is
if (!$this->getNullifyEmpty()) {
return NullableField::create($field);
}
return $field;
}
}

View File

@ -10,6 +10,8 @@ use SilverStripe\Forms\TextField;
use SilverStripe\ORM\Filters\SearchFilter;
use SilverStripe\ORM\Queries\SQLSelect;
use SilverStripe\Model\ModelData;
use SilverStripe\Core\Validation\ValidationResult;
use Symfony\Component\Validator\Constraints\Valid;
/**
* Single field in the database.
@ -43,6 +45,10 @@ use SilverStripe\Model\ModelData;
*/
abstract class DBField extends ModelData implements DBIndexable
{
public function validate(): ValidationResult
{
return ValidationResult::create();
}
/**
* Raw value of this field