0 value reported as empty on non-null field
Opened this issue · 1 comments
Greetings, I'm using v1.10
If a field is required (since it's a not null column) and a user enters 0 for that field, an error is thrown during validation stating that the value is empty.
Steps to reproduce:
- Create a table with a column that is not null, but where the default is null.
- Switch to input mode and try to add a record where that value is 0.
Additional background:
The problem appears to be in the validate_data
method of CustomDatabaseTables
class in lib/cdbt.class.php
function validate_data($column_schema, $data) {
if ($column_schema['not_null'] && $column_schema['default'] == null) {
if (empty($data))
return array(false, __('empty', self::DOMAIN));
}
if (!empty($data)) {
The empty
function returns true when $data evaluates as false. See http://php.net/manual/en/function.empty.php for more information.
This means that empty($data)
will return true
when $data
is a string containing the number 0. In my installation I've changed the validate function to explicitly check for empty string or null which then allows me to set 0 values on input or edit.
function validate_data($column_schema, $data) {
if ($column_schema['not_null'] && $column_schema['default'] == null) {
if ($data == '' || $data == NULL)
return array(false, __('empty', self::DOMAIN));
}
if ($data != '' && $data != NULL) {
Sorry for my late reply.
And, thank you very much for reporting.
I tried to verify the process that inputting data to table by the plugin.
And, as you say, I also found a few bugs in the data verification part.
I will fix the bugs of data verification process. Then, I am going to try to release in the next version up.
Thank you,