logo
down
shadow

Symfony2 Validator message: Which variables are available?


Symfony2 Validator message: Which variables are available?

By : AlanP
Date : November 19 2020, 12:41 AM
wish help you to fix your issue If you check out the code for the LengthValidator you posted as an example you can see that these "variables" are just static strings that are replaced inside their own Validator class.
As such, all of them are custom, which is possibly also why there isn't a list available.
code :
if (null !== $constraint->max && $length > $constraint->max) {
            $this->buildViolation($constraint->min == $constraint->max ? $constraint->exactMessage : $constraint->maxMessage)
                ->setParameter('{{ value }}', $this->formatValue($stringValue))
                ->setParameter('{{ limit }}', $constraint->max)
                ->setInvalidValue($value)
                ->setPlural((int) $constraint->max)
                ->setCode(Length::TOO_LONG_ERROR)
                ->addViolation();
            return;
        }


Share : facebook icon twitter icon
Symfony2 data transformer, validator and error message

Symfony2 data transformer, validator and error message


By : nsyochum
Date : March 29 2020, 07:55 AM
wish of those help I am the one that answered your previous thread so maybe someone else will jump in here.
Your code can be simplified considerably. You are only dealing with user names. No need for use objects or array collections.
code :
public function reverseTransform($val)
{
    if (empty($val)) { return null; }


    // Extract usernames in an array from the raw text
    // $val = str_replace("\r\n", "\n", trim($val));
    $usernames = explode("\n", $val);
    array_map('trim', $usernames);

    // No real need to check for dups here
    return $usernames;
}
public function isValid($userNames, Constraint $constraint)
{
    $repo = $this->em->getRepository('SkepinUserBundle:User');
    $notValidUsernames = array();

    foreach ($userNames as $userName) 
    {
      if (!($user = $repo->findOneByUsername($username))) 
        {
            $notValidUsernames[$userName] = $userName; // Takes care of dups
        }
    }

    if (count($notValidUsernames) == 0) {
        return true;
    }

    // At least one username is not ok here
    $invalidNames = implode(' ,',$notValidUsernames);


$this->setMessage(
        $this->translator->transChoice(
            'form.error.participant_not_found',
            count($notValidUsernames),
            array(
                '%usernames%' => $invalidNames,
                '%last_username%' => end($notValidUsernames)
            )
        )
);

    return false;
}
$threadManager = $this->get('thread.manager');
$threadManager->addUsersToThread($thread,$users);
Symfony2 custom minimum age validator - no error message displayed in twig?

Symfony2 custom minimum age validator - no error message displayed in twig?


By : Ikziriv
Date : March 29 2020, 07:55 AM
it should still fix some issue The reason of this behavior is error bubbling. Set the field's error_bubbling option to false.
Getting specific error message in Twig that was generated from Symfony2 Validator

Getting specific error message in Twig that was generated from Symfony2 Validator


By : bobolly
Date : March 29 2020, 07:55 AM
will help you You cant just access the fieldname of an array of validation-error objects directly. You have to search it by looping.
code :
{% for error in errors %}
   {% if error.propertyPath = 'fieldname' %}
      {{ error.propertyPath }}: {{ error.message }}
   {% endif %}
{% endfor %}
$errors = $validator->validateProperty($insert, 'fieldname);
symfony2 multilanguage message error with form validator through yml

symfony2 multilanguage message error with form validator through yml


By : Rodrigo Cury
Date : March 29 2020, 07:55 AM
it should still fix some issue Validation strings go to validators.LANG.FORMAT instead of messages.LANG.FORMAT. Don't forget to clear the cache after you add a new translation file.
Symfony2 Validator Message depend on group

Symfony2 Validator Message depend on group


By : Jim Carney
Date : March 29 2020, 07:55 AM
should help you out I am having a common validator like this for adding User and patien detail , Try:
code :
Acme\DemoBundle\Entity\contact:
    properties:
        lastName:
            - NotBlank:
                groups: adduser
                message: User Name Should not be blank
            - NotBlank:
                groups: addpatient
                message: Patient Name Should not be blank
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org