Created
July 6, 2011 16:51
-
-
Save n3b/1067741 to your computer and use it in GitHub Desktop.
symfony2 custom validator
This file contains hidden or 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 n3b\Bundle\Shop\Form\Validator; | |
use Symfony\Component\Form\Extension\Core\Validator\DefaultValidator; | |
use Symfony\Component\Form\FormInterface; | |
class CheckoutDeliveryValidator extends DefaultValidator | |
{ | |
public function validate(FormInterface $form) | |
{ | |
$data = $form->getData(); | |
if(!$data['deliver']) | |
$data['checkout']->unsetDelivery(); | |
else | |
// Symfony2 пока не хочет самостоятельно устанавливать bidirectional связи | |
$data['checkout']->getDelivery()->setCheckout($data['checkout']); | |
} | |
} |
This file contains hidden or 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 n3b\Bundle\Shop\Form; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilder; | |
class CheckoutFullType extends AbstractType | |
{ | |
public function buildForm(FormBuilder $builder, array $options) | |
{ | |
$builder->add('deliver', 'checkbox') | |
->add('user_save', 'checkbox') | |
->add('checkout', new CheckoutType()); | |
$builder->addValidator(new Validator\CheckoutDeliveryValidator()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment