-
Notifications
You must be signed in to change notification settings - Fork 821
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c523022
commit c173753
Showing
11 changed files
with
228 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace SilverStripe\ORM\FieldType; | ||
|
||
use SilverStripe\Forms\EmailField; | ||
use SilverStripe\ORM\FieldType\DBVarchar; | ||
use SilverStripe\Validation\EmailValidator; | ||
use SilverStripe\Forms\FormField; | ||
use SilverStripe\Forms\NullableField; | ||
|
||
class DBEmail extends DBVarchar | ||
{ | ||
private static array $field_validators = [ | ||
[ | ||
'class' => EmailValidator::class, | ||
], | ||
]; | ||
|
||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Validation; | ||
|
||
use SilverStripe\Core\Validation\ValidationResult; | ||
use SilverStripe\Validation\FieldValidator; | ||
use SilverStripe\Core\Validation\ConstraintValidator; | ||
use Symfony\Component\Validator\Constraints; | ||
use SilverStripe\Forms\FormField; | ||
use SilverStripe\ORM\FieldType\DBField; | ||
|
||
class EmailValidator extends FieldValidator | ||
{ | ||
protected function validateValue(ValidationResult $result): ValidationResult | ||
{ | ||
$message = _t('SilverStripe\\Forms\\EmailField.VALIDATION', 'Please enter an email address'); | ||
$validationResult = ConstraintValidator::validate( | ||
$this->value, | ||
new Constraints\Email(message: $message), | ||
$this->name | ||
); | ||
return $result->combineAnd($validationResult); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Validation; | ||
|
||
use RuntimeException; | ||
use SilverStripe\Core\Validation\ValidationResult; | ||
use SilverStripe\Forms\FormField; | ||
use SilverStripe\ORM\FieldType\DBField; | ||
use SilverStripe\Core\Injector\Injector; | ||
|
||
/** | ||
* Abstract class that can be used as a validator for FormFields and DBFields | ||
*/ | ||
abstract class FieldValidator | ||
{ | ||
protected string $name; | ||
protected mixed $value; | ||
|
||
public function __construct(string $name, mixed $value) | ||
{ | ||
$this->name = $name; | ||
$this->value = $value; | ||
} | ||
|
||
public function validate(): ValidationResult | ||
{ | ||
$result = ValidationResult::create(); | ||
$result = $this->validateValue($result); | ||
return $result; | ||
} | ||
|
||
abstract protected function validateValue(ValidationResult $result): ValidationResult; | ||
|
||
public static function createFieldValidatorsForField( | ||
FormField|DBField $field, | ||
string $name, | ||
mixed $value | ||
): array { | ||
$fieldValidators = []; | ||
$config = $field->config()->get('field_validators'); | ||
foreach ($config as $spec) { | ||
$class = $spec['class']; | ||
$argCalls = $spec['argCalls'] ?? null; | ||
if (!is_a($class, FieldValidator::class, true)) { | ||
throw new RuntimeException("Class $class is not a FieldValidator"); | ||
} | ||
if (!is_array($argCalls)) { | ||
throw new RuntimeException("argCalls for $class is not an array"); | ||
} | ||
$args = [$name, $value]; | ||
if (!is_null($argCalls)) { | ||
foreach ($argCalls as $i => $argCall) { | ||
if (!is_string($argCall) && !is_null($argCall)) { | ||
throw new RuntimeException("argCall $i for $class is not a string or null"); | ||
} | ||
if ($argCall) { | ||
$args[] = call_user_func([$field, $argCall]); | ||
} else { | ||
$args[] = null; | ||
} | ||
} | ||
} | ||
$fieldValidators[] = Injector::inst()->createWithArgs($class, $args); | ||
} | ||
return $fieldValidators; | ||
} | ||
} |
Oops, something went wrong.