Input form type is different than attribute in model

Hi, i am new using Phoenix framework!
I created a LiveView using phoenix generator. I have 6 attributes in the model that each one is an array of integers and the input form should be with a specific pattern (integers separated by “,”)
Example
For one of these attribute
Input format: “1,5,6,4”
And in the model is an array of integers [1,5,6,4]

What is the best strategy to face this? Which implications in coding has each one?
I was thinking in:
1- Parse the data in controllers, like trying to parse from string to array in the request forms, and when i need to response parse from array to string.
2- Maybe using new attributes (each one corresponds to the original attribute) in a model that will not saved in database, at the end it just fill the correspondent

There are a lot of possible approaches to this problem, what I would do is:

  1. Use a regex pattern to validate the input. This can be either done with the help of ecto, or you can make it separated in the controller, your choice;
  2. Parse, cast and validate your ecto changeset once the input is valid.

I would use Ecto.Changeset for this, you can easily make your own cast function that takes the comma-separated values and split + parse them.

Hey, Thank you for your response!!!
My question was more in deep when i asked in which implications in coding has each one.

1- Yes, the validation it’s not a problem, there are different ways and that wasn’t my question. Sorry if i didn’t explain well.

2- How do you face the different used functions in liveview?
For example: when the action is a new and you fill the input: it calls a validation in the controller (remember that i said that i created a liveview with the phoenix generator). In this handle validation function, i can parse the param, and it’s easy (a function that just split the string), then create the changeset with the params (some of them already parsed) and validate the information, but then some issues appears:

  • Ecto changeset has the “data” and “changes”. So, when is new, and there is no errors, the information of the field is in “change”, so for the response (using the known function -to_form(changeset)-) i need to parse the field from array to string, or just update the field received in the param into the correspondent field in “changes”. Here my first stop… is this the best way to this?

What happen when you want to edit a created one? Because in the Changeset there is no “changes” here, and is taken from the data (that it’s the Ecto Model) and you cant just parse the data there, right? and it sounds like doing some hard-code haha.

That is why i am trying to identify the best approach to deal with this.

My second option that i mentioned in the post is related to use a virtual field.

1 Like

You can go 2 ways:

  1. Create a new schema that will be used purely for form validation, as you mentioned above.
  2. Create a new ecto type and write your own validator for cast, seems to be the perfect use-case here for you.