The code for the demographic form concludes on page 215.
Let’s see it in action.
Any change to a form value causes an error. For example I changed female to male.
[debug] HANDLE EVENT "validate" in PentoWeb.SurveyLive
Component: PentoWeb.DemographicLive.Form
Parameters: %{"_target" => ["demographic", "gender"], "demographic" => %{"gender" => "male", "year_of_birth" => "2023"}}
[error] GenServer #PID<0.3809.0> terminating
** (FunctionClauseError) no function clause matching in PentoWeb.DemographicLive.Form.handle_event/3
(pento 0.1.0) lib/pento_web/live/demographic_live/form.ex:28: PentoWeb.DemographicLive.Form.handle_event("validate", ...)
...
pento/lib/pento_web/live/demographic_live/form.ex
has no function to handle validation events. This handle_event
function and its helper function are available in the book’s downloadable code:
def handle_event("validate", %{"demographic" => demographic_params}, socket) do
params = params_with_user_id(demographic_params, socket)
{:noreply, validate_demographic(socket, params)}
end
defp validate_demographic(socket, demographic_params) do
changeset =
socket.assigns.demographic
|> Survey.change_demographic(demographic_params)
|> Map.put(:action, :validate)
assign_form(socket, changeset)
end
Please include these functions in the book.