Hello there,
I have a simple :create
action in my controller and everytime I try submitting the form with invalid data it redirects to the :index
route without showing any error_tag
here is my code:
controller
def new(conn, _params) do
changeset = Accounts.change_student(%Student{})
render(conn, "new.html", changeset: changeset)
end
def create(conn, %{"student" => student_params}) do
case Accounts.create_student(student_params) do
{:ok, student} ->
conn
|> put_flash(:info, "Welcome aboard, You Should Recieve A Confirmation Mail Soon!")
|> redirect(to: Routes.student_path(conn, :show, student))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
Changeset:
def changeset(student, attrs) do
student
|> cast(attrs, [:username, :password, :firstName, :lastName, :birthdate, :gender, :email, :uniID])
|> validate_required([:username, :password, :firstName, :lastName, :birthdate, :gender, :email, :uniID])
|> unique_constraint(:username)
|> unique_constraint(:email)
end
Form Templae:
<%= form_for @changeset, @action, fn f -> %>
<%= if @changeset.action do %>
<div class="alert alert-danger">
<p>Oops, something went wrong! Please check the errors below.</p>
</div>
<% end %>
<%= label f, :username %>
<%= text_input f, :username %>
<%= error_tag f, :username %>
<%= label f, :password %>
<%= password_input f, :password %>
<%= error_tag f, :password %>
<%= label f, :firstName %>
<%= text_input f, :firstName %>
<%= error_tag f, :firstName %>
<%= label f, :lastName %>
<%= text_input f, :lastName %>
<%= error_tag f, :lastName %>
<%= label f, :birthdate %>
<%= date_select f, :birthdate %>
<%= error_tag f, :birthdate %>
<%= label f, :gender %>
<%= select f, :gender, ["Male", "Female", "Other", "Rather Not Specify"] %>
<%= error_tag f, :gender %>
<%= label f, :email %>
<%= text_input f, :email %>
<%= error_tag f, :email %>
<%= label f, :uniID %>
<%= text_input f, :uniID %>
<%= error_tag f, :uniID %>
<div>
<%= submit "Submit" %>
</div>
<% end %>
UPDATE: I noticed that it also does not insert when the data is valid so it a problem with the DB I think, can you help me with that?
Thanks in advance!