DorkRawk
DorkRawk

Reputation: 770

How To Build Select Fields With CssBoundLiftScreen and Record in Lift?

I have a record class like this:

class Address extends Record[Address] {
  def meta = Address

  object countryCode extends OptionalStringField(this, None) {
    override def name = "Country"

    var c = "" // this isn't doing anything but I set it up just to build the simple SHtml.select
    override def toForm = Full(SHtml.select(List(("usa", "usa"),("ca", "ca")), Empty, c = _ ))
  }
  ...
}

object Address extends Address with MetaRecord[Address] { }

and then this form is displayed like this:

object FormPage extends CssBoundLiftScreen {
  def formName = "MyForm"

  val record = Address.createRecord

  field(record.countryCode, FieldBinding("addressCountryCode")
}

in a template like this:

<div class="form">
  <div class="lift:FormPage">
    <div class="fields">
      <fieldset>
        <div id="MyForm_addressCountryCode_field"></div>
      </fieldset>
    </div>
  </div>
</div>

Is this the right way to do inputs other than a simple text field using Record/CssBoundLiftScreen? It doesn't seem like this select would update or create a record properly. How would I have the select show the value of the record field?

Upvotes: 1

Views: 128

Answers (1)

jcern
jcern

Reputation: 7848

If you look at the scaladaoc for OptionalStringField, there are two methods that are provided through the superclass SettableValueHolder and that provide access to the underlying value: get and set

def set (in: Option[MyType]) : Option[MyType]

Set the value of the field to the given value. Note: Because setting a field can fail (return non-Full), this method will return defaultValueBox if the field could not be set.

get : Option[MyType]

get the value

I suspect something like this should work for you:

override def toForm = Full(SHtml.select(List(("usa", "usa"),("ca", "ca")), get, v => set(Some(v)) ))

Upvotes: 1

Related Questions