Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No validation error when submitting a required select component with an empty option #329

Open
rbarreca opened this issue Oct 8, 2023 · 2 comments

Comments

@rbarreca
Copy link
Contributor

rbarreca commented Oct 8, 2023

It's probably just user error, but I can't figure out how to make a required select component with an empty option. It doesn't trigger any Polaris validation, rather just gets to the Rails controller and errors out with undefined method map' for nil:NilClass`.

I've tried adding [''] or [['', '']] or [['', nil]] to @options_from_controller, but none of them do the trick. What am I doing wrong?

<%= polaris_select(name: 'vendor_product_setting[foo_id]', 
              label: "Infoplus vendor",
              options: @options_from_controller, 
              required: true, selected: @vendor_product_setting.foo_id) %>

The example for required doesn't have an empty option which makes it not useful.

@k0va1
Copy link
Contributor

k0va1 commented Oct 8, 2023

Hi @rbarreca! Basically, polaris_select uses Rails select under the hood, so you can pass either input_options: { include_blank: true } or input_options: { prompt: "Select..." } to achieve the desired behavior

@rbarreca
Copy link
Contributor Author

Thanks @k0va1 that sent me in the right direction, although it's select_options: { include_blank: true }. I opened #332 to improve the example (although I'm not sure if there is another spot I need to update for the docs).

Now I have a blank, but I'm expecting there to be a validation error message when submitting since it's required, but I don't have one. Do you have any advice?

View

<%= polaris_page(
  title: @vendor_product_setting.persisted? ? "Edit vendor product setting" : "Add vendor product setting",
  back_url: vendor_product_settings_path
) do %>
  <%= polaris_card do %>
    <%= form_with(model: @vendor_product_setting, builder: Polaris::FormBuilder) do |form| %>
      <%= polaris_form_layout do |form_layout| %>
        <% form_layout.with_item do %>
          <%= form.polaris_select(:infoplus_vendor_id, 
              label: "Infoplus vendor",
              options: @infoplus_vendor_options, 
              required: true, 
              select_options: { include_blank: true }
            ) %>
        <% end %>
        <% form_layout.with_item do %>
          <%= form.polaris_text_field(:sku, label: "SKU", 
                help_text: "Leave empty to set the default incoming percentage for this vendor.") %>
        <% end %>
        <% form_layout.with_item do %>
          <%= form.polaris_text_field(:incoming_percent, label: "Percent incoming to list", 
                type: :number, min: 0, max: 100) %>
        <% end %>
        <% form_layout.with_item do %>
          <%= polaris_button(submit: true, primary: true) { "Submit" } %>
        <% end %>
      <% end %>
    <% end %>
  <% end %>
<% end %>

Controller

class VendorProductSettingsController < AuthenticatedController
  before_action :set_infoplus_vendor_options, only: [:edit, :new, :create, :update]

  def index
    @vendor_product_settings = VendorProductSetting.all
  end

  def edit
    @vendor_product_setting = VendorProductSetting.find(params[:id])
  end

  def new
    @vendor_product_setting = VendorProductSetting.new
  end

  def create
    @vendor_product_setting = VendorProductSetting.new(vendor_product_setting_params)

    if @vendor_product_setting.save
      redirect_to vendor_product_settings_path
    else
      render :new, status: :unprocessable_entity
    end
  end

  def update
    @vendor_product_setting = VendorProductSetting.find(params[:id])

    if @vendor_product_setting.update(vendor_product_setting_params)
      turbo_redirect_to vendor_product_settings_path, notice: 'Vendor product setting was successfully updated.'
    else
      render :edit, status: :unprocessable_entity
    end
  end

  private

  def vendor_product_setting_params
    params.require(:vendor_product_setting).permit(:infoplus_vendor_id, :sku, :incoming_percent)
  end

  def set_infoplus_vendor_options
    @infoplus_vendor_options = InfoplusVendor.all.map do |v|
      [v.name, v.id]
    end
  end
end

@rbarreca rbarreca changed the title How to create a required select component with an empty option? No validation error when submitting a required select component with an empty option Nov 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants