Hey Folks,
I’ve got a marketing site that has a contact us form to reach out for potential requests of work/services.
The website has Ecto
dependencies enabled and an empty PostgreSQL instance attached. However, the first iteration of the marketing site is mostly static content and therefore no Ecto
models or an overarching schema have been created yet.
Because little thought has been put into the long term schema, for the sake of simplicity and reduction of future issues, I created a form from an atom
instead of a @changeset
.
<section class="submit-service-request-page">
<%= service_request_form = form_for :service_request, "#", [phx_change: :validate, phx_submit: :submit] %>
<label>
All fields required <span class="light-red-text">*</span>
</label>
<%= text_input service_request_form, :preferred_name, placeholder: "Preferred Name" %>
<%= error_tag service_request_form, :preferred_name %>
<%= email_input service_request_form, :email, placeholder: "Your Email" %>
<%= error_tag service_request_form, :email %>
<%= select service_request_form, :choose_service, @service_options, selected: [1] %>
<%= error_tag service_request_form, :choose_service %>
<%= textarea service_request_form, :brief_description, placeholder: "Describe the Job" %>
<%= error_tag service_request_form, :brief_description %>
<div class="text-align-center">
<%= submit "Send Request" %>
</div>
</form>
</section>
In the live_view
I handle the validation event as such:
def handle_event("validate", %{"service_request" => validate_params}, socket) do
data = %{}
types = %{brief_description: :string, choose_service: :string, email: :string, preferred_name: :string}
service_request =
{data, types}
|> cast(validate_params, Map.keys(types))
|> validate_required([:brief_description, :choose_service, :email, :preferred_name])
|> validate_format(:email, ~r/@/)
|> validate_length(:brief_description, min: 10)
{:noreply, assign(socket, changeset: service_request)}
end
I know that the Ecto changeset
is being cast properly and that the validations are working as when I log them I get the following:
Ecto.Changeset<action: nil, changes: %{brief_description: "asfa"},
errors: [brief_description: {"should be at least %{count} character(s)", [count: 10, validation: :length, kind: :min, type: :string]},
choose_service: {"can't be blank", [validation: :required]},
email: {"can't be blank", [validation: :required]},
preferred_name: {"can't be blank", [validation: :required]}], data: %{}, valid?: false>
However, my forms UI doesn’t automatically update like it would if it were connected to an Ecto model/schema
. How do I get the updated @changeset
to my service request form? I feel like I’m missing something obvious here. I’ve looked at the following post on these forums and the linked blog, but it didn’t help with this specific issue.
Thanks for the help,
Scott