0

I've got a basic field constraint figured out (per question here: Validation constraints on form fields ignored )

Next, I want to constrain the main image field to limit the file name. For example, if someone uploads a file named "1234-shutterstock.jpg" I want to detect that the file name contains "shutterstock" and add that violation.

But I have no clue how to tackle the syntax for this. Maybe someone can point me in the right direction?

I've started with something like this. I know this is wrong.

            <?php   
            namespace Drupal\custom_validation\Plugin\Validation\Constraint;

            use Symfony\Component\Validator\Constraint;
            use Symfony\Component\Validator\ConstraintValidator;

            /**
             * Validates the Image constraint.
             */
            class MainImageConstraintValidator extends ConstraintValidator {

              /**
               * {@inheritdoc}
               */
              public function validate($items, Constraint $constraint) {
                foreach ($items as $item) {   
                  if (strpos($item->value, 'shutterstock') !== false) {
                    $this->context->addViolation($constraint->wrongImageName, ['%value' => $item->value]);
                  }

                }
              }

            }
2
  • 1
    Try $item->entity->filename->value in the strpos
    – Clive
    Dec 5, 2018 at 20:06
  • Perfect! I didn't think it was gonna be that easy!
    – turpentyne
    Dec 5, 2018 at 20:13

1 Answer 1

0

Per Clive's comment above, it works with this:

        namespace Drupal\custom_validation\Plugin\Validation\Constraint;

        use Symfony\Component\Validator\Constraint;
        use Symfony\Component\Validator\ConstraintValidator;

        /**
         * Validates the Image constraint.
         */
        class MainImageConstraintValidator extends ConstraintValidator {

          /**
           * {@inheritdoc}
           */
          public function validate($items, Constraint $constraint) {
            foreach ($items as $item) {   
              if (strpos($item->entity->filename->value, 'shutterstock') !== false) {
                $this->context->addViolation($constraint->wrongImageName, ['%value' => $item->value]);
              }

            }
          }

        }
6
  • This is missing the pattern match on the value?
    – Kevin
    Dec 5, 2018 at 22:25
  • Sorry, I'm not understanding the question? I added in your $item->entity->filename->value, and it does exactly what I want.
    – turpentyne
    Dec 5, 2018 at 22:40
  • if ($item->entity->filename->value, 'shutterstock') !== false) how does that work? Nothing is being evaluated.
    – Kevin
    Dec 5, 2018 at 22:44
  • I'll have to research to understand better. I assume it's in the core ConstraintValidator that this class is extending? and/or the other files that I didn't include from the module, as I didn't think they were necessary to show. At this moment, all I can say is it's doing exactly what I want.
    – turpentyne
    Dec 5, 2018 at 22:49
  • It’s in the code you posted in the answer that you say works. You’re not doing a strpos or preg_match or anything on the value - so how does this work?
    – Kevin
    Dec 5, 2018 at 22:52

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.