Wednesday, February 2, 2011

ColdMVC: Binding

I really like the binding option in the form tag of ColdMVC. It takes an object from the param scope, prefixes all the elements within it, with model's name (ex. user.first_name), and sets the value from the object. It's very handy. Here is an example of what it currently does:

<c:form controller="user" action="save" bind="user">
<c:hidden name="id"/>

<c:input name="first_name"/>
<c:input name="last_name"/>

<c:buttons>
<c:submit label="save"/>
</c:buttons>

</c:form>

If you wanted to bind the form to 2 or more objects it would be cool if you could use a bind tag and group a set of elements within a form.

<c:form controller="student" action="save">

<c:bind key="user">
<c:hidden name="id"/>

<c:input name="first_name"/>
<c:input name="last_name"/>
</c:bind>

<c:bind key="student">

<c:input name="student_number"/>

</c:bind>

<c:buttons>
<c:submit label="save"/>
</c:buttons>

</c:form>

Or even better on the tag itself. (I think this available already, but I am not sure).

<c:form controller="student" action="save">

<c:hidden name="id" bind="user"/>

<c:input name="first_name" bind="user"/>
<c:input name="last_name" bind="user"/>
<c:input name="student_number" bind="student"/>

<c:buttons>
<c:submit label="save"/>
</c:buttons>

</c:form>

3 comments:

  1. I like the idea. I'll add it to my list.

    ReplyDelete
  2. You can now specify a bind attribute on individual form fields as well as wrap multiple form fields in a bind tag.

    <c:bind to="user">
    <c:input name="firstName" />
    <c:input name="lastName" />
    <c:input name="userName" bind="profile" />
    </c:bind>

    ReplyDelete