Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2113 from chihiro-adachi/fix-form-session
会員マスタの検索条件のセッション保持方法を変更
- Loading branch information
Showing
4 changed files
with
238 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
/* | ||
* This file is part of EC-CUBE | ||
* | ||
* Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved. | ||
* | ||
* http://www.lockon.co.jp/ | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License | ||
* as published by the Free Software Foundation; either version 2 | ||
* of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
*/ | ||
|
||
namespace Eccube\Util; | ||
|
||
|
||
use Symfony\Component\Form\FormInterface; | ||
|
||
class FormUtil | ||
{ | ||
/** | ||
* formオブジェクトからviewDataを取得する. | ||
* | ||
* @param FormInterface $form | ||
* @return array | ||
*/ | ||
public static function getViewData(FormInterface $form) | ||
{ | ||
$viewData = array(); | ||
$forms = $form->all(); | ||
|
||
if (empty($forms)) { | ||
return $form->getViewData(); | ||
} | ||
|
||
foreach ($forms as $key => $value) { | ||
// choice typeは各選択肢もFormとして扱われるため再帰しない. | ||
if ($value->getConfig()->hasOption('choices')) { | ||
$viewData[$key] = $value->getViewData(); | ||
} else { | ||
$viewData[$key] = self::getViewData($value); | ||
} | ||
} | ||
|
||
return $viewData; | ||
} | ||
|
||
/** | ||
* formオブジェクトにviewdataをsubmitし, マッピングした結果を返す. | ||
* | ||
* @param FormInterface $form | ||
* @param $viewData | ||
* @return mixed | ||
*/ | ||
public static function submitAndGetData(FormInterface $form, $viewData) | ||
{ | ||
$form->submit($viewData); | ||
|
||
return $form->getData(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
<?php | ||
|
||
namespace Eccube\Tests\Util; | ||
|
||
use Eccube\Tests\EccubeTestCase; | ||
use Eccube\Util\FormUtil; | ||
|
||
class FormUtilTest extends EccubeTestCase | ||
{ | ||
protected $form; | ||
|
||
protected $formData = array( | ||
'pref' => '28', | ||
'name' => 'パーコレータ', | ||
'date' => '2017-02-01' | ||
); | ||
|
||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->form = $this->app['form.factory'] | ||
->createBuilder( | ||
'form', | ||
null, | ||
array( | ||
'csrf_protection' => false, | ||
) | ||
) | ||
->add('pref', 'pref') | ||
->add('name', 'text') | ||
->add('date', 'date', array( | ||
'label' => '受注日(FROM)', | ||
'required' => false, | ||
'input' => 'datetime', | ||
'widget' => 'single_text', | ||
'format' => 'yyyy-MM-dd', | ||
'empty_value' => array('year' => '----', 'month' => '--', 'day' => '--'), | ||
)) | ||
->getForm(); | ||
} | ||
|
||
public function testGetViewData() | ||
{ | ||
$this->form->submit($this->formData); | ||
|
||
$viewData = FormUtil::getViewData($this->form); | ||
|
||
// POSTしたデータと同じになるはず. | ||
$this->assertEquals($this->formData, $viewData); | ||
} | ||
|
||
public function testSubmitAndGetData() | ||
{ | ||
$data = FormUtil::submitAndGetData($this->form, $this->formData); | ||
|
||
// formはsubmitされている. | ||
$this->assertTrue($this->form->isSubmitted()); | ||
|
||
// prefはPrefエンティティに変換されている. | ||
$this->assertInstanceOf('\Eccube\Entity\Master\Pref', $data['pref']); | ||
$this->assertEquals(28, $data['pref']->getId()); | ||
$this->assertEquals('兵庫県', $data['pref']->getName()); | ||
|
||
// dateはDateTimeに変換されている. | ||
$this->assertInstanceOf('\DateTime', $data['date']); | ||
} | ||
|
||
/** | ||
* AddressTypeなど, 子要素をもつFormTypeのテスト. | ||
*/ | ||
public function testNestedFormType() | ||
{ | ||
$formData = array( | ||
'address' => array( | ||
'pref' => '27', | ||
'addr01' => '北区', | ||
'addr02' => '梅田' | ||
) | ||
); | ||
|
||
$form = $this->app['form.factory'] | ||
->createBuilder( | ||
'form', | ||
null, | ||
array( | ||
'csrf_protection' => false, | ||
) | ||
) | ||
->add('address', 'address') | ||
->getForm(); | ||
|
||
$form->submit($formData); | ||
$viewData = FormUtil::getViewData($form); | ||
$this->assertEquals($formData, $viewData); | ||
} | ||
|
||
/** | ||
* choice typeのテスト | ||
*/ | ||
public function testChoiceType() | ||
{ | ||
$formData = array( | ||
'sex' => '1', | ||
); | ||
|
||
$form = $this->app['form.factory'] | ||
->createBuilder( | ||
'form', | ||
null, | ||
array( | ||
'csrf_protection' => false, | ||
) | ||
) | ||
->add('sex', 'sex') | ||
->getForm(); | ||
|
||
$form->submit($formData); | ||
$viewData = FormUtil::getViewData($form); | ||
$this->assertEquals($formData, $viewData); | ||
} | ||
|
||
|
||
/** | ||
* choice type(multiple)のテスト | ||
*/ | ||
public function testChoiceTypeMultiple() | ||
{ | ||
$formData = array( | ||
'sex' => array('1', '2') | ||
); | ||
|
||
$form = $this->app['form.factory'] | ||
->createBuilder( | ||
'form', | ||
null, | ||
array( | ||
'csrf_protection' => false, | ||
) | ||
) | ||
->add('sex', 'sex', array( | ||
'multiple' => true, | ||
)) | ||
->getForm(); | ||
|
||
$form->submit($formData); | ||
$viewData = FormUtil::getViewData($form); | ||
$this->assertEquals($formData, $viewData); | ||
} | ||
|
||
} |