Created
May 15, 2018 21:10
-
-
Save adavia/ca1de5d5624ff60294a17930ddedad27 to your computer and use it in GitHub Desktop.
Changeset cond validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def changeset(%User{} = user, attrs) do | |
| # Conditional req validation | |
| required_fields = if attrs[:provider] do | |
| [:username, :email] | |
| else | |
| [:username, :email, :password] | |
| end | |
| user | |
| |> cast(attrs, @required_fields, @optional_fields) | |
| |> validate_required(required_fields) | |
| |> validate_format(:email, ~r/@/) | |
| |> validate_length(:password, min: 6) | |
| |> validate_confirmation(:password) | |
| |> unique_constraint(:username) | |
| |> unique_constraint(:email) | |
| |> downcase_email | |
| |> encrypt_password | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment