Adding custom validation for form builder
Steps
1. create two new file - Validator/Constraints
YOURBundle\Validator\Constraints\ContainsAlphanumeric.php
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace Acme\DemoBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
class ContainsAlphanumeric extends Constraint
{
public $message = 'The string "%string%" contains an illegal character: it can only contain letters or numbers.';
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
YOURBundle\Validator\Constraints\ContainsPhoneNumberValidator.php
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// src/Acme/DemoBundle/Validator/Constraints/ContainsAlphanumericValidator.php
namespace Acme\DemoBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class ContainsAlphanumericValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
$pattern = "/^(\+d{2})\s?|\d{4}\s?\d{3}\s?\d{3}$/";
$match = preg_match($pattern,$value);
if (!$match) {
$this->context->addViolation($constraint->message, array('%string%' => $value));
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2. Call Validation class from the form builder __construct method
$this->contains_alphanumeric = new ContainsAlphanumeric();
3. Add as array on constraints
$builder->add(
'name',
'text',
array(
'label' => 'message',
'attr' => array('placeholder' => 'message', 'class' => 'input-large'),
'constraints' => array($this->contains_alphanumeric)
)
);
4. full code of form builder page
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace Acme\DemoBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\Email;
use Acme\DemoBundle\Validator\Constraints\ContainsAlphanumeric;
use Acme\DemoBundle\Validator\Constraints\ContainsAlphanumericValidator;
class ContactType extends AbstractType
{
protected $validEmailConstrain;
protected $notBlankConstrain;
public function __construct()
{
$this->validEmailConstrain = new Email();
$this->contains_alphanumeric = new ContainsAlphanumeric();
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add(
'name',
'text',
array(
'label' => 'message',
'attr' => array('placeholder' => 'message', 'class' => 'input-large'),
'constraints' => array($this->contains_alphanumeric)
)
);
}
public function getName()
{
return 'contact';
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Nice post ,Thanks for sharing this information.
ReplyDeletelooking forward for more posts like this.
Joomla cms