Last active
August 20, 2018 14:34
-
-
Save codeinabox/65ace603403ce69c189a320e88a45d29 to your computer and use it in GitHub Desktop.
Form builder default value for checkbox
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
<?php | |
namespace AppBundle\Form\Type; | |
use AppBundle\DTO\Tag; | |
use Symfony\Component\Form\FormInterface; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\OptionsResolver\OptionsResolver; | |
use Symfony\Component\OptionsResolver\Exception\AccessException; | |
use Symfony\Component\Form\Extension\Core\Type\CollectionType; | |
use Symfony\Component\Form\Extension\Core\Type\TextType; | |
use Symfony\Component\Form\Extension\Core\Type\IntegerType; | |
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; | |
/** | |
* Class CreateTagType | |
*/ | |
class CreateTagType extends AbstractType | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder | |
->add('name', TextType::class, ['required' => true]) | |
->add('emailVisible', CheckboxType::class, ['required' => false]) | |
->add('mobileVisible', CheckboxType::class, ['required' => false]) | |
->add('skypeVisible', CheckboxType::class, ['required' => false]); | |
} | |
/** | |
* {@inheritdoc} | |
* | |
* @throws AccessException If called from a lazy option or normalizer | |
*/ | |
public function configureOptions(OptionsResolver $resolver) | |
{ | |
parent::configureOptions($resolver); | |
$resolver->setDefaults([ | |
'csrf_protection' => false, | |
'method' => 'POST', | |
'data_class' => Tag::class, | |
'empty_data' => new Tag() | |
]); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getBlockPrefix() | |
{ | |
return ''; | |
} | |
} |
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
<?php | |
namespace Tests\AppBundle\Form\Type; | |
use AppBundle\DTO\Tag; | |
use AppBundle\Form\Type\CreateTagType; | |
use Symfony\Component\Form\Test\TypeTestCase; | |
class CreateTagTypeTest extends TypeTestCase | |
{ | |
public function testShouldSubmitFormData() | |
{ | |
$formData = ['name' => 'cheese']; | |
$form = $this->factory->create(CreateTagType::class); | |
$form->submit($formData); | |
$tag = $form->getData(); | |
$this->assertInstanceOf(Tag::class, $tag); | |
$this->assertEquals('cheese', $tag->getName()); | |
$this->assertTrue($tag->isMobileVisible()); | |
$this->assertTrue($tag->isSkypeVisible()); | |
$this->assertTrue($tag->isEmailVisible()); | |
} | |
public function testShouldToggleVisibility() | |
{ | |
$formData = [ | |
'name' => 'cheese', | |
'emailVisible' => false, | |
'skypeVisible' => false, | |
'mobileVisible' => true | |
]; | |
$form = $this->factory->create(CreateTagType::class); | |
$form->submit($formData); | |
$tag = $form->getData(); | |
$this->assertTrue($tag->isMobileVisible()); | |
$this->assertFalse($tag->isSkypeVisible()); | |
$this->assertFalse($tag->isEmailVisible()); | |
} | |
} |
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
<?php declare(strict_types=1); | |
namespace AppBundle\DTO; | |
use Symfony\Component\Validator\Constraints as Assert; | |
use JMS\Serializer\Annotation; | |
/** | |
* Class Tag | |
*/ | |
class Tag | |
{ | |
/** | |
* @Assert\NotBlank() | |
* @Assert\Length( | |
* min = 1, | |
* max = 80, | |
* minMessage = "Tag name must be between 1 and 80 characters long.", | |
* maxMessage = "Tag name must be between 1 and 80 characters long." | |
* ) | |
* | |
* @var string | |
* @Annotation\Type("string") | |
*/ | |
private $name; | |
/** | |
* @var boolean | |
* @Annotation\Type("boolean") | |
*/ | |
private $emailVisible; | |
/** | |
* @var boolean | |
* @Annotation\Type("boolean") | |
*/ | |
private $mobileVisible; | |
/** | |
* @var boolean | |
* @Annotation\Type("boolean") | |
*/ | |
private $skypeVisible; | |
public function __construct() | |
{ | |
$this->emailVisible = true; | |
$this->mobileVisible = true; | |
$this->skypeVisible = true; | |
} | |
/** | |
* @return string | |
*/ | |
public function getName() | |
{ | |
return $this->name; | |
} | |
/** | |
* @param string $name | |
* @return Tag | |
*/ | |
public function setName($name) | |
{ | |
$this->name = $name; | |
return $this; | |
} | |
/** | |
* @return bool | |
*/ | |
public function isEmailVisible(): bool | |
{ | |
return $this->emailVisible; | |
} | |
/** | |
* @param bool $visible | |
* @return Tag | |
*/ | |
public function setEmailVisible(bool $visible) | |
{ | |
$this->emailVisible = $visible; | |
return $this; | |
} | |
/** | |
* @return bool | |
*/ | |
public function isMobileVisible(): bool | |
{ | |
return $this->mobileVisible; | |
} | |
/** | |
* @param bool $visible | |
* @return Tag | |
*/ | |
public function setMobileVisible(bool $visible) | |
{ | |
$this->mobileVisible = $visible; | |
return $this; | |
} | |
/** | |
* @return bool | |
*/ | |
public function isSkypeVisible(): bool | |
{ | |
return $this->skypeVisible; | |
} | |
/** | |
* @param bool $visible | |
* @return Tag | |
*/ | |
public function setSkypeVisible(bool $visible) | |
{ | |
$this->skypeVisible = $visible; | |
return $this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment