laravel-form-builder
laravel-form-builder copied to clipboard
Old value in empty field submission
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?
For which field type that's happening?
It's happening with text field.
same here
Newest version? How do you make the form? Is it a named form?
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.
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.
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.
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.
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?