Skip to content

Commit

Permalink
NEW DBField validation
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Sep 23, 2024
1 parent e2e3231 commit 5ab18c6
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 15 deletions.
2 changes: 2 additions & 0 deletions _config/model.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
25 changes: 11 additions & 14 deletions src/Forms/EmailField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -18,32 +21,26 @@ public function Type()
}

/**
* 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])?$';

// Escape delimiter characters.
$safePattern = str_replace('/', '\\/', $pattern ?? '');
$result = true;
$message = _t('SilverStripe\\Forms\\EmailField.VALIDATION', 'Please enter an email address');
$validationResult = ConstraintValidator::validate(
$this->value,
new Constraints\Email(message: $message)
);

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;
}

Expand Down
7 changes: 6 additions & 1 deletion src/ORM/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,12 @@ public function forceChange()
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;
}

Expand Down
43 changes: 43 additions & 0 deletions src/ORM/FieldType/DBEmail.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
6 changes: 6 additions & 0 deletions src/ORM/FieldType/DBField.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
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.
Expand Down Expand Up @@ -43,6 +45,10 @@
*/
abstract class DBField extends ModelData implements DBIndexable
{
public function validate(): ValidationResult
{
return ValidationResult::create();
}

/**
* Raw value of this field
Expand Down

0 comments on commit 5ab18c6

Please sign in to comment.