ka215 / cdbt

Custom DataBase Tables plugin is for WordPress
25 stars 8 forks source link

0 value reported as empty on non-null field #9

Open Kitsap-Sun-545 opened 9 years ago

Kitsap-Sun-545 commented 9 years ago

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:

  1. Create a table with a column that is not null, but where the default is null.
  2. 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) {
ka215 commented 9 years ago

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,