1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of EC-CUBE |
4
|
|
|
* |
5
|
|
|
* Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved. |
6
|
|
|
* |
7
|
|
|
* http://www.lockon.co.jp/ |
8
|
|
|
* |
9
|
|
|
* This program is free software; you can redistribute it and/or |
10
|
|
|
* modify it under the terms of the GNU General Public License |
11
|
|
|
* as published by the Free Software Foundation; either version 2 |
12
|
|
|
* of the License, or (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* This program is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU General Public License |
20
|
|
|
* along with this program; if not, write to the Free Software |
21
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
22
|
|
|
*/ |
23
|
|
|
namespace Eccube\Form\Type; |
24
|
|
|
use Symfony\Component\Form\AbstractType; |
25
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
26
|
|
|
use Symfony\Component\Form\FormError; |
27
|
|
|
use Symfony\Component\Form\FormEvents; |
28
|
|
|
use Symfony\Component\Form\FormInterface; |
29
|
|
|
use Symfony\Component\Form\FormView; |
30
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
31
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
32
|
|
|
|
33
|
|
|
class TelType extends AbstractType |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
703 |
|
public function __construct($config = array('tel_len' => 5, 'tel_len_min' => 2)) |
39
|
|
|
{ |
40
|
703 |
|
$this->config = $config; |
41
|
|
|
} |
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
242 |
|
public function buildForm(FormBuilderInterface $builder, array $options) |
46
|
|
|
{ |
47
|
242 |
|
$options['tel01_options']['required'] = $options['required']; |
48
|
242 |
|
$options['tel02_options']['required'] = $options['required']; |
49
|
242 |
|
$options['tel03_options']['required'] = $options['required']; |
50
|
|
|
// required の場合は NotBlank も追加する |
51
|
242 |
View Code Duplication |
if ($options['required']) { |
|
|
|
|
52
|
160 |
|
$options['options']['constraints'] = array_merge(array( |
53
|
160 |
|
new Assert\NotBlank(array()), |
54
|
160 |
|
), $options['options']['constraints']); |
55
|
|
|
} |
56
|
242 |
View Code Duplication |
if (!isset($options['options']['error_bubbling'])) { |
57
|
242 |
|
$options['options']['error_bubbling'] = $options['error_bubbling']; |
58
|
|
|
} |
59
|
|
|
// nameは呼び出しもので定義したものを使う |
60
|
242 |
|
if (empty($options['tel01_name'])) { |
61
|
242 |
|
$options['tel01_name'] = $builder->getName().'01'; |
62
|
|
|
} |
63
|
242 |
|
if (empty($options['tel02_name'])) { |
64
|
242 |
|
$options['tel02_name'] = $builder->getName().'02'; |
65
|
|
|
} |
66
|
242 |
|
if (empty($options['tel03_name'])) { |
67
|
242 |
|
$options['tel03_name'] = $builder->getName().'03'; |
68
|
|
|
} |
69
|
|
|
// 全角英数を事前に半角にする |
70
|
242 |
|
$builder->addEventSubscriber(new \Eccube\EventListener\ConvertKanaListener()); |
71
|
|
|
$builder |
72
|
242 |
|
->add($options['tel01_name'], 'text', array_merge_recursive($options['options'], $options['tel01_options'])) |
73
|
242 |
|
->add($options['tel02_name'], 'text', array_merge_recursive($options['options'], $options['tel02_options'])) |
74
|
242 |
|
->add($options['tel03_name'], 'text', array_merge_recursive($options['options'], $options['tel03_options'])) |
75
|
|
|
; |
76
|
242 |
|
$builder->setAttribute('tel01_name', $options['tel01_name']); |
77
|
242 |
|
$builder->setAttribute('tel02_name', $options['tel02_name']); |
78
|
242 |
|
$builder->setAttribute('tel03_name', $options['tel03_name']); |
79
|
|
|
// todo 変 |
80
|
242 |
|
$builder->addEventListener(FormEvents::POST_BIND, function ($event) use ($builder) { |
81
|
215 |
|
$form = $event->getForm(); |
82
|
215 |
|
$count = 0; |
83
|
215 |
|
if ($form[$builder->getName().'01']->getData() != '') { |
84
|
205 |
|
$count++; |
85
|
|
|
} |
86
|
215 |
|
if ($form[$builder->getName().'02']->getData() != '') { |
87
|
207 |
|
$count++; |
88
|
|
|
} |
89
|
215 |
|
if ($form[$builder->getName().'03']->getData() != '') { |
90
|
207 |
|
$count++; |
91
|
|
|
} |
92
|
215 |
|
if ($count != 0 && $count != 3) { |
93
|
|
|
// todo メッセージをymlに入れる |
94
|
5 |
|
$form[$builder->getName().'01']->addError(new FormError('全て入力してください。')); |
95
|
|
|
} |
96
|
242 |
|
}); |
97
|
|
|
} |
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
39 |
|
public function buildView(FormView $view, FormInterface $form, array $options) |
102
|
|
|
{ |
103
|
39 |
|
$builder = $form->getConfig(); |
104
|
39 |
|
$view->vars['tel01_name'] = $builder->getAttribute('tel01_name'); |
105
|
39 |
|
$view->vars['tel02_name'] = $builder->getAttribute('tel02_name'); |
106
|
39 |
|
$view->vars['tel03_name'] = $builder->getAttribute('tel03_name'); |
107
|
|
|
} |
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
242 |
|
public function configureOptions(OptionsResolver $resolver) |
112
|
|
|
{ |
113
|
242 |
|
$resolver->setDefaults(array( |
114
|
242 |
|
'options' => array('constraints' => array()), |
115
|
|
|
'tel01_options' => array( |
116
|
|
|
'constraints' => array( |
117
|
242 |
|
new Assert\Type(array('type' => 'numeric', 'message' => 'form.type.numeric.invalid')), //todo messageは汎用的に出来ないものか? |
118
|
242 |
|
new Assert\Length(array('max' => $this->config['tel_len'], 'min' => $this->config['tel_len_min'])), |
119
|
|
|
), |
120
|
|
|
), |
121
|
|
|
'tel02_options' => array( |
122
|
|
|
'constraints' => array( |
123
|
242 |
|
new Assert\Type(array('type' => 'numeric', 'message' => 'form.type.numeric.invalid')), |
124
|
242 |
|
new Assert\Length(array('max' => $this->config['tel_len'], 'min' => $this->config['tel_len_min'])), |
125
|
|
|
), |
126
|
|
|
), |
127
|
|
|
'tel03_options' => array( |
128
|
|
|
'constraints' => array( |
129
|
242 |
|
new Assert\Type(array('type' => 'numeric', 'message' => 'form.type.numeric.invalid')), |
130
|
242 |
|
new Assert\Length(array('max' => $this->config['tel_len'], 'min' => $this->config['tel_len_min'])), |
131
|
|
|
), |
132
|
|
|
), |
133
|
242 |
|
'tel01_name' => '', |
134
|
242 |
|
'tel02_name' => '', |
135
|
242 |
|
'tel03_name' => '', |
136
|
|
|
'error_bubbling' => false, |
137
|
|
|
'inherit_data' => true, |
138
|
|
|
'trim' => true, |
139
|
|
|
)); |
140
|
|
|
} |
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
703 |
|
public function getName() |
145
|
|
|
{ |
146
|
703 |
|
return 'tel'; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.