Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow default value selection in a @select helper?

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:

  1. How do I allow default value to be selected in the dropdownlist?

  2. 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?)

like image 230
Manoj Avatar asked May 17 '13 10:05

Manoj


People also ask

How do you set a selection to default value?

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.

What is the use of default value in the properties?

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.

What do you understand by default value?

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.

What do you mean by default value of a field in base and how can it be set?

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.


1 Answers

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!

like image 200
jakob Avatar answered Oct 19 '22 23:10

jakob