smpallen99 / ex_admin

ExAdmin is an auto administration package for Elixir and the Phoenix Framework
MIT License
1.2k stars 272 forks source link

Required fields for forms not taking into account custom Changesets #443

Open benrom opened 6 years ago

benrom commented 6 years ago

I have registered two changesets for a user account: one for creation with a required :password field, and one for update where the virtual :password field is not required (if no new password is set, I just dismiss this field and just update the other fields).

In the resulting form, the :password field is always set as required when editing an existing account, even though its associated changeset does not list it in validate_required().

If I change the default changeset method to remove the required field, I can see my form not showing password as required. Though it breaks the creation form where I wanted it to be required.

After a quick search through the code, the issue could lie around here:

required_list =
      cond do
        Map.get(res, :__struct__, false) ->
          res.__struct__.changeset(res).required

        true ->
          []
      end

It should use instead the custom changeset functions if they were defined, using changeset() only as a fallback.