-
Notifications
You must be signed in to change notification settings - Fork 14
Cannot customize text for radio_button_fieldset. #82
Comments
…yofjustice#82) When generating a radio-button fieldset, there are times when we want to specify how to get the form value or label text for the choices passed in, the same way `collection_radio_buttons` does in Rails. For example, when assigning a team to a person we may want to use the team's DB ID as the value and the team's name as the radio button text. This change adds these options to `radio_button_fieldset` (adding them to other places, such as `check_box_fieldset`, is an exercise for later).
Opened a PR with a suggested way we could fix this. |
…yofjustice#82) When generating a radio-button fieldset, there are times when we want to specify how to get the form value or label text for the choices passed in, the same way `collection_radio_buttons` does in Rails. For example, when assigning a team to a person we may want to use the team's DB ID as the value and the team's name as the radio button text. This change adds these options to `radio_button_fieldset` (adding them to other places, such as `check_box_fieldset`, is an exercise for later).
Thanks @misaka I'll take a look at the PR. I think a better solution might be to create/re-use I had mocked up a spec describe '#collection_radio_button' do
let(:pretty_output) { HtmlBeautifier.beautify output }
it 'outputs radio buttons wrapped in labels' do
@location = [:ni, :isle_of_man_channel_islands, :british_abroad]
output = builder.collection_radio_buttons :location, @location, :to_s, :to_s
expect_equal output, [
'<div class="form-group">',
'<fieldset>',
'<legend>',
'<span class="form-label-bold">',
'Where do you live?',
'</span>',
'<span class="form-hint">',
'Select from these options because you answered you do not reside in England, Wales, or Scotland',
'</span>',
'</legend>',
'<label class="block-label selection-button-radio" for="person_location_ni">',
'<input type="radio" value="ni" name="person[location]" id="person_location_ni" />',
'Northern Ireland',
'</label>',
'<label class="block-label selection-button-radio" for="person_location_isle_of_man_channel_islands">',
'<input type="radio" value="isle_of_man_channel_islands" name="person[location]" id="person_location_isle_of_man_channel_islands" />',
'Isle of Man or Channel Islands',
'</label>',
'<label class="block-label selection-button-radio" for="person_location_british_abroad">',
'<input type="radio" value="british_abroad" name="person[location]" id="person_location_british_abroad" />',
'I am a British citizen living abroad',
'</label>',
'</fieldset>',
'</div>'
]
end
end And started to create the method def collection_radio_buttons method, collection, value_method, text_method, options = {}, html_options = {}, *args
content_tag :div, class: form_group_classes(method), id: form_group_id(method) do
content_tag :fieldset, fieldset_options(method, options) do
safe_join([
fieldset_legend(method),
radio_inputs(method, options)
], "\n")
end
end
end The only thing that I got stuck on was trying to work out what class the value/text method were to be able to iterate through the collection to generate the radio buttons. |
I took a look at that too, and looking at |
@aliuk2012 I've added a commit that adds TBH I'm not convinced of the value of adding this method. I think that if we're going to add |
<select>
<option value="1"> Option 1</option>
<option value="2"> Option 2</option>
</select> The main idea behind the various form builder methods was to try and implement them as close to the default Rails form builder as possible and to just wrap the govuk html and css classes around the elements. Oh I see what you mean about whether it would add value. Sorry my tests might of needed a bit of extra work. output = builder.collection_radio_buttons :location, @location, :to_s, :to_i
output = builder.collection_radio_buttons :location, @location, :to_s, :to_s
output = builder.collection_radio_buttons :author_id, Author.all, :id, :name_with_initial I do suspect that we would be changing |
@aliuk2012 My point is that it's hard to replicate |
When generating a list of radio buttons for a list of objects, I'd like a way to use a way to use different text and values for each object, e.g. when listing a bunch of teams that may be assigned to a case, I'd like the value to be the team's ID and the text to be the team's name. For example, using Rails'
collection_radio_buttons
method, this would be analogous to:There is already a
collection_select
in this project, but nocollection_radio_buttons
, would that be the right way to implement this? Alternatively, to keep thefieldset
, we could addvalue_method
andtext_method
to theoptions
ofradio_button_fieldset
and default them toto_s
.The text was updated successfully, but these errors were encountered: