Skip to content

Commit

Permalink
受注登録時に電話番号のバリデーションを効くように修正
Browse files Browse the repository at this point in the history
  • Loading branch information
kiy0taka committed Jul 22, 2022
1 parent 091b3b1 commit 36cbdeb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/Eccube/Form/Type/PhoneNumberType.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 @@ -56,29 +55,26 @@ 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_tel_len_max'],
'max' => $this->eccubeConfig['eccube_tel_len_max'],
]);

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

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

$resolver->setDefaults([
'options' => ['constraints' => []],
'constraints' => $constraints,
'attr' => [
'placeholder' => 'common.phone_number_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 @@ -162,4 +162,11 @@ public function testInValidChargeHasMinus()
$this->form->submit($this->formData);
$this->assertFalse($this->form->isValid());
}

public function testInvalidPhoneNumberTooLong() {
$this->formData['phone_number'] = '0123456789012345';

$this->form->submit($this->formData);
$this->assertFalse($this->form['phone_number']->isValid());
}
}

0 comments on commit 36cbdeb

Please sign in to comment.