MDL-86063 customfield: internally validate numeric data in persistent.

Move previous validation from the data controller, added in 89dbe63d,
into the persistent class itself so that it can internally validate
itself rather than relying on callers.

This resolves problems with empty/null numeric fields contained within
course backups (e.g. during course copy).
This commit is contained in:
Paul Holden
2025-10-19 16:53:17 +01:00
parent 7927f8d058
commit f9f4c49fd2
3 changed files with 37 additions and 30 deletions
+3 -17
View File
@@ -14,21 +14,11 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Customfield component data controller abstract class
*
* @package core_customfield
* @copyright 2018 Toni Barbera <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_customfield;
use backup_nested_element;
use core_customfield\output\field_data;
defined('MOODLE_INTERNAL') || die;
/**
* Base class for custom fields data controllers
*
@@ -38,7 +28,7 @@ defined('MOODLE_INTERNAL') || die;
* Custom field plugins must define a class
* \{pluginname}\data_controller extends \core_customfield\data_controller
*
* @package core_customfield
* @package core_customfield
* @copyright 2018 Toni Barbera <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@@ -200,15 +190,11 @@ abstract class data_controller {
if (!property_exists($datanew, $elementname)) {
return;
}
$datafieldvalue = $value = $datanew->{$elementname};
// For numeric datafields, persistent won't allow empty string, swap for null.
$datafield = $this->datafield();
if ($datafield === 'intvalue' || $datafield === 'decvalue') {
$datafieldvalue = $datafieldvalue === '' ? null : $datafieldvalue;
}
$value = $datanew->{$elementname};
$this->data->set($datafield, $datafieldvalue);
$this->data->set($datafield, $value);
$this->data->set('value', $value);
$this->save();
}