1

Currently I'm trying to figure out how to handle the form field validation of a TextType field in Symfony2's form builder context.

There is a field that should explicitly be filled manually with one of two possible values ([email=all], [email=test]). It's intended to be some security feature to assure that the user is definitely aware of what he's doing next.

I'm wondering if there is a validation constraint that can be used to validate the field's input value to fit one of the two known options. Either [email=all] or [email=test]. In plain PHP i would do like so

function isValid($value){
    $options = array("[email=all]","[email=test]");
    return (in_array($value, $options)) ? true : false;
}

I'm aware that I'd build a custom validation constraint but maybe this could even be solved with Symfony standard constraints?

Thanks in advance!

2 Answers 2

4

You can use the Choice constraint.

An example:

// src/AppBundle/EntityAuthor.php
namespace AppBundle\Entity;

use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints as Assert;

class Author
{
    protected $gender;

    public static function loadValidatorMetadata(ClassMetadata $metadata)
    {
        $metadata->addPropertyConstraint('gender', new Assert\Choice(array(
            'choices' => array('male', 'female'),
            'message' => 'Choose a valid gender.',
        )));
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer. That's exactly what i was looking for! Kind regards!
2

Yes, it is possible!
You should use either Callback constraint validation or Expression constraint. That last one is the easiest and pragmatic way to go. Consider an entity called Task:

<?php 

namespace AppBundle\Entity;

class Task 
{
    protected $id;
    protected $title;  

    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }   
    public function getTitle()
    {
        return $this->title;
    }

    //...
}

Now we assume the title property MUST have a foo or bar word, or whether some value (from title property) is in an defined array:

 # src/AppBundle/Resources/config/validation.yml
    AppBundle\Entity\Task:
        properties:
            title: 
                - Expression: 
                    expression: "this.getTitle() in ['foo', 'bar']", 
                    message: "Title must be 'foo' or 'bar'!"
                - NotBlank: ~

So in your specific case, just swap ['foo', 'bar'] by ['[email=all]', '[email=test]'] or whatever you want.

1 Comment

Thanks for your help. I'm not a friend of doing the validation on the entity itself (no matter if via yml or annotation). Is there a way to set it as constraint directly in the form builder class?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.