Saving Text / Varchar fields as a blank / empty string is not allowed when the field is set to not null
Validation stops me from saving until I put in a value
An empty text box should be saved as an empty string.
- Project name: Content(POC)
- Team name: Operations
- Environment name: Prod
- Agent (forest package) name & version: latest
- Database type: MSSQL
- Recent changes made on your end if any: N/A
Currently a NOT NULL field in the DB is required in Forest Admin, and the front automatically convert empty strings to null values.
It appears to us a bad design to allow empty strings in required fields, so it is not possible to do it easily in Forest Admin.
If you cannot/doesn’t want to do it otherwise, there is some workarounds :
- You can hide the real NOT NULL field, and create a computed field that is not required. Forest allow to define the write method of the computed field, to make it write in the real DB field. The front will still send null values but you can replace them with empty string in this function.
- collection.removeField to hide the real field in forest admin
- collection.addField to create the new not required field
- collection.replaceFieldWriting to define the writing method of the new field
- Simpler, but you need to change your DB:
Remove the NOT NULL constraint, and replace the writing method of the field with collection.replaceFieldWriting to set an empty string when the frontend is sending null.
Empty string is a valid value. I think what you are talking about is VARCHAR vs CHAR
A CHAR field has a required length at all times.
A VARCHAR is a variable length string from 0 to the length of the field.
Maybe modify the text control to honour the type CHAR vs. VARCHAR and a max length on the text box.
I decide to turn off Not Null for now. One of the big pluses for using ForestAdmin is zero code. So at this point I wont try your work around, but thanks for sharing.
Hello @David_Martin, I’m sorry for the delay,
Updating the text field to allow null values is not a feature that we plan to do as it hasn’t been requested by other users.
Forest Admin widgets are built to ignore the underlying database type.
A max length can be enforced through field validation.