Empty strings are not registered as a change in the Changeset by Ecto


I have a form and when I enter a value to an input field and then erase it, and then make a change to the form like changing a select input from True to False, the erased value reappears. Did a little bit of digging and it looks like when I erase the value, the value is set to an empty string (“”) and this is not considered as a change in the changeset as shown below,

"channels" => %{
    "0" => %{
      "channel_name" => "test",
      "description" => "",
      "id" => "a58fa456-53f7-4fee-832b-8f4653af22f6"
        action: :update,
        changes: %{},
        errors: [description: {"can't be blank", [validation: :required]}],
        data: #Webappl<>,
        valid?: false

Any idea how to tell Ecto to consider this as a change?

Change the empty_values option. It defaults to [""]. More info: Ecto.Changeset — Ecto v3.9.1

I actually have it as shown below, still the output is the same,

|> cast(attrs, @channel_fields, empty_values: [nil])

Oh sorry I was mistaken. validate_required checks for empty string and doesn’t consider empty_values.

I think your only 2 options are these:

  1. Make the field optional if you allow empty string
  2. Keep "" as an empty_value but put a default value on the field. That way when you cast it, "" will be changed to the default value and validate_required won’t add an error.
  1. I can’t make it optional, because this is actually an embedded schema (embeds_many - atleast one is required). It shouldn’t be optional, but the issue is, if a user erases the value and then changes something else in the form and when the erased value reappears, its not good UX

  2. I want this field to be set to nil and the error msg shown until the user enters some value again, and it should stay blank even when the user is changing other parts of the form.

when I put the default value as nil and empty_values as “”, still it does not get set to nil

Thanks for the inputs. I was able to achieve what I wanted with this method,

[erlang - How to make Ecto.changeset validate_required accept blank values? - Stack Overflow]

1 Like

Out of curiosity, how come you can’t make the description field on your embedded schema optional? That is the one that is causing the error. You could still do what you are saying and have description be optional.

I’m asking because I’m wondering if this should be made easier by Ecto. But struggling a bit to understand why that field couldn’t be optional for you.

So let me explain the context a little bit. I have a select (True or False) if the user selects True, then a nested form is shown to the user where they have to enter some details, “Description” is in this nested form. So if the user selects True, the fields in this nested form can’t be optional, they are required. But if the user selects, False, then these fields should not be required. (Also, there is a + button, where the user can add more nested forms like this)

I wrote a function to set these fields required or not based on the user’s input.