Prompt for select not shown, but it is listed

Hi,
I have a few selects with the same problem for prompt (Hanami 1.3).

The code in the template is as follows: select :client_id, clients_for_select, options: {prompt: (I18n.t :gm_contact_new_prompt_select_client)}.

The problem is that the prompt is not displayed as the “prompt”. Rather the first value in client list is shown as selected (see image below).

The values for select are an ordinary hash.

slika

Is any additional setting required?

Best, seba

@sebastjan_hribar Sorry for the problem.

Can you please dump here the value of your clients_for_select or double check your usage against the tests?

Hi @jodosha,

here is the clients_for_select as prepared in the action: {"Huda Firma"=>1, "ACME"=>2}.

I have another select with the same setup and the same problem. The prompt is disabled, which is in accordance with the sample code, but it is not shown to the user. Here is the html output:

<select name="qareport[error_category_id]" id="qareport-error-category-id" class="tmsr-form-input">
<option disabled="disabled">Select error category</option>
<option value="1">NP: Napačen prevod stavka.</option>
<option value="2">NT: Napačen prevod termina.</option>
</select>

Is this the correct output?

I’ve also tried removing all styling and the problem remains.

I have another follow-up question. Looking at the docs (Class: Hanami::Helpers::FormHelper::FormBuilder — Documentation for hanami-helpers (1.3.3)), specifically for Prompt option and Prompt option and HTML attributes I see a difference in the output.

With Prompt option:

<option>Select a store</option>

And with Prompt option and HTML attributes:

<option disabled="disabled">Select a store</option>

However, in my new and almost empty test app I have this in the form:

<h2>Add book</h2>

<%=
  form_for :book, '/books' do
    div class: 'input' do
      label      :title
      text_field :title
    end

    div class: 'input' do
      label      :author
      text_field :author
    end

    div class: 'select' do
        select :store, values, options: { prompt: 'Select a store' }
    end
    
    div class: 'controls' do
      submit 'Create Book'
    end
  end
%>

The action:

expose :values
def call(params)
    @values = Hash['Ljubljana' => 'LJ', 'Maribor' => 'MB']
end

This is the page inspection code:

<div class="select">
<select name="book[store]" id="book-store">
<option disabled="disabled">Select a store</option>
<option value="LJ">Ljubljana</option>
<option value="MB">Maribor</option>
</select>
</div>

At this point I have nothing in the DB and also nothing specified as selected so it should display the prompt. In terms of functionality of course and not the generated output. Right?

What I get is this:

image

image

What I would need is to have the prompt displayed before clicking the select control.

So far I’ve been using an empty value or nil as the first option with the default text. But when I have an optional ID which may get selected my validation fail when such empty value or nil gets passed and is checked against this: optional(:error_category_id).maybe(:int?). Is there any way to do this another way, if the helper prompt would not be possible?