Manukam

Manukam

Hi,

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"
    }
  }
 #Ecto.Changeset<
        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?

Showing Posts 1 to 7

joey_the_snake

joey_the_snake

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

Manukam

Manukam OP

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

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

joey_the_snake

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.
Manukam

Manukam OP

  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

Manukam

Manukam OP

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

[https://stackoverflow.com/questions/45754213/how-to-make-ecto-changeset-validate-required-accept-blank-values]

joey_the_snake

joey_the_snake

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.

Manukam

Manukam OP

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
aseigo
ICal is a library for interacting with iCalendar data. It parses iCalendars into typed Elixir structs via ICal.from_ics, and can prepare ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews