How to set `label_text` for optional fields?

67 views
Skip to first unread message

Adrien

unread,
Jul 7, 2015, 9:13:35 AM7/7/15
to plataformate...@googlegroups.com
Hey!

I've been looking for a way to put some labels for optional fields. I've spotted how to set and build the label text for required text but I found nothing for optional fields.

Any hints?

Carlos Antonio da Silva

unread,
Jul 7, 2015, 9:35:06 AM7/7/15
to plataformate...@googlegroups.com
You can set the `label_text` option in your configuration, it receives a `required` argument that you can conditionally add a required or optional text to the label, for instance:

  lambda { |label, required, explicit_label| required.present? ? "#{required} #{label}" : "(optional) #{label}" }

Hope that helps.

--
You received this message because you are subscribed to the Google Groups "SimpleForm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to plataformatec-simp...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
At.
Carlos Antonio

Jeroen Visser

unread,
Apr 11, 2016, 5:50:34 AM4/11/16
to SimpleForm
Hi Carlos,

I've got a very similar question, but in my case I would like it to be in a separate element. Would you recon I'd do that in the same fashion, in combination with #html_safe, or is there possibly a better solution?


On Tuesday, July 7, 2015 at 3:35:06 PM UTC+2, Carlos Antonio da Silva wrote:
You can set the `label_text` option in your configuration, it receives a `required` argument that you can conditionally add a required or optional text to the label, for instance:

  lambda { |label, required, explicit_label| required.present? ? "#{required} #{label}" : "(optional) #{label}" }

Hope that helps.
On Tue, Jul 7, 2015 at 10:13 AM, Adrien <adrien....@gmail.com> wrote:
Hey!

I've been looking for a way to put some labels for optional fields. I've spotted how to set and build the label text for required text but I found nothing for optional fields.

Any hints?

--
You received this message because you are subscribed to the Google Groups "SimpleForm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to plataformatec-simpleform+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
At.
Carlos Antonio

Carlos Antonio da Silva

unread,
Apr 11, 2016, 6:41:13 AM4/11/16
to plataformate...@googlegroups.com
If you're thinking about adding html to the label_text option so that you can tweak the required output, you may be able to use the html: option available through the locale instead. You can change the entire html for the required flag in there, with a different html as you need.

If you need html over your optional flag like the previous option, then I think the html locale option might not work for you, and in this case it's probably ok to use it in the label_text (since you have total control over the output).

On Mon, Apr 11, 2016 at 6:50 AM, Jeroen Visser <m...@jrnv.nl> wrote:
Hi Carlos,

I've got a very similar question, but in my case I would like it to be in a separate element. Would you recon I'd do that in the same fashion, in combination with #html_safe, or is there possibly a better solution?

On Tuesday, July 7, 2015 at 3:35:06 PM UTC+2, Carlos Antonio da Silva wrote:
You can set the `label_text` option in your configuration, it receives a `required` argument that you can conditionally add a required or optional text to the label, for instance:

  lambda { |label, required, explicit_label| required.present? ? "#{required} #{label}" : "(optional) #{label}" }

Hope that helps.
On Tue, Jul 7, 2015 at 10:13 AM, Adrien <adrien....@gmail.com> wrote:
Hey!

I've been looking for a way to put some labels for optional fields. I've spotted how to set and build the label text for required text but I found nothing for optional fields.

Any hints?

--
You received this message because you are subscribed to the Google Groups "SimpleForm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to plataformatec-simp...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
At.
Carlos Antonio

--
You received this message because you are subscribed to the Google Groups "SimpleForm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to plataformatec-simp...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
At.
Carlos Antonio

Jeroen Visser

unread,
Apr 11, 2016, 7:29:47 AM4/11/16
to SimpleForm
Thanks for your quick response – I'm currently using the label_text and the output is something like this:

<label>
  [actual label]
  <div ...>(optional)</div>
</label>

I would actually want the output to be more like:

<label>...</label>
<div ...>(optional)</div>

...since it's being wrapped in a div. Would that be possible through the html option and if so, where do I configure that? On the label itself?

On Monday, April 11, 2016 at 12:41:13 PM UTC+2, Carlos Antonio da Silva wrote:
If you're thinking about adding html to the label_text option so that you can tweak the required output, you may be able to use the html: option available through the locale instead. You can change the entire html for the required flag in there, with a different html as you need.

If you need html over your optional flag like the previous option, then I think the html locale option might not work for you, and in this case it's probably ok to use it in the label_text (since you have total control over the output).
On Mon, Apr 11, 2016 at 6:50 AM, Jeroen Visser <m...@jrnv.nl> wrote:
Hi Carlos,

I've got a very similar question, but in my case I would like it to be in a separate element. Would you recon I'd do that in the same fashion, in combination with #html_safe, or is there possibly a better solution?

On Tuesday, July 7, 2015 at 3:35:06 PM UTC+2, Carlos Antonio da Silva wrote:
You can set the `label_text` option in your configuration, it receives a `required` argument that you can conditionally add a required or optional text to the label, for instance:

  lambda { |label, required, explicit_label| required.present? ? "#{required} #{label}" : "(optional) #{label}" }

Hope that helps.
On Tue, Jul 7, 2015 at 10:13 AM, Adrien <adrien....@gmail.com> wrote:
Hey!

I've been looking for a way to put some labels for optional fields. I've spotted how to set and build the label text for required text but I found nothing for optional fields.

Any hints?

--
You received this message because you are subscribed to the Google Groups "SimpleForm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to plataformatec-simpleform+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
At.
Carlos Antonio

--
You received this message because you are subscribed to the Google Groups "SimpleForm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to plataformatec-simpleform+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
At.
Carlos Antonio

Jeroen Visser

unread,
Apr 11, 2016, 7:59:31 AM4/11/16
to SimpleForm
Or should I consider doing it by overriding the label form helper like this:

Carlos Antonio da Silva

unread,
Apr 11, 2016, 9:55:27 AM4/11/16
to plataformate...@googlegroups.com
By default the required text/mark is part of the label, so there's no way to put it outside of the label tag. I'd perhaps control it with a span tag and a specific class, and using css to make it display block and break the line as it seems you're expecting to. Keep in mind that having it inside the label usually means it's part of the clickable label area that focus on the input as well.

You might be able to achieve this by overriding (read monkey patching) the label component in Simple Form: https://github.com/plataformatec/simple_form/blob/master/lib/simple_form/components/labels.rb#L24-L37.

Hope that helps.

Hi Carlos,

To unsubscribe from this group and stop receiving emails from it, send an email to plataformatec-simp...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
At.
Carlos Antonio

--
You received this message because you are subscribed to the Google Groups "SimpleForm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to plataformatec-simp...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
At.
Carlos Antonio

--
You received this message because you are subscribed to the Google Groups "SimpleForm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to plataformatec-simp...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
At.
Carlos Antonio

Jeroen Visser

unread,
Apr 11, 2016, 10:41:35 AM4/11/16
to plataformate...@googlegroups.com
That helps for sure, I had the thing you suggested at first and will consider money patching. Thanks a lot, appreciate your help!

You received this message because you are subscribed to a topic in the Google Groups "SimpleForm" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/plataformatec-simpleform/_NRBranMCWE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to plataformatec-simp...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--

Jeroen Visser

Software Engineer

@jeroenvisser101 on GitHub

Reply all
Reply to author
Forward
0 new messages