-
Notifications
You must be signed in to change notification settings - Fork 824
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 0db4cf4
Showing
11 changed files
with
223 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,29 @@ | ||
<?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 = [ | ||
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,72 @@ | ||
<?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 $classOrIndex => $classOrExtraArgMethods) { | ||
if (is_int($classOrIndex)) { | ||
$class = $classOrExtraArgMethods; | ||
$extraArgMethods = []; | ||
} else { | ||
$class = $classOrIndex; | ||
$extraArgMethods = $classOrExtraArgMethods; | ||
} | ||
if (!is_a($class, FieldValidator::class, true)) { | ||
throw new RuntimeException("Class $class is not a FieldValidator"); | ||
} | ||
if (!is_array($extraArgMethods)) { | ||
throw new RuntimeException("extra arg methods for $class is not an array"); | ||
} | ||
$args = [$name, $value]; | ||
if (!is_null($extraArgMethods)) { | ||
foreach ($extraArgMethods as $i => $extraArgMethod) { | ||
if (!is_string($extraArgMethod) && !is_null($extraArgMethod)) { | ||
throw new RuntimeException("extra arg method $i for $class is not a string or null"); | ||
} | ||
if ($extraArgMethod) { | ||
$args[] = call_user_func([$field, $extraArgMethod]); | ||
} else { | ||
$args[] = null; | ||
} | ||
} | ||
} | ||
$fieldValidators[] = Injector::inst()->createWithArgs($class, $args); | ||
} | ||
return $fieldValidators; | ||
} | ||
} |
Oops, something went wrong.