Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

FAQ for model binding topics, focussing specifically on how to use model binding with tags.

How to Bind Inputfields to a Structure Field?

Question: Is possible to assign values of inputfields directly to field of a structure? (Presently I have lots of inputfields on one view and these fields are corresponding to a line of a table which is defined on my model. To use the data binding, I create currently for each inputfield a model-attribute and than I use the usual way to write all values from these many model-attributes back to the table.)

Answer: Use a structure as an attribute of a model class, and then use the model binding relative to the structure in the view. Take care with the data binding syntax. Important is that strings are used to specify the model binding path. The string format is //<model_name>/<structure_name>.<field>. This string always starts with a //, then the name of the model attribut in the view follows. The attribute of the model comes behind a '/' and the structure-element is separated by a '.'.

Why Can Model Binding Parameters Not Be Set Dynamically?

I use a <htmlb:inputField> with model binding and it works perfectly.

<htmlb:inputField id="id1" type="string" value="//model/client.clientno" />

However, I would like to set the actual model binding string dynamically for many complex reasons. My code is now (with v = "//model/client.clientno"), which then displays the model binding string itself in the browser instead of the value behind it:

<htmlb:inputField id="id1" type="string" value="<%=v%>" />

Answer: The problem is actually complex. When the compiler sees a tag, it generates code to execute it. The BSP compiler sees:

<tag:element a1= "v1"/>

It generates code:

DATA: _t1 TYPE REF TO CL_TAG_ELEMENT.
CREATE OBJECT _t1.
_t1->a1 = 'v1'.
...code to execute tag...

The problem is now what happens if the attribute is of type integer, or of type ref to data, and we have model binding. Then it is not possible to place the binding string inside the attribute (not matching types). For this reason, once model binding is set for a specific attribute, there are two generated attributes on the element. The one is of the configured type, the other (prefixed with _) is of type string for the binding path. Only one is set by the BSP compiler.

The BSP compiler sees:

  <tag:element a1 = "//model/m1"/>

It generates code (important, notice _a1):

  DATA: _t1 TYPE REF TO CL_TAG_ELEMENT.
CREATE OBJECT _t1.
_t1->_a1 = '//model/m1'.
...code to execute tag...

Already at compile time, the compiler must know whether it must set a1 or _a1. Model binding is pure compile time construct, and does not work at runtime. It is not possible to set this parameter dynamically.

The only possible way to achieve this, if you really insist, is to dynamically process the tag directly on the page. Then you must set the attributes yourself correctly. For background reading information, I would recommend the weblog BSP Programming: Writing Composite Elements. The expected code will be about:

<%
    DATA: myTag TYPE REF TO CL_TAG_ELEMENT.
    myTag = CL_TAG_ELEMENT=>FACTORY( _a1 = vh_value ).
    WHILE _m_page_context->element_process( myTag ) = if_bsp_element=>co_element_continue.
    ENDWHILE.
%>

How to highlight inputfields containing wrong values?

I use a <htmlb:inputField> with model binding

<htmlb:inputField id="id1" type="string" value="//model/client.clientno" />

In the methods of the modelI add a message to the errors object via method ADD_MESSAGE_FROM_T100.

errors->add_message_from_t100( condition = 'client.clientno'
                               msgid     = 'Z01'
                               msgno     = '001'
                               msgty     = 'E'                                dummy   = <model_instance>client-clientno ).

Note: It's very important to set the value entered by the user in the method's parameter DUMMY otherwise the inputfiled will be displayed empty!