-
Notifications
You must be signed in to change notification settings - Fork 296
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
Comments
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. |
Thank you. As always, let me know if you need help with testing it. |
@koichirose I added this in the latest release (1.7.0). You can update and start using it. |
@kristijanhusak it doesn't work for child forms. <?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.
|
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'
],
]) |
Let me know if that works. I'll probably set it up so it collects child form name and append it. |
It works by namespacing the error messsages. Let me know if you manage to add it automatically if it's a child. Thanks |
@koichirose I fixed it in the latest version (1.7.10). Please update and let me know if it works. |
It works both for parent and child without namespacing. |
I have the following in my buildForm() function:
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:
The text was updated successfully, but these errors were encountered: