In my form, I've defined a drop-down:
@select(
myForm("category_id"),
options(Category.options()),
'_label -> "Category",
'_default -> "-- Choose a Category --",
'_showConstraints -> false
)
in my controller code:
Form<Category> catForm = form(Category.class).bindFromRequest();
if(catForm.hasErrors()) {
return badRequest(categoryEdit.render(catForm));
}
The form submission does not allow me to select the default value and catForm.hasErrors() is true if I make no selection. Two questions:
How do I allow default value to be selected in the dropdownlist?
I want the default value to be -1
, where to set it? (Maybe this is where the problem is, there is no value associated with -- Choose a Category --
option?)
The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean attribute. The option that is having the 'selected' attribute will be displayed by default on the dropdown list.
The DefaultValue property specifies text or an expression that's automatically entered in a control or field when a new record is created. For example, if you set the DefaultValue property for a text box control to =Now(), the control displays the current date and time.
default value means a value derived from a typical value by the application of pre-determined factors and that may, in circumstances specified in this Directive, be used in place of an actual value.
Answer: Default values, in the context of databases, are preset values defined for a column type. Default values are used when many records hold similar data.
aviks suggestion is working. Maybe you have not imported the template correctly.
I did it like this. First I created a customSelectField.scala.html in views/helper/
as avik suggested:
@(field: play.api.data.Field, options: Seq[(String,String)], args: (Symbol,Any)*)(implicit handler: FieldConstructor, lang: play.api.i18n.Lang)
@getAsTuple(x : Any) = @{
x match {
case (value: String, text: String) => (value, text)
case _ => ("-1", "Select")
}
}
@input(field, args:_*) { (id, name, value, htmlArgs) =>
<select id="@id" name="@name" @toHtmlArgs(htmlArgs)>
@args.toMap.get('_default).map { dv =>
<option class="blank" value="@getAsTuple(dv)._1">@getAsTuple(dv)._2</option>
}
@options.map { v =>
<option value="@v._1" @(if(value == Some(v._1)) "selected" else "")>@v._2</option>
}
</select>
}
and then in my template e.g index.scala.html where I want the select I do:
@import helper._
@helper.customSelectField(
field = proposeNewTimeForm("selectTime"),
options = times.get,
'_label -> "Category",
'_default -> ("-1" -> "-- Choose a category --"),
'_showConstraints -> false
)
Remember you should NOT do:
@implicitField = @{
FieldConstructor(helper.customSelectField.f)
}
because this will cause your error.
If you would like to format the html surrounding the select somehow you could do as I with
a customField.scala.html
in views/helper/:
@(elements: helper.FieldElements)
@elements.input
<span class="errors">@elements.errors.mkString(", ")</span>
<span class="help">@elements.infos.mkString(", ")</span>
and then in the top of index.scala.html
:
@import helper._
@implicitField = @{
FieldConstructor(helper.customField.f)
}
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With