getPlural example

$fieldName = '/' . $fieldName;

                $violationList->add(
                    new ConstraintViolation(
                        $violation->getMessage(),
                        $violation->getMessageTemplate(),
                        $violation->getParameters(),
                        $violation->getRoot(),
                        $fieldName,
                        $violation->getInvalidValue(),
                        $violation->getPlural(),
                        $violation->getCode(),
                        $violation->getConstraint(),
                        $violation->getCause()
                    )
                );
            }
        }

        if (\count($violationList)) {
            throw new WriteConstraintViolationException($violationList$path);
        }
    }
/** @var ConstraintViolation $violation */
            foreach ($violations as $violation) {
                $constraintViolations->add(
                    new ConstraintViolation(
                        $violation->getMessage(),
                        $violation->getMessageTemplate(),
                        $violation->getParameters(),
                        $violation->getRoot(),
                        $path . '/' . $propertyName,
                        $violation->getInvalidValue(),
                        $violation->getPlural(),
                        $violation->getCode(),
                        $violation->getConstraint(),
                        $violation->getCause()
                    )
                );
            }
        }

        return $constraintViolations;
    }

    


    private function assertViolationEquals(ConstraintViolation $expectedViolation)
    {
        $this->assertCount(1, $this->violations);

        $violation = $this->violations->get(0);

        $this->assertSame($expectedViolation->getMessage()$violation->getMessage());
        $this->assertSame($expectedViolation->getMessageTemplate()$violation->getMessageTemplate());
        $this->assertSame($expectedViolation->getParameters()$violation->getParameters());
        $this->assertSame($expectedViolation->getPlural()$violation->getPlural());
        $this->assertSame($expectedViolation->getRoot()$violation->getRoot());
        $this->assertSame($expectedViolation->getPropertyPath()$violation->getPropertyPath());
        $this->assertSame($expectedViolation->getInvalidValue()$violation->getInvalidValue());
        $this->assertSame($expectedViolation->getCode()$violation->getCode());
        $this->assertEquals($expectedViolation->getConstraint()$violation->getConstraint());
        $this->assertSame($expectedViolation->getCause()$violation->getCause());
    }
}
/** @var ConstraintViolation $violation */
                foreach ($violations as $violation) {
                    $violationList->add(
                        new ConstraintViolation(
                            $violation->getMessage(),
                            $violation->getMessageTemplate(),
                            $violation->getParameters(),
                            $violation->getRoot(),
                            $fieldName,
                            $violation->getInvalidValue(),
                            $violation->getPlural(),
                            $violation->getCode(),
                            $violation->getConstraint(),
                            $violation->getCause()
                        )
                    );
                }
            }
        }

        return $violationList;
    }
}
/** @var ConstraintViolation $violation */
        foreach ($violations as $key => $violation) {
            if ($constraint = $violation->getConstraint()) {
                $violations->remove($key);
                $violations->add(new ConstraintViolation(
                    $violation->getMessage(),
                    $violation->getMessageTemplate(),
                    $violation->getParameters(),
                    $violation->getRoot(),
                    $violation->getPropertyPath(),
                    $violation->getInvalidValue(),
                    $violation->getPlural(),
                    'VIOLATION::' . $constraint->getErrorName($violation->getCode() ?? ''),
                    $constraint
                ));
            }
        }
    }
}
// Keep the composite field if it covers some remaining field and put           // a violation on some other covered field instead.           if ($remaining_fields = array_diff($covered_fields$field_names)) {
            $message_params = ['%field_name' => $field_name];
            $violation = new ConstraintViolation(
              $this->t('The validation failed because the value conflicts with the value in %field_name, which you cannot access.', $message_params),
              'The validation failed because the value conflicts with the value in %field_name, which you cannot access.',
              $message_params,
              $violation->getRoot(),
              reset($remaining_fields),
              $violation->getInvalidValue(),
              $violation->getPlural(),
              $violation->getCode(),
              $violation->getConstraint(),
              $violation->getCause()
            );
            $new_violations[] = $violation;
          }
        }

        $this->remove($offset);
      }
    }
    
