-
Notifications
You must be signed in to change notification settings - Fork 823
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
e2e3231
commit 5ab18c6
Showing
5 changed files
with
68 additions
and
15 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
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; | ||
} | ||
} |
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