formex_ecto v0.2.3 Formex.Ecto.ChangesetValidator View Source

Changeset validator adapter for Formex.

It was created to make use of validation functions included in Ecto.Changeset. This module creates a fake changeset to perform validation rules.

You don’t need to use this validator - any validator works with Ecto schemas. You can also add errors in the c:Formex.Ecto.modify_changeset/2 callback, which modifies real changeset.

Limitations

  • can be used only with Ecto schemas.
  • length validation for collections doesn’t work. Maybe there is a way to fix it. If you need this now - use Vex validator instead.

Installation

See Formex.Validator docs.

Usage

defmodule App.UserType do
  use Formex.Type
  use Formex.Ecto.Type
  use Formex.Ecto.ChangesetValidator # <- add this
def build_form(form) do
  form
  |> add(:username, :text_input, validation: [
    :required
  ])
  |> add(:email, :text_input, validation: [
    required: [message: "give me your email!"],
    format: [arg: ~r/@/]
  ])
  |> add(:age, :text_input, validation: [
    :required,
    inclusion: [arg: 13..100, message: "you must be 13."]
  ])
end

Keys from validation list are converted to validate_ functions from Ecto.Changeset. For example required -> Ecto.Changeset.validate_required/3.

Value is list of options. If function requires additional argument (e.g. Ecto.Changeset.validate_format/4 needs format as third argument) it must be passed as :arg option.

Link to this section Summary

Link to this section Functions