How to validate presence in Phoenix framework?

NoDisplayName

Ecto can validate format, inclusion, uniqueness and so on, but I can't see how I can validate presence? is there a method to add an error to a field if it's empty? Like validates_presence_of in RoR ? I can make it manually, it's not a problem, but I wonder if there is an already existing method for it like validate_presence\3 or something?

Lenin Raj Rajasekaran

Just use the required_fields annotator in the model.

@required_fields ~w(name email)

For a Customer model with a total of 4 fields and 2 required fields like this:

defmodule HelloPhoenix.Customer do
  use HelloPhoenix.Web, :model

  schema "customers" do
    field :name, :string
    field :email, :string
    field :bio, :string
    field :number_of_pets, :integer

    timestamps
  end

  @required_fields ~w(name email)
  @optional_fields ~w()

  def changeset(model, params \\ :empty) do
    model
    |> cast(params, @required_fields, @optional_fields)
  end
end

Phoenix will automatically validate the presence of the required fields and display error messages at the top of the form like below:

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Rails - how to validate presence of attribute AFTER creation

From Dev

Zend framework - how to validate the email?

From Dev

how to test validate presence unless another field is true rails?

From Dev

How to render raw HTML code in Phoenix Framework?

From Dev

How to use SASS/SCSS with Phoenix framework?

From Dev

How to send emails with Phoenix framework

From Dev

How to handle associations and nested forms in Phoenix framework?

From Dev

How to render raw html in Phoenix framework

From Dev

How to selectively disable CSRF check in Phoenix framework

From Dev

How to concatenate two tags in phoenix framework?

From Dev

How to create a global view in Phoenix Framework?

From Dev

Phoenix Framework: How to Route Custom Media Type?

From Dev

How to validate_presence_of with condition in a nested attributes?

From Dev

phoenix framework how to render object as json in a template

From Dev

How to render a JSON file with the Phoenix framework?

From Dev

How to serve static page in phoenix framework?

From Dev

How to validate presence of an array using Enumerize?

From Dev

How to validate a form in play framework

From Dev

Rails - how to validate presence of attribute AFTER creation

From Dev

How to validate presence of parameter in API call

From Dev

Zend framework - how to validate the email?

From Dev

how to test validate presence unless another field is true rails?

From Dev

How to validate presence of at least one field in form in Ruby on Rails

From Dev

How to validate presence of drop down list in Rails

From Dev

How to Use CoffeeScript with Phoenix Framework?

From Dev

How to validate a form in play framework

From Dev

How to validate the presence of attributes when updating a record?

From Dev

How do I use Phoenix Presence to track the existence of topics/subtopics?

From Dev

Connect to Phoenix Socket with Token and Presence

Related Related

  1. 1

    Rails - how to validate presence of attribute AFTER creation

  2. 2

    Zend framework - how to validate the email?

  3. 3

    how to test validate presence unless another field is true rails?

  4. 4

    How to render raw HTML code in Phoenix Framework?

  5. 5

    How to use SASS/SCSS with Phoenix framework?

  6. 6

    How to send emails with Phoenix framework

  7. 7

    How to handle associations and nested forms in Phoenix framework?

  8. 8

    How to render raw html in Phoenix framework

  9. 9

    How to selectively disable CSRF check in Phoenix framework

  10. 10

    How to concatenate two tags in phoenix framework?

  11. 11

    How to create a global view in Phoenix Framework?

  12. 12

    Phoenix Framework: How to Route Custom Media Type?

  13. 13

    How to validate_presence_of with condition in a nested attributes?

  14. 14

    phoenix framework how to render object as json in a template

  15. 15

    How to render a JSON file with the Phoenix framework?

  16. 16

    How to serve static page in phoenix framework?

  17. 17

    How to validate presence of an array using Enumerize?

  18. 18

    How to validate a form in play framework

  19. 19

    Rails - how to validate presence of attribute AFTER creation

  20. 20

    How to validate presence of parameter in API call

  21. 21

    Zend framework - how to validate the email?

  22. 22

    how to test validate presence unless another field is true rails?

  23. 23

    How to validate presence of at least one field in form in Ruby on Rails

  24. 24

    How to validate presence of drop down list in Rails

  25. 25

    How to Use CoffeeScript with Phoenix Framework?

  26. 26

    How to validate a form in play framework

  27. 27

    How to validate the presence of attributes when updating a record?

  28. 28

    How do I use Phoenix Presence to track the existence of topics/subtopics?

  29. 29

    Connect to Phoenix Socket with Token and Presence

HotTag

Archive