Form validation without Ecto

Hi all, is there any resource that can point out how one can do form validation for a simple search form? I am asking because it seems most of the examples out there is talking about form validation with Ecto model or changeset

What type of validation would you like to run on it? If it is simple a text input field that you want to have content before submitting you could add required to the input field and the majority of browsers will enforce that for you.

How about logical checks like age must be less than 100?

That specific example you can do via html5 required and such attributes with a regex check.

To do purely logical checks on the client requires javascript.

Neither of these have anything to do with Phoenix since they are clientside.

For Server-side checks, well, just write the code. ^.^

1 Like

Yeah I meant sever side validation. Just thinking is there a standard
practice how it should be done for a form with no backing object ? Most of
the examples I see involve some database model

You can use changesets without schemas and without a database (example below), or if you’d like you could also use an embedded_schema if you just don’t need the db behind the schema.

data  = %{}
types = %{first_name: :string, last_name: :string, email: :string}

changeset =
  {data, types}
  |> Ecto.Changeset.cast(params["sign_up"], Map.keys(types))
  |> validate_required(...)
  |> validate_length(...)

Taken from here: http://blog.plataformatec.com.br/2016/05/ectos-insert_all-and-schemaless-queries/

3 Likes