area17 / twill

Twill is an open source CMS toolkit for Laravel that helps developers rapidly create a custom admin console that is intuitive, powerful and flexible. Chat with us on Discord at https://discord.gg/cnWk7EFv8R.
https://twillcms.com
Apache License 2.0
3.72k stars 568 forks source link

Form fields error messages are not flashed when validating (again) #2632

Closed kfina-planxy closed 2 months ago

kfina-planxy commented 2 months ago

Description

Same as #697 and #605. When using the FormBuilder with Request Rules no error messages are shown

// MyModelController.php
    public function getForm(TwillModelContract $model): Form
    {
        $form = parent::getForm($model);

        $form->add( Input::make()->name('title')->label('Title')->translatable() );
        $form->add( Input::make()->name('item number')->label('Item Number') );

        [...]

        return $form;
    }
// MyModelRequest.php
    public function rulesForUpdate()
    {
        return [
            'item_number' => 'required',
            'title' => Rule::unique('my_model_translations', 'title')
        ];
    }

Steps to reproduce

  1. Enter code as above
  2. Try to submit something that break Network Tab shows correct messages: (manually translated by myself to english, because the messages were correctly localized)
    {
    "item_number": [
        "The field \"item number\" is required"
    ],
    "title": [
        "That title is already used."
    ]
    }

Expected result

Red outlined fields and a message telling what's wrong ( See screenshots in #697 )

Actual result

Only a red notification saying: "Your submission could not be validated, please fix and retry"...

Versions

ifox commented 2 months ago

Hi @kfina-planxy you are not using the form request class correctly to validate translated field. You need to use $this->rulesForTranslatedFields([<non translated field rules>], [<translated field rules>])

kfina-planxy commented 2 months ago

Thank you! Got it to work. I still wonder why the item_number message did not work initially though.