Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

受注登録時に郵便番号のバリデーションが効くように修正 #5470

Merged
merged 2 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
35 changes: 21 additions & 14 deletions tests/Eccube/Tests/Form/Type/Admin/OrderTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,85 +88,92 @@ public function testInvalidPhoneNumberBlank()
{
$this->formData['phone_number'] = '';
$this->form->submit($this->formData);
$this->assertFalse($this->form->isValid());
$this->assertFalse($this->form['phone_number']->isValid());
}

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

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

public function testInvalidDiscountOverMaxLength()
{
$this->formData['discount'] = '12345678910'; //Max 9

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

public function testInvalidDiscountNotNumeric()
{
$this->formData['discount'] = 'abcde';

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

public function testInValidDiscountHasMinus()
{
$this->formData['discount'] = '-12345';

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

public function testInvalidDeliveryFeeTotalOverMaxLength()
{
$this->formData['delivery_fee_total'] = '12345678910'; //Max 9

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

public function testInvalidDeliveryFeeTotalNotNumeric()
{
$this->formData['delivery_fee_total'] = 'abcde';

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

public function testInValidDeliveryFeeTotalHasMinus()
{
$this->formData['delivery_fee_total'] = '-12345';

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

public function testInvalidChargeOverMaxLength()
{
$this->formData['charge'] = '12345678910'; //Max 9

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

public function testInvalidChargeNotNumeric()
{
$this->formData['charge'] = 'abcde';

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

public function testInValidChargeHasMinus()
{
$this->formData['charge'] = '-12345';

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

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

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