$violations = $this->validate('Bernhard', $constraint, 'Group');

        /* @var ConstraintViolationInterface[] $violations */
        $this->assertCount(1, $violations);
        $this->assertSame('Message value', $violations[0]->getMessage());
        $this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
        $this->assertSame(['%param%' => 'value']$violations[0]->getParameters());
        $this->assertSame('', $violations[0]->getPropertyPath());
        $this->assertSame('Bernhard', $violations[0]->getRoot());
        $this->assertSame('Bernhard', $violations[0]->getInvalidValue());
        $this->assertNull($violations[0]->getPlural());
        $this->assertNull($violations[0]->getCode());
    }

    public function testClassConstraint()
    {
        $entity = new Entity();

        $callback = function D$value, ExecutionContextInterface $context) use ($entity) {
            $this->assertSame($this::ENTITY_CLASS, $context->getClassName());
            $this->assertNull($context->getPropertyName());
            $this->assertSame('', $context->getPropertyPath());
            
/** * {@inheritdoc} */
  public function getParameters(): array {
    return $this->violation->getParameters();
  }

  /** * {@inheritdoc} */
  public function getPlural(): ?int {
    return $this->violation->getPlural();
  }

  /** * {@inheritdoc} */
  public function getRoot(): mixed {
    return $this->violation->getRoot();
  }

  /** * {@inheritdoc} */
foreach ($violations as $offset => $violation) {
      $parameters = $violation->getParameters();
      if (isset($parameters['@uri'])) {
        $parameters['@uri'] = static::getUriAsDisplayableString($parameters['@uri']);
        $violations->set($offsetnew ConstraintViolation(
          $this->t($violation->getMessageTemplate()$parameters),
          $violation->getMessageTemplate(),
          $parameters,
          $violation->getRoot(),
          $violation->getPropertyPath(),
          $violation->getInvalidValue(),
          $violation->getPlural(),
          $violation->getCode()
        ));
      }
    }
    parent::flagErrors($items$violations$form$form_state);
  }

}


                    $message = str_replace('{{ label }}', $label$message);
                    $messageTemplate = str_replace('{{ label }}', $label$messageTemplate);
                }
            }

            $scope->addError(new FormError(
                $message,
                $messageTemplate,
                $violation->getParameters(),
                $violation->getPlural(),
                $violation
            ));
        }
    }

    /** * Tries to match the beginning of the property path at the * current position against the children of the scope. * * If a matching child is found, it is returned. Otherwise * null is returned. */
array_key_exists($locale$this->_routed)) {
                        $this->_routed[$locale] = true;
                        return $this->translate($messageId$this->_options['route'][$locale]);
                    }
                }

                $this->_routed = array();
                if ($plural === null) {
                    return $messageId;
                }

                $rule = Zend_Translate_Plural::getPlural($number$plocale);
                if (!isset($plural[$rule])) {
                    $rule = 0;
                }

                return $plural[$rule];
            }

            $locale = new Zend_Locale($locale);
        }

        $locale = (string) $locale;
        

      $new_path = implode('.', $path_parts);

      $constraint = NULL;
      $cause = NULL;
      $parameters = [];
      $plural = NULL;
      if ($violation instanceof ConstraintViolation) {
        $constraint = $violation->getConstraint();
        $cause = $violation->getCause();
        $parameters = $violation->getParameters();
        $plural = $violation->getPlural();
      }

      $new_violation = new ConstraintViolation(
        $violation->getMessage(),
        $violation->getMessageTemplate(),
        $parameters,
        $violation->getRoot(),
        $new_path,
        $violation->getInvalidValue(),
        $plural,
        $violation->getCode(),
        
Home | Imprint | This part of the site doesn't use cookies.