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

Add examples to use @b3.radio with boolean properties? #35

Closed
adis-me opened this issue Jul 15, 2015 · 4 comments
Closed

Add examples to use @b3.radio with boolean properties? #35

adis-me opened this issue Jul 15, 2015 · 4 comments

Comments

@adis-me
Copy link

adis-me commented Jul 15, 2015

Are there any examples how to use radio or checkbox with boolean properties? In the documentation I can read that the radio and checkbox only have Seq[(String, Any)] and thus not allowed to pass boolean.

Btw: I am using your lib a Java project...

@adis-me
Copy link
Author

adis-me commented Jul 15, 2015

Hmm perhaps I solved it by extending, is this correct to use:

@(field: play.api.data.Field, args: (Symbol,Any)*)(implicit handler: b3.B3FieldConstructor)
@b3.inputFormGroup(field, withFeedback = false, withLabelFor = true, b3.Args.withDefault(args, 'class -> "")) { fieldInfo =>
    <div class="checkbox">
        <label for="@fieldInfo.id">
        <input type="checkbox" id="@fieldInfo.id" name="@fieldInfo.name" @if(fieldInfo.value == Some("true")){ checked } @toHtmlArgs(fieldInfo.innerArgsMap)>
        </label>
    </div>
}

@adrianhurt
Copy link
Owner

Sorry, but I don't understand your question. A checkbox will work with value="true" as default, and a radio uses a Seq[(String, Any)], so you could use simply something like Seq("true"->"True","false"->"False").

Play will handle the true and false strings as the corresponding boolean.

Then, the following code

@b3.checkbox( form("foo"), '_text -> "Remember me" )
@b3.radio( form("foo"), options = Seq("true"->"True","false"->"False"), '_label -> "Radio Group" )

will be rendered as:

<div class="form-group" id="foo_field">
  <div class="checkbox">
    <label for="foo">
      <input type="checkbox" id="foo" name="foo" value="true">
      Remember me
    </label>
  </div>
</div>

<div class="form-group" id="foo_field">
  <label class="control-label ">Radio Group</label>
  <div class="radio">       
    <label class="" for="foo_true">
      <input type="radio" id="foo_true" name="foo" value="true">
      True
    </label>
  </div>
  <div class="radio">       
    <label class="" for="foo_false">
      <input type="radio" id="foo_false" name="foo" value="false">
      False
    </label>
  </div>
</div>

@adis-me
Copy link
Author

adis-me commented Jul 15, 2015

@adrianhurt Yes it works like you described. I did not know that Seq("true"->"True","false"->"False") will work because of the signature Seq[(String, Any)].

I tried the following but that did not worked for radio inputs: Seq("1"->"True","0"->"False").

I will close this issue since the real problem is my Scala knowledge 😆

@adis-me adis-me closed this as completed Jul 15, 2015
@adrianhurt
Copy link
Owner

Don't worry ;) And yes, I think you had a little confusion but not with Scala. Note that everything will be finally rendered as a simple String for HTML. That's the reason to use (String, Any) values (where any Any value will call its toString function.

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