‹ Elliot Jackson

Tags / Elixir

The problem GIFs are great but sometimes it could be nice to have a little bit of dynamism on a site without having a GIF changing in a loop and distracting the user or just without adding the file size of a GIF to the page load. The solution What we’re going to build is a small Phoenix web app that has a single endpoint, /quote.png, that will return an image of a random quote and attribution from an array of user defined quotes.
Written for Phoenix 1.5.7 & Ecto 3.5.5 Ecto changesets provide common validation options but we can also write our own. Before we get into writing our own though, we need to understand how (most of) the default validators work. Let’s have a look at the definition of validate_inclusion for example: def validate_inclusion(changeset, field, data, opts \\ []) do validate_change changeset, field, {:inclusion, data}, fn _, value -> if value in data, do: [], else: [{field, {message(opts, "is invalid"), [validation: :inclusion, enum: data]}}] end end We can see that the actual logic is wrapped inside validate_change/4.
« Older posts Newer posts »