laravel-form-builder icon indicating copy to clipboard operation
laravel-form-builder copied to clipboard

Old value in empty field submission

Open fabiosegalla opened this issue 7 years ago • 9 comments

If I submit the empty field name and form validation fails, after redirect()->back()->withInput() I see the form with the original value of my field name. This behaviour is different from the result of function Input::old("name", $myObject->name) that mantain the submitted value empty. What do you think about it?

fabiosegalla avatar Mar 03 '17 16:03 fabiosegalla

For which field type that's happening?

kristijanhusak avatar Mar 05 '17 17:03 kristijanhusak

It's happening with text field.

fabiosegalla avatar Mar 07 '17 08:03 fabiosegalla

same here

kitkimwong avatar Jun 19 '19 01:06 kitkimwong

Newest version? How do you make the form? Is it a named form?

rudiedirkx avatar Jun 21 '19 17:06 rudiedirkx

Hi, I have a similar problem in my fork and would like to try some debugging. However, I have trouble pinpointing where supplying old input values really takes place. Does the code depend on Collective\Html to retrieve old input in the rendering stage? I would not expect that, however it seems so.

kmbt avatar Nov 20 '19 09:11 kmbt

I seem to have managed to solve the problem in my case. In my app the form was used for updating an array stored in database, so i am using model attribute for instantiating the form.

The problem arises when the app returns from validation error with old input. If the old input for a certain field is null and the field is present in the model then the value from the model takes precedence. I fixed the problem in the controller by detecting if there is any old input in request()->old() and providing empty model if there was. I consider this a little hack and I guess the problem could be properly solved inside the Form Builder.

Or perhaps there already is a solution in a newest version? Honestly, I haven't looked into that.

kmbt avatar Nov 20 '19 09:11 kmbt

I think the problem is Laravel's relatively new ConvertEmptyStringsToNull middleware. Form input can't be null. All input is always strings. But with this middleware input can be null, which messes up the old-logic. I never enable that middleware, because forms and models can handle null. No need to change the input source.

rudiedirkx avatar Nov 20 '19 09:11 rudiedirkx

I think you are right. Disabling ConvertEmptyStringsToNull also works around this particular issue in my case. I think the problem could be taken care of either by providing information on ConvertEmptyStringsToNull in the documentation (maybe near the description of model parameter) or by adding an option in a fashion similar to prefer_input that would cause model override in case old input is present.

kmbt avatar Nov 20 '19 10:11 kmbt

But old input isn't present if ConvertEmptyStringsToNull made it null. I don't see how they could work together. I don't like middleware that changes input anyway, so I would never enable it, and I won't debug/fix/improve it. A note in the docs is a good idea. Suggestion? PR?

rudiedirkx avatar Nov 21 '19 13:11 rudiedirkx