Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8831027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:13:24+00:00 2026-06-14T08:13:24+00:00

Sometimes happen that a collection of values is stored in a database as a

  • 0

Sometimes happen that a collection of values is stored in a database as a unique string. This string is so made of all the values separated by a user-defined delimiter (e.g. “,” or “_”).

What would be nice in a Symfony2.1 application is to have a Validation Constraint that validates a string (e.g. provided by an input text form) by counting the number of tokens included in that string.

A possible example is when you store the tags in a string format, i.e. you receive a string from an input field like value1,value2,value10,value25. You see that 4 tokens are passed, but there is no form validator that does that control for you. So, one should use such a validator like:

/**
* @Assert\Token(
     *      delimiter=",", 
     *      min = "1",
     *      max = "5",
     *      minMessage = "You must specify at least one token",
     *      maxMessage = "You cannot specify more than 5 tokens")
*/ 
$tags;

There is something similar when using the new in Symfony2.1 Count validator, but is doesn’t work on strings, just on array of objects that implements Countable.

Who know how to implement that kind of “tokenized string” validator?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:13:25+00:00Added an answer on June 14, 2026 at 8:13 am

    I solved my problem, I just want to share my solutions.

    One possible solution is to use a Callback constraint. For instance, following the tag list example provided in the question:

    /**
     * @Assert\Callback(methods={"isTagStringValid"})
     */
    class AFormModel{
    
    protected $tags;
    
        public function isTagStringValid(ExecutionContext $context){
            $tagsExploded = explode(',', $this->tags);
    
            if(count($tagsExploded)==0){
                $context->addViolationAtSubPath('tags', 'Insert at least a tag', array(), null);    
            }
            if(count($tagsExploded)==1 && $tagsExploded[0]==='')
                $context->addViolationAtSubPath('tags', 'Insert at least a tag', array(), null);            
            }
            else if(count($tagsExploded)>10){
                $context->addViolationAtSubPath('tags', 'Max 10 values', array(), null);            
            }
        }
    
    }
    

    A more elegant way is to define the “Token” validator. An example follows here:

    namespace .....
    
    use Symfony\Component\Validator\Constraint;
    
    /**
      * @Annotation
    */
    class Token extends Constraint {
        public $min;
        public $max;
        public $minMessage = '{{ min }} token(s) are expected';
        public $maxMessage = '{{ max }} token(s) are expected';
        public $invalidMessage = 'This value should be a string.';   
        public $delimiter = ',';
    public function __construct($options = null){
        parent::__construct($options);
    
        if (null === $this->min && null === $this->max) {
            throw new MissingOptionsException('Either option "min" or "max" must be given for constraint ' . __CLASS__, array('min', 'max'));
        }
    }    
    

    }

    And the validator class is:

    namespace ...
    
    use Symfony\Component\Validator\Constraint;
    use Symfony\Component\Validator\ConstraintValidator;
    
    class TokenValidator extends ConstraintValidator {
        public function isValid($value, Constraint $constraint) {
            if ($value === null) {
                return;
            }
            if(!is_string($value)){
                $this->context->addViolation($constraint->invalidMessage, array(
                    '{{ value }}' => $value,
                ));
    
                return;
            }
    
            $tokensExploded = explode($constraint->delimiter, $value);
            $tokens = count($tokensExploded);
            if($tokens==1){
                if($tokensExploded[0]==='')
                    $tokens = 0;
            }
    
            if (null !== $constraint->max && $tokens > $constraint->max) {
                $this->context->addViolation($constraint->maxMessage, array(
                    '{{ value }}' => $value,
                    '{{ limit }}' => $constraint->max,
                ));
    
                return;
            }
            if (null !== $constraint->min && $tokens < $constraint->min) {
                $this->context->addViolation($constraint->minMessage, array(
                    '{{ value }}' => $value,
                    '{{ limit }}' => $constraint->min,
                ));
            }        
        }
    }
    

    In this way you can import the user-defined validator and use it everywhere like I proposed in my question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a website, which will be frequently updated. Sometimes changes happen to User
Sometimes it happens that one missing character in a huge js file ruins the
I have a Hgroup that contains of four panels. Sometimes it happens then when
This is an odd behaviour by my D2006 as it happens sometimes only. I
Sometimes users mistakenly redirected to ?Process=ViewImages&PAGEID=. When this happens, they get the following error.
Sometimes i face the following problem : string txt = con.Request.Params[Par_name].ToString();//the original par value
Sometimes when using Visual Studio it displays a prompt with a filename: This file
I have a script that builds my webpage in one string ($content) and then
I have the binary for several pdf files stored in a collection of Byte
I have a mid-size collection of records --about 20 million -- that I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.