framework icon indicating copy to clipboard operation
framework copied to clipboard

Field::text - 'flush' => false

Open boxybird opened this issue 5 years ago • 3 comments

  • Themosis Version: 2.0.4
  • WordPress Version: 5.2.3
  • PHP Version: 7.2.16

Description

When using the text field for a metabox like...

...
Field::text('author', [
    'flush' => false,
    'rules' => 'required|min:10'
])
->set();

... and the validation fails, the existing text is cleared.

My understanding when reading the docs, is setting 'flush' => false should prevent this.

What am I missing?

Expected behavior

Old, invalid text should remain so user can modify and correct the error.

boxybird avatar Oct 02 '19 20:10 boxybird

Normally the flush option, the value is kept only if it's valid. I mean, if another input was not valid, your current field should keep the inserted value.

So in the case where your text field is not valid, even with the flush option, the input is cleared and display an error message below the field.

jlambe avatar Oct 08 '19 14:10 jlambe

Your logic is sound, but in practice it feels a bit wrong.

Let's say I have a section:

...
->add(new Section('ideabox', 'Idea Box', [
    Field::text('idea_title'),
    Field::textarea('idea_body', [
        'rules' => 'required|min:250'
    ])
]))

If the user hits the save button, the Field::text('idea_title') will be stored and (assuming the user typed less than 250 characters) the Field::textarea('idea_body') will be cleared and an error message will be shown.

It just feels like bad UX to make the user retype an entire 250 characters if they were just a few characters short.

boxybird avatar Oct 08 '19 15:10 boxybird

Indeed, in this case it makes sense to keep the initial value then. But currently, there is technical change to take into account is that when used as a custom field (post meta), the current mechanism would need to store the "invalid" data nonetheless into the database. Somehow, we need to make it return the invalid data in the ajax response based on the flush option.

jlambe avatar Oct 08 '19 15:10 jlambe