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 messages set via buildForm #225

Closed
koichirose opened this issue Apr 12, 2016 · 9 comments
Closed

Custom validation messages set via buildForm #225

koichirose opened this issue Apr 12, 2016 · 9 comments

Comments

@koichirose
Copy link
Contributor

I have the following in my buildForm() function:

$this
            ->add('title', 'text', [
                'label' => 'Title',
                'rules' => 'required'
            ])

Is there a way to set custom validation messages for the 'required' field directly in that array? I couldn't find anything.
Since this is needed for each form of that type, I'd like to avoid doing that in the controller as shown in the docs:

$form->validate(['title' => 'required'], [
    'title.required' => 'Please provide a valid title.'
]);
@kristijanhusak
Copy link
Owner

It's not possible to do it that way at this moment, but it is a good idea. I'll mark it as feature and work on it when i get some time.

@koichirose
Copy link
Contributor Author

Thank you. As always, let me know if you need help with testing it.

@kristijanhusak
Copy link
Owner

@koichirose I added this in the latest release (1.7.0). You can update and start using it.
There is short explanation in the docs here http://kristijanhusak.github.io/laravel-form-builder/overview/validation.html
If it works, please close the issue.

@koichirose
Copy link
Contributor Author

koichirose commented Jun 3, 2016

@kristijanhusak it doesn't work for child forms.
Example (userform):

<?php namespace App\Forms;

use Kris\LaravelFormBuilder\Form;

class UserForm extends Form
{
    protected $clientValidationEnabled = false;    
    public function buildForm()
    {
        $model = $this->getModel();
        if ($model) {
            $model->load('user_data');
        }

        $this
            ->add('name', 'text', [
                'rules' => 'required',
                'label' => trans('users.name'),
                'error_messages' => [
                    'name.required' => 'testing error message parent'
                ],
            ])
            [.....more fields.....]
        ->add('user_data', 'form', [
                'class' =>  'App\Forms\UserDataForm',
                'wrapper' => false,
                'model' => $this->getModel(),
                'label' => false,
            ])

UserDataForm:

<?php namespace App\Forms;

use Kris\LaravelFormBuilder\Form;

class UserDataForm extends Form
{
    protected $clientValidationEnabled = false;
    public function buildForm()
    {
        $model = $this->getModel();
        $model = ($model instanceof \App\Models\User) ? $model : null;

        $this
            ->add('first_name', 'text', [
                'rules' => 'required',
                'label' => trans('user_data.first_name'),
                'error_messages' => [
                    'first_name.required' => 'testing error message child'
                ],
            ])

I disabled client validation.
Leaving both fields empty shows this:

testing error message parent

The Name field is required. 

@kristijanhusak
Copy link
Owner

you need to namespace the error messages properly. I'm not managing that at all. so in your child form something like this should work:

<?php namespace App\Forms;

use Kris\LaravelFormBuilder\Form;

class UserDataForm extends Form
{
    protected $clientValidationEnabled = false;
    public function buildForm()
    {
        $model = $this->getModel();
        $model = ($model instanceof \App\Models\User) ? $model : null;

        $this
            ->add('first_name', 'text', [
                'rules' => 'required',
                'label' => trans('user_data.first_name'),
                'error_messages' => [
                    'user_data.first_name.required' => 'testing error message child'
                ],
            ])

@kristijanhusak
Copy link
Owner

Let me know if that works. I'll probably set it up so it collects child form name and append it.

@koichirose
Copy link
Contributor Author

It works by namespacing the error messsages.
I guess that by doing that it wouldn't work if the form is used as a parent and not a child.

Let me know if you manage to add it automatically if it's a child.

Thanks

@kristijanhusak
Copy link
Owner

@koichirose I fixed it in the latest version (1.7.10). Please update and let me know if it works.

@koichirose
Copy link
Contributor Author

It works both for parent and child without namespacing.
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants