0

I'm currently fighting with ExpressionLanguage validation regarding to a constraint wanted on form validation :

My form :

  • Text item
  • Entity1 item selector (dropdown)
  • Entity2 item selector (dropdown)
  • Textarea item

My wishes :

  • One error trigger on condition Text Item is empty and Entity1 selector is empty
  • Another error trigger on condition : Entity1.id == XX and Entity2.id == YY and Textarea is empty

So far, I got this in validation.yml :

Experveo\ShopBundle\Entity\Vehicle:
    constraints:
        - Expression:
            expression: "this.getTextItem() != '' |  this.getEntity1() != ''"
            message: error1.

It seems I need to put the opposite condition to make it work. So far I didn't find any advanced clue on documentation, nor in expression language syntax help.

How can I achieve those conditions? Following is not working at all...

- Expression:    
    expression: >
        this.getTextItem() == ''
        && this.getEntity1() != ''
        && this.getEntity1().getId() === 49
        && this.getEntity2() != ''
        && this.getEntity2() === 914
        && this.getTextAreaItem() == ''"
    message: error2.
3
  • When you're getting this complex I might look at creating a custom validation constraint, especially seeing hardcoded values like that. It also seems like you'd want to check == '' for your two entities, not the other way around. Commented Jan 4, 2016 at 18:16
  • Thanks for your reply. I would say your answer is the "big" tool, allowing even more complex validation patterns and logic. The Callback I suggest below suited my needs. But thank you for pointing me this lead. Commented Jan 8, 2016 at 21:34
  • No problem - the nice thing about Symfony is that flexibility to do either and not always require the most complex solution to solve a simple problem Commented Jan 8, 2016 at 21:37

1 Answer 1

0

I finally found a solution. As I run on 2.4 and according to this fix merged later on, the ExpressionValidator was incorrectly skipping validation of null or empty string values.

Therefore, the callback option is a correct workaround, as mentionned in this other SO post. In my case :

 /**
 * @Assert\Callback
 *
 * Using a callback is a better solution insted of make an "Assert\Expression" for the class,
 * because of a Symfony bug where the ExpressionValidator skip validating if the value if empty or null.
 * Exemple for information : https://stackoverflow.com/questions/24649713/assert-expression-validation-not-working-at-attribute-level-in-symfony-2-4
 */
public function validate(ExecutionContextInterface $context)
{
    if( '' === $this->getTextItem() && '' === $this->getMark() ){
        $context->buildViolation("error 1")
            ->addViolation();
    }

    if( '' === $this->getTextItem()
        && '' === $this->getTextAreaItem()
        && (( $this->getEntity1() && $this->getEntity1()->getId() === self::ENTITY1_VALUE)
             || ($this->getEntity2() && $this->getEntity2()->getId() === self::ENTITY2_VALUE))  
    ){
        $context->buildViolation("error2")
            ->addViolation();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.