Skip to content

Commit

Permalink
CC-16892 Added Validation for LastName and FirstName (#9553)
Browse files Browse the repository at this point in the history
CC-16892 Improved name validation for several entities.
  • Loading branch information
sprykerQwermus authored Sep 10, 2022
1 parent e459737 commit 6219757
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 16 deletions.
51 changes: 43 additions & 8 deletions src/Spryker/Zed/Customer/Communication/Form/AddressForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Spryker\Zed\Customer\Communication\Form;

use Spryker\Zed\Customer\CustomerConfig;
use Spryker\Zed\Kernel\Communication\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
Expand All @@ -16,6 +17,7 @@
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Regex;

/**
* @method \Spryker\Zed\Customer\Business\CustomerFacadeInterface getFacade()
Expand Down Expand Up @@ -203,7 +205,11 @@ protected function addFirstNameField(FormBuilderInterface $builder)
{
$builder->add(static::FIELD_FIRST_NAME, TextType::class, [
'label' => 'First Name',
'constraints' => $this->getTextFieldConstraints(),
'constraints' => [
$this->createNotBlankConstraint(),
$this->createLengthConstraint(),
$this->createFirstNameRegexConstraint(),
],
]);

return $this;
Expand All @@ -218,7 +224,11 @@ protected function addLastNameField(FormBuilderInterface $builder)
{
$builder->add(static::FIELD_LAST_NAME, TextType::class, [
'label' => 'Last Name',
'constraints' => $this->getTextFieldConstraints(),
'constraints' => [
$this->createNotBlankConstraint(),
$this->createLengthConstraint(),
$this->createLastNameRegexConstraint(),
],
]);

return $this;
Expand Down Expand Up @@ -386,14 +396,39 @@ protected function addCommentField(FormBuilderInterface $builder)
}

/**
* @return array
* @return \Symfony\Component\Validator\Constraints\NotBlank
*/
protected function getTextFieldConstraints()
protected function createNotBlankConstraint(): NotBlank
{
return [
new NotBlank(),
new Length(['max' => 100]),
];
return new NotBlank();
}

/**
* @return \Symfony\Component\Validator\Constraints\Length
*/
protected function createLengthConstraint(): Length
{
return new Length(['max' => 100]);
}

/**
* @return \Symfony\Component\Validator\Constraints\Regex
*/
protected function createFirstNameRegexConstraint(): Regex
{
return new Regex([
'pattern' => CustomerConfig::PATTERN_FIRST_NAME,
]);
}

/**
* @return \Symfony\Component\Validator\Constraints\Regex
*/
protected function createLastNameRegexConstraint(): Regex
{
return new Regex([
'pattern' => CustomerConfig::PATTERN_LAST_NAME,
]);
}

/**
Expand Down
51 changes: 43 additions & 8 deletions src/Spryker/Zed/Customer/Communication/Form/CustomerForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Spryker\Zed\Customer\Communication\Form;

use DateTime;
use Spryker\Zed\Customer\CustomerConfig;
use Spryker\Zed\Kernel\Communication\Form\AbstractType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
Expand All @@ -22,6 +23,7 @@
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Regex;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

/**
Expand Down Expand Up @@ -201,7 +203,11 @@ protected function addFirstNameField(FormBuilderInterface $builder)
{
$builder->add(static::FIELD_FIRST_NAME, TextType::class, [
'label' => 'First Name',
'constraints' => $this->getTextFieldConstraints(),
'constraints' => [
$this->createNotBlankConstraint(),
$this->createLengthConstraint(),
$this->createFirstNameRegexConstraint(),
],
]);

return $this;
Expand All @@ -216,7 +222,11 @@ protected function addLastNameField(FormBuilderInterface $builder)
{
$builder->add(static::FIELD_LAST_NAME, TextType::class, [
'label' => 'Last Name',
'constraints' => $this->getTextFieldConstraints(),
'constraints' => [
$this->createNotBlankConstraint(),
$this->createLengthConstraint(),
$this->createLastNameRegexConstraint(),
],
]);

return $this;
Expand Down Expand Up @@ -364,14 +374,39 @@ protected function createEmailConstraints()
}

/**
* @return array
* @return \Symfony\Component\Validator\Constraints\NotBlank
*/
protected function getTextFieldConstraints()
protected function createNotBlankConstraint(): NotBlank
{
return [
new NotBlank(),
new Length(['max' => 100]),
];
return new NotBlank();
}

/**
* @return \Symfony\Component\Validator\Constraints\Length
*/
protected function createLengthConstraint(): Length
{
return new Length(['max' => 100]);
}

/**
* @return \Symfony\Component\Validator\Constraints\Regex
*/
protected function createFirstNameRegexConstraint(): Regex
{
return new Regex([
'pattern' => CustomerConfig::PATTERN_FIRST_NAME,
]);
}

/**
* @return \Symfony\Component\Validator\Constraints\Regex
*/
protected function createLastNameRegexConstraint(): Regex
{
return new Regex([
'pattern' => CustomerConfig::PATTERN_LAST_NAME,
]);
}

/**
Expand Down
20 changes: 20 additions & 0 deletions src/Spryker/Zed/Customer/CustomerConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@ class CustomerConfig extends AbstractBundleConfig
*/
public const CUSTOMER_REGISTRATION_WITH_CONFIRMATION_MAIL_TYPE = 'customer registration confirmation mail';

/**
* Specification:
* - Regular expression to validate Customer First Name field.
*
* @api
*
* @var string
*/
public const PATTERN_FIRST_NAME = '/^[^:\/<>]+$/';

/**
* Specification:
* - Regular expression to validate Customer Last Name field.
*
* @api
*
* @var string
*/
public const PATTERN_LAST_NAME = '/^[^:\/<>]+$/';

/**
* @var int
*/
Expand Down

0 comments on commit 6219757

Please sign in to comment.