0

I'm using Respect to validate some forms in a project but this project is in Spanish and I don't understand how messages work after spending a long time reading the documentation and even its code.

I'm using Slim and I'm using a NestedValidationException following an example I read on a tutorial on Youtube.

This is the validate method:

public function validate($request, array $rules) 
    {
        foreach ($rules as $field => $rule) {
            try {
                $rule->setName(ucfirst($field))->assert($request->getParam($field));
            } catch (NestedValidationException $e) {
                    $e->findMessages([
                    'usernameAvailable' => '{{name}} ya existe en la base de datos',
                    'emailAvailable' => '{{name}} ya existe en la base de datos',
                    'notEmpty' => '{{name}} no puede estar vacío',
                    'noWhitespace' => '{{name}} no puede contener espacios',
                    'email' => '{{name}} debe contener un e-mail válido'
                ]);
                 //In English it's enough with 
                 //$this->errors[$field] = $e->getMessages();
                     $this->$errors[$field] = $e->getMainMessage();

            }
        }

        $_SESSION['errors'] = $this->errors;



        return $this;
    }

I've seen some responses before but some are very hard to grasp for me as I don't intend on doing a whole translation of the library. I'm just attempting to write 5 or 6 custom messages.

EDIT: A method using the messages:

 $validation = $this->c->validator->validate($request, [
            'username' => v::noWhitespace()->notEmpty()->usernameAvailable(),
            'email'  => v::noWhitespace()->notEmpty()->email()->emailAvailable(),
            'password1' => v::noWhitespace()->notEmpty(),
            'password2' => v::noWhitespace()->notEmpty()->identical($inputPassword),

        ]);
ffuentes
  • 1,042
  • 5
  • 16
  • 36

1 Answers1

1

Using findMessages with the parameter you're translating the messages. You just have to replace the content in the catch block:

catch (NestedValidationException $e) {
    $errors = $e->findMessages([
        'usernameAvailable' => '{{name}} ya existe en la base de datos',
        'emailAvailable' => '{{name}} ya existe en la base de datos',
        'notEmpty' => '{{name}} no puede estar vacío',
        'noWhitespace' => '{{name}} no puede contener espacios',
        'email' => '{{name}} debe contener un e-mail válido'
    ]);
    $filteredErrors = array_filter($errors); // Ensure the array is not containing empty values
    $this->$errors[$field] = $filteredErrors;
}
Davide Pastore
  • 8,678
  • 10
  • 39
  • 53
  • I have the exact same setup and when using the findMessages method, I do get back my custom messages, but the {{ name }} parameter is not being replaced. I have chained setName('Vorname') on the validation rules chain but no success It only works when using the getMessages method but then the original english messages are shown Anyone an idea why this might be ? – Chris Jun 29 '19 at 14:38
  • @Chris I think it's better to create a new question to explain the context of your issue and to find more users that could answer. – Davide Pastore Sep 11 '19 at 14:32