<commit_before><?php

namespace ACSEO\Bundle\FormJsValidationBundle\Service;

class FormValidationIo
{
    private $validator;
    private $translator;

    // http://formvalidation.io/validators/
    private $mapping = array(
        "NotBlank" => "data-fv-notempty",
        "Email" => "data-fv-emailaddress"
    );

    public function __construct($validator, $translator)
    {
        $this->validator = $validator;
        $this->translator = $translator;
    }

    public function addJsValidation($form, $validationGroup = "Default")
    {
        $metadata = $this->validator->getMetadataFor($form->getConfig()->getDataClass());
        $constrainedProperties = $metadata->getConstrainedProperties();
        foreach($form->all() as $field) {
            $name = $field->getConfig()->getName();
            $type = get_class($field->getConfig()->getType()->getInnerType());
            $options = $field->getConfig()->getOptions();

            if (in_array($name, $constrainedProperties)) {
                    $constraints = $metadata->getPropertyMetadata($name)[0]->getConstraints();
                    foreach ($constraints as $constraint) {
                        if (in_array($validationGroup, $constraint->groups)) {
                            if (!isset($options["attr"])) {
                                $options["attr"] = array();
                            }
                            $constraintName = ((new \ReflectionClass($constraint))->getShortName());
                            if (!isset($this->mapping[$constraintName])) {
                                continue;
                                // throw new \Exception("The constraint ". $constraintName." is not known");
                            }
                            $options["attr"][$this->mapping[$constraintName]] = "true";
                            $options["attr"][$this->mapping[$constraintName]."-message"] = $this->translator->trans($constraint->message);
                        }

                    }
                    $form->add($name, $type, $options);
            }
        }

        return $form;
    }

}<commit_msg>Update ValidationService : use closure to define attributes to add to the form. Add Length validation<commit_after><?php

namespace ACSEO\Bundle\FormJsValidationBundle\Service;

class FormValidationIo
{
    private $validator;
    private $translator;

    public function __construct($validator, $translator)
    {
        $this->validator = $validator;
        $this->translator = $translator;
    }

    public function addJsValidation($form, $validationGroup = "Default")
    {

        $mapping = $this->getMapping();

        $metadata = $this->validator->getMetadataFor($form->getConfig()->getDataClass());
        $constrainedProperties = $metadata->getConstrainedProperties();


        foreach($form->all() as $field) {
            $name = $field->getConfig()->getName();
            $type = get_class($field->getConfig()->getType()->getInnerType());
            $options = $field->getConfig()->getOptions();

            if (in_array($name, $constrainedProperties)) {
                    $constraints = $metadata->getPropertyMetadata($name)[0]->getConstraints();
                    foreach ($constraints as $constraint) {
                        if (in_array($validationGroup, $constraint->groups)) {
                            if (!isset($options["attr"])) {
                                $options["attr"] = array();
                            }
                            $constraintName = ((new \ReflectionClass($constraint))->getShortName());
                            if (!isset($mapping[$constraintName])) {
                                continue;
                                //throw new \Exception("The constraint ". $constraintName." is not known");
                            }
                            $newAttrs = call_user_func_array($mapping[$constraintName], [$constraint, $this->translator]);
                            $options["attr"] = array_merge($options["attr"], $newAttrs);

                        }

                    }
                    $form->add($name, $type, $options);
            }
        }

        return $form;
    }

    private function getMapping()
    {
        // http://formvalidation.io/validators/
        $mapping = [
            "NotBlank" => function($constraint, $translator) {
                return array(
                    "data-fv-notempty" => "true",
                    "data-fv-notempty-message" => $translator->trans($constraint->message)
                );
            },
            "Email" => function($constraint, $translator) {
                return array(
                    "data-fv-emailaddress" => "true",
                    "data-fv-notempty-message" => $translator->trans($constraint->message)
                );
            },
            "Length" => function($constraint, $translator) {
                return array(
                    "data-fv-stringlength-max" => $constraint->max,
                    "data-fv-stringlength-min" => $constraint->min,
                    "data-fv-stringlength-message" => $translator->trans($constraint->minMessage).". ".$translator->trans($constraint->maxMessage)
                );
            },
        ];

        return $mapping;
    }

}
{'fraction_non_alphanumeric': 0.14253521126760563, 'fraction_numerical': 0.0003755868544600939, 'mean_word_length': 1.6967088607594936, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4484010', 'n_tokens_mistral': 1316, 'n_tokens_neox': 1265, 'n_words': 212}