Skip to content

Instantly share code, notes, and snippets.

@chihiro-adachi
Created February 21, 2019 09:01
Show Gist options
  • Save chihiro-adachi/588d6380a1067b375391bb03553e2794 to your computer and use it in GitHub Desktop.
Save chihiro-adachi/588d6380a1067b375391bb03553e2794 to your computer and use it in GitHub Desktop.
FormEventのサンプル
<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
*
* http://www.lockon.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Eccube\Form\Type;
use Eccube\Entity\Customer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
class TestType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name01', TextType::class);
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
$data = $event->getData();
if ($data instanceof Customer) {
if ($data->getCountry() === null) {
$data->setCountry(0);
}
}
});
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => 'Eccube\Entity\Customer',
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment