Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Validation Message #37

Closed
rogerioitcom opened this issue Jun 26, 2018 · 6 comments
Closed

Custom Validation Message #37

rogerioitcom opened this issue Jun 26, 2018 · 6 comments

Comments

@rogerioitcom
Copy link

rogerioitcom commented Jun 26, 2018

Hi, there. Thanks for sharing with us this amazing class!

Well, my validation code is working pretty well but I didn't understand how to custom validation messages. I want to do that cause I need generate messages into brazilian portuguese.

See the below code:

$validation = $validator->make($_POST, [
      'name' => 'required|min:3',
      'age' => 'required|numeric'
    ]);

How do I custom messages to those fields using rakit class?

Thanks in advance.

@emsifa
Copy link
Member

emsifa commented Jun 27, 2018

You can use setMessages like this:

$validation->setMessages([
    // Custom error by field name
    'name' => 'You have invalid name', 
    // or make it specific
    'name:required' => 'Field name is required guys',
    // or by rule name
    'required' => 'Field :attribute is required guys'
]);

// then validate it
$validation->validate();

@rogerioitcom
Copy link
Author

Hi, Emsifa. Thank you very much for you quick reply. My code is running pretty nice right now.

I have another question: How do I use the unique validation with MVC?

My project is under MVC pattern and I'd like to use your class to verify if a column name is already taken

@emsifa
Copy link
Member

emsifa commented Jun 27, 2018

You can add custom rule class like this.

@rogerioitcom
Copy link
Author

rogerioitcom commented Jun 27, 2018

Thanks for helping me again.

      'name:required' => 'Preencha o seu nome.',
      'name:min' => 'O campo nome deve ter no mínimo 3 carecteres.',
      'name:regex' => 'Por favor, o campo nome deve conter apenas letras.',

      'age:required' => 'Preencha a sua senha.',
      'age:min' => 'O campo idade deve ter no mínimo 6 caracteres.',
      'age:numeric' => 'O campo idade aceita somente números.'

Can I put the above code inside of setMessages() using json?

@emsifa
Copy link
Member

emsifa commented Jun 27, 2018

No, you should json_decode($json_string, true) it.

@emsifa
Copy link
Member

emsifa commented Jun 29, 2018

I think this issue have been solved. I should close this. Thanks.

@emsifa emsifa closed this as completed Jun 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants