Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edit resources with NULL values #304

Closed
telmobarros opened this issue Feb 19, 2020 · 7 comments
Closed

Edit resources with NULL values #304

telmobarros opened this issue Feb 19, 2020 · 7 comments
Labels
enhancement New feature or request normal something which can be done in one day

Comments

@telmobarros
Copy link

I can't edit resources with NULL values that I desire to keep as NULL.

To begin with I receive this Warning in the browser. (From React I suppose)

Warning: value prop on input should not be null. Consider using an empty string to clear the component or undefined for uncontrolled components.

Then if I try to save after changing only one value it just clears the form. If I want to update I have to fill all the input fields, even the ones I desire to keep NULL.

@wojtek-krysiak
Copy link
Contributor

can you paste a schema and please write which adapter you use (mongoose/typeorm/sequelize)?

@telmobarros
Copy link
Author

This model for instance, using sequelize adapter. I am able to create a new Partner leaving website as an empty string and its converted to NULL after inserting in the database. But when I try to edit I am not able to do it at all. I tried forcing the allowNull property to true but I get the same error

'use strict';
module.exports = (sequelize, DataTypes) => {
	const Partner = sequelize.define('Partner', {
		name: {
			allowNull: false,
			type: DataTypes.STRING(50)
		},
		logo: {
			allowNull: false,
			type: DataTypes.STRING
		},
		website: {
			type: DataTypes.STRING
		}
	}, {});
	return Partner;
};

@wojtek-krysiak wojtek-krysiak added enhancement New feature or request small something which can be done in ~4h debug labels Feb 27, 2020
@Mark-J-Lawrence
Copy link

Mark-J-Lawrence commented Apr 6, 2020

@wojtek-krysiak I've seen the #370 fix go in, but it changes all NULLs to be blank string.

I'm planning to have admin-bro working with an existing MSSQL DB via sequelize, and at the moment it will be changing pre-existing records replacing NULLs with ''. I can't afford to allow data changing unexpectedly. Is there a plan to have it handle NULLs or will it continue as its currently implemented? (I can't make any changes via the models I'm using sequelize-auto to generate them on the fly)

UPDATE: I'm getting around this at the moment using the following:

in record-to-form-data.ts

if (value === null) {
      formData.set(key, '__NULL_PLACEHOLDER__')
    } else if (typeof value === 'object' && (value as object).constructor !== File) {
      formData.set(key, '')
    } else {
      formData.set(key, value as string)
    }

and then in edit-action.js I'm adding:

if (request.method === 'post') {
      request.payload = Object
        .entries(request.payload)
        .reduce((memo, [key, value]) => ({
          ...memo,
          [key]: value === '__NULL_PLACEHOLDER__' ? null : value,
        }), {})
    }

@wojtek-krysiak
Copy link
Contributor

@mjlawrence83 wow - this is an actual very good solution. We will have to implement this in the core.

@wojtek-krysiak wojtek-krysiak added normal something which can be done in one day and removed debug small something which can be done in ~4h labels Apr 29, 2020
@MichaelLeeHobbs
Copy link
Contributor

Just sharing the approach I took to resolve this issue.

/* jshint indent: 1 */

const NAME = 'Email'

module.exports = (sequelize, DataTypes) => {
    let schema = {
        templateData: {
            type: DataTypes.JSON,
            allowNull: true,
        },
        from: {
            type: DataTypes.STRING,
            allowNull: false,
            unique: false,
            validate: {
                isEmail: true
            }
        },
        sender: {
            type: DataTypes.STRING,
            allowNull: true,
            unique: false,
            validate: {
                isEmail: true
            }
        },
        replyTo: {
            type: DataTypes.STRING,
            allowNull: true,
            unique: false,
            validate: {
                isEmail: true
            }
        },
        priority: {
            type: DataTypes.ENUM('high', 'normal', 'low'),
            allowNull: false,
            unique: false,
            defaultValue: 'normal'
        },
        to: {
            type: DataTypes.TEXT,
            allowNull: false,
            unique: false
        },
        cc: {
            type: DataTypes.TEXT,
            allowNull: true,
            unique: false
        },
        bcc: {
            type: DataTypes.TEXT,
            allowNull: true,
            unique: false
        },
        subject: {
            type: DataTypes.TEXT,
            allowNull: false,
            unique: false
        },
        body: {
            type: DataTypes.TEXT,
            allowNull: false,
            unique: false
        },
        result: {
            type: DataTypes.JSON,
            allowNull: true,
            unique: false
        },
    }
    Object.keys(schema).forEach(key=>{
        if (schema[key].allowNull) {
            let _set = schema[key].set
            schema[key].set = function (val) {
                if (val === '') val = null
                if (_set && 'function' === typeof _set) _set(val)
                else this.setDataValue(key, val)
            }
        }
    })

    let model = sequelize.define(NAME, schema)
    model.associate = function (models) {
        models[NAME].hasOne(models.EmailTemplate)
    }
    model.adminBroConfig = {
        resource: model,
        // options: {
        //     editProperties: ['name'],
        //     listProperties: ['name'],
        //     properties: {
        //         name: {isTitle: true},
        //     }
        // },
    }
    return model
}

wojtek-krysiak added a commit that referenced this issue Aug 25, 2020
github-actions bot pushed a commit that referenced this issue Aug 26, 2020
# [3.1.0-beta.1](v3.0.1...v3.1.0-beta.1) (2020-08-26)

### Bug Fixes

* propert handle null valuescloses [#304](#304) ([7d16a4e](7d16a4e))

### Features

* allow to clear <select> field ([19e8db9](19e8db9)), closes [#161](#161)
* unify flatten unflatten logic ([b8435de](b8435de)), closes [#352](#352)
@wojtek-krysiak wojtek-krysiak mentioned this issue Aug 26, 2020
github-actions bot pushed a commit that referenced this issue Aug 27, 2020
# [3.1.0](v3.0.1...v3.1.0) (2020-08-27)

### Bug Fixes

* disabled fields are stripped from the payloadrelates to [#430](#430) ([f0b4319](f0b4319))
* make sure old adapters also work ([cb0bd3f](cb0bd3f))
* onChange can also update other fields ([46dacb9](46dacb9))
* propert handle null valuescloses [#304](#304) ([7d16a4e](7d16a4e))

### Features

* allow to clear <select> field ([19e8db9](19e8db9)), closes [#161](#161)
* release ([d21ec77](d21ec77))
* unify flatten unflatten logic ([b8435de](b8435de)), closes [#352](#352)
@wojtek-krysiak
Copy link
Contributor

fixed in 3.1.0

@NoahSaso
Copy link

Is there any documentation about this? I only now know to use __FORM_VALUE_NULL__ to set properties to null in the db because I stumbled upon this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request normal something which can be done in one day
Projects
None yet
Development

No branches or pull requests

5 participants