KRojH

KRojH

Ecto: How to fix my custom “Out of range” message?

Hi dear friends
I am new in Elixir
I made a table using Migration
also I create a schema the program is in continue

defmodule Houshold.HtmlApi.Device.DeviceSchema do
  use Ecto.Schema
  import Ecto.Changeset

  @primary_key {:id, :binary_id, autogenerate: true}
  @foreign_key_type :binary_id

  schema "devices" do        # name of Schema (devises) is equal
    #with name of table that create using Migration
    #defines the structure of data and maps it to a database (like PostgreSQL، MySQL, ...) table.
    field :name, :string 
    timestamps()
  end
  # validate and transform data before saving it to the database
  def changeset(device, params \\ %{}) do
    device
    |> cast(params, [:name]) # Filters and allows only the fields specified in the list
    |> validate_required([:name], message: "fill all fields") #Ensures certain fields are present and cannot be empty
    |> validate_length(:name, max: 10, message: "out of range ") #to validate the length of a field
    end
end

also I made a query

defmodule Houshold.HtmlApi.Device.DeviceQuery do
   alias Houshold.HtmlApi.Device.DeviceSchema, as: DeviceSchema

   # create a function to add device feuture to created teble in Schema
   # atters is a string like: device =  "mobile"

  def add_device(attrs) do
       case Houshold.Repo.insert %DeviceSchema{name: attrs} do
         {:ok, struct}       -> {:ok, struct}
         {:error, changeset} -> {:error, changeset}
       end
   end
end

there is not error but use validation for length of string should not be more than 10 character (validate_length(:name, max: 10, message: "out of range "))
but it didn’t work even when the name is more than 10 character it print the name but I want to print message: "out of range "

thank you

Most Liked

BartOtten

BartOtten

You are not using the changeset but insert directly in the database. Have a look at the link below to learn how changesets should be used; then you will get your error message.

Last Post!

AstonJ

AstonJ

3 posts were split intoto a new message.

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44532 311
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement