Skip to content

Commit

Permalink
Merge pull request #5470 from kiy0taka/fix/order-type-constraints
Browse files Browse the repository at this point in the history
受注登録時に郵便番号のバリデーションが効くように修正
  • Loading branch information
chihiro-adachi authored Aug 2, 2022
2 parents e09643d + fbee989 commit d5b681c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/Eccube/Form/Type/PostalType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TelType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints as Assert;

Expand Down Expand Up @@ -55,29 +54,27 @@ public function buildForm(FormBuilderInterface $builder, array $options)
*/
public function configureOptions(OptionsResolver $resolver)
{
$eccubeConfig = $this->eccubeConfig;
$constraints = function (Options $options) use ($eccubeConfig) {
$resolver->setNormalizer('constraints', function($options, $value) {
$constraints = [];
// requiredがtrueに指定されている場合, NotBlankを追加
if (isset($options['required']) && true === $options['required']) {
$constraints[] = new Assert\NotBlank();
}

$constraints[] = new Assert\Length([
'max' => $eccubeConfig['eccube_postal_code'],
'max' => $this->eccubeConfig['eccube_postal_code'],
]);

$constraints[] = new Assert\Type([
'type' => 'numeric',
'message' => 'form_error.numeric_only',
]);

return $constraints;
};
return array_merge($constraints, $value);
});

$resolver->setDefaults([
'options' => [],
'constraints' => $constraints,
'attr' => [
'class' => 'p-postal-code',
'placeholder' => 'common.postal_code_sample',
Expand Down
7 changes: 7 additions & 0 deletions tests/Eccube/Tests/Form/Type/Admin/OrderTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,11 @@ public function testInValidChargeHasMinus()
$this->form->submit($this->formData);
$this->assertFalse($this->form['charge']->isValid());
}

public function testInvalidPostalCodeToLong()
{
$this->formData['postal_code'] = '012345678';
$this->form->submit($this->formData);
$this->assertFalse($this->form['postal_code']->isValid());
}
}

0 comments on commit d5b681c

Please sign in to comment.