Created
November 15, 2012 01:00
-
-
Save ahsio/4075957 to your computer and use it in GitHub Desktop.
code snippet for custom validation constraint for two fields
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
| class Person extends Constraint | |
| { | |
| /** | |
| * (non-PHPdoc) | |
| * @see Symfony\Component\Validator.Constraint::validatedBy() | |
| */ | |
| public function validatedBy() | |
| { | |
| return 'person_validator'; | |
| } | |
| /** | |
| * (non-PHPdoc) | |
| * @see Symfony\Component\Validator.Constraint::getTargets() | |
| */ | |
| public function getTargets() | |
| { | |
| return Constraint::CLASS_CONSTRAINT; | |
| } | |
| } |
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
| class Person | |
| { | |
| /** | |
| * @ORM\Id | |
| * @ORM\Column(type="integer") | |
| * @ORM\GeneratedValue(strategy="AUTO") | |
| */ | |
| protected $id; | |
| /** | |
| * @ORM\Column(type="string", length="255", name="firstname") | |
| */ | |
| protected $firsname; | |
| /** | |
| * @ORM\Column(type="string", length="255", name="lastname") | |
| */ | |
| protected $lastname; | |
| // ... |
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
| public function buildForm(FormBuilder $builder, array $options) | |
| { | |
| $builder | |
| ->add('firsname', 'text', array()) | |
| ->add('lastname', 'text', array()) | |
| ->add('username', 'text', array('property_path' => false)) | |
| ; | |
| } | |
| /** | |
| * (non-PHPdoc) | |
| * @see Symfony\Component\Form.AbstractType::getDefaultOptions() | |
| */ | |
| public function getDefaultOptions(array $options) | |
| { | |
| return array( | |
| 'data_class' => 'Ahsio\StackBundle\Entity\Person', | |
| 'validation_constraint' => new Person(), | |
| ); | |
| } |
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
| class PersonValidator extends ConstraintValidator | |
| { | |
| protected $request; | |
| public function __construct(Request $request) | |
| { | |
| $this->request = $request; | |
| } | |
| /** | |
| * (non-PHPdoc) | |
| * @see Symfony\Component\Validator\ConstraintValidatorInterface::isValid() | |
| */ | |
| public function isValid($value, Constraint $constraint) | |
| { | |
| var_dump($this->request->request->get('person'));die(); | |
| return true; | |
| } | |
| } |
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
| <services> | |
| <service id="person.validator" class="Ahsio\StackBundle\Validator\Constraints\PersonValidator" scope="request"> | |
| <argument type="service" id="request" /> | |
| <tag name="validator.constraint_validator" alias="person_validator" /> | |
| </service> | |
| <!-- ... --> | |
| </services> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment