themosis / framework

The Themosis framework core.
https://framework.themosis.com/
GNU General Public License v2.0
671 stars 121 forks source link

Failing validation results in empty string #828

Closed reneroboter closed 2 years ago

reneroboter commented 3 years ago

Description

Today I created a settings page with Themosis. After testing the validation rules I noticed that when the validation rules failed that WordPress stored an empty string.

Steps to reproduce

  1. Create a hook class and add the following code block

// Customize WordPress...
use Themosis\Support\Facades\Field;
use Themosis\Support\Facades\Page;
use Themosis\Support\Section;

$page = Page::make('demo-page', 'Demo page')
    ->set();

$page->addSections([
    new Section('general', 'General'),
]);

$page->addSettings([
    'general' => [
        Field::textarea('field_1', [
            'rules' => 'required|min:10|max:120',
        ]),
    ],
]);
  1. Go to the demo page and add the following text "This is a test text block" and save this setting
  2. Change the setting to "This"

The old value "This is a test text block" will be overwritten with an empty string

Expected behavior

In my point of view, when validation rules fail, the old values should not overwrite with an empty string. Instead, the old values should don't touched. Validation should ensure that only valid values get stored. Currently, this solution seems broken.

Here is the "buggy" code line :)

https://github.com/themosis/framework/blob/2.0/src/Page/Page.php#L720

reneroboter commented 3 years ago

Validation is a very common task in web applications. Data entered in forms needs to be validated. Data also needs to be validated before it is written into a database or passed to a web service.

https://symfony.com/doc/current/validation.html