Files
ubicloud/views/components/form/select.erb
Jeremy Evans 23601a07a3 Use correct params typecasting when handling validation failures
Previously, this would assume a given type, and likely raise
NoMethodError if the type was not what was expected. If there
is a typecast error when rendering the validation failure
template, display a simple error page.  This should only happen
if a user monkeys around with a form parameter before submitting
the request.

This uses a new feature in the Roda typecast_params plugin, so
update to the latest Roda commit.
2025-08-08 01:52:14 +09:00

47 lines
1.8 KiB
Plaintext

<%# locals: (name: nil, id: name, classes: nil, label: nil, options: {}, selected: nil, placeholder: nil, error: nil, description_html: nil, attributes: {}) %>
<% selected = flash.dig("old", name) || typecast_body_params.str(name) || selected if name
error ||= flash.dig("errors", name) %>
<div class="space-y-2 text-gray-900">
<% if label %>
<label for="<%= name %>" class="text-sm font-medium leading-6"><%= label %></label>
<% end %>
<select
id="<%= id %>"
name="<%= name %>"
class="<%= classes %> block w-full rounded-md border-0 py-1.5 pl-3 pr-10 shadow-sm ring-1 ring-inset focus:ring-2 focus:ring-inset text-sm sm:leading-6 <%= error ? "text-red-900 ring-red-300 placeholder:text-red-300 focus:ring-red-500" : "text-gray-900 ring-gray-300 placeholder:text-gray-400 focus:ring-orange-600"%>"
<%== html_attrs(attributes) %>
>
<% if placeholder %>
<option class="always-visible" value>
<%= placeholder %>
</option>
<% end %>
<% options = {nil => options} unless options.is_a?(Hash) %>
<% options.each do |group_name, opts| %>
<% next if opts.empty? %>
<% if group_name %>
<% opt_group_attributes = group_name.is_a?(Hash) ? group_name : {"label" => group_name} %>
<optgroup <%== html_attrs(opt_group_attributes) %>>
<% end %>
<% opts.each do |opt_val, opt_text, opt_classes, opt_attrs| %>
<option
value="<%= opt_val %>"
class="<%= opt_classes %>"
<%= (opt_val == selected) ? "selected" : "" %>
<%== html_attrs(opt_attrs || {}) %>
>
<%= opt_text %>
</option>
<% end %>
<% if group_name %>
</optgroup>
<% end %>
<% end %>
</select>
<% if error %>
<p class="text-sm text-red-600 leading-6" id="<%= name %>-error"><%= error %></p>
<% end %>
<%== description_html %>
</div>