Foros de discusión

need explanation on aui:modelContext

thumbnail
Parth Ghiya, modificado hace 9 años.

need explanation on aui:modelContext

Junior Member Mensajes: 35 Fecha de incorporación: 2/07/13 Mensajes recientes
hello

i am a newbie and need explanation on aui:modelContext with some example..

Thanks emoticon
thumbnail
Rajeev Nair, modificado hace 9 años.

RE: need explanation on aui:modelContext

New Member Mensajes: 15 Fecha de incorporación: 5/03/13 Mensajes recientes
Hi Parth,

aui:context bean tag is used for persisting the values of any object.It should be used to set an object that will be accessed from fields in the form.

Syntax: <aui:model-context bean="<%= entry %>" model="<%= BlogsEntry.class %>" />

Here, bean attribute should be assigned with the object that we need to persist.

model will refer to the data type of the persisting object.

This tag should be used in scenarios where we use the same form for adding an entry as well as editing the entries.

Refer the following example:

<%
Leave leave = null;

long leaveId = ParamUtil.getLong(request, "leaveId");

if (leaveId > 0) {
leave = LeaveLocalServiceUtil.getLeave(leaveId);
}
%>

<aui:form action="<%= requestLeaveURL %>" method="POST">
<aui:input name="leaveId" type="hidden" value="<%= leaveId %>" />
<aui:input name="leave-title" type="text" value='<%= leave == null ? "" : leave.getLeavetitle() %>'>
<aui:validator name="required" errorMessage="Please enter the Leave Title"></aui:validator>
</aui:input>
<aui:input name="leave-description" type="textarea" value='<%= leave == null ? "" : leave.getDescription() %>'>
<aui:validator name="required" errorMessage="Please enter the Leave Details"></aui:validator>
</aui:input>
<aui:button type="submit" value="apply-leaves" />
</aui:form>

In the above example we are actually retrieving the elements from the leave object and then assigning it each form field if it is not null.

Now instead of the above code we should simplify it using the tag: <aui:model-context bean>

Example:

<%
Leave leave = null;

long leaveId = ParamUtil.getLong(request, "leaveId");

if (leaveId > 0) {
leave = LeaveLocalServiceUtil.getLeave(leaveId);
}


%>

<aui:model-context bean="<%= leave %>" model="<%= Leave.class %>" />

<aui:form action="<%= requestLeaveURL %>" method="POST">
<aui:input name="leaveId" type="hidden" />
<aui:input name="leavetitle" type="text">
<aui:validator name="required" errorMessage="Please enter the Leave Title"></aui:validator>
</aui:input>
<aui:input name="description" type="textarea">
<aui:validator name="required" errorMessage="Please enter the Leave Details"></aui:validator>
</aui:input>

<aui:button type="submit" value="apply-leaves" />
</aui:form>

The form will be pe-populated with the appropriate values automatically if the field name matches with any of the column name mentioned in your service.xml file.
As in this case it will refer to the bean and retrieve the value based on the field name

*Note: The only thing we need to take care while using <aui:model-context bean> is that the form fields name should be exactly same as that of the column name mentioned in the entity of your service.xml file.

Hope this was helpful.

Please let me know in case you need more clarification.

Thanks,
Rajeev Nair
thumbnail
Parth Ghiya, modificado hace 9 años.

RE: need explanation on aui:modelContext

Junior Member Mensajes: 35 Fecha de incorporación: 2/07/13 Mensajes recientes
Brilliant..!!

perfect explanation and found it Handy..
Thanks..

i have some further questions pertaining to the same topic..

1. when i write custom aui validation here, it doesnt work emoticon
i mean
<aui:validation name="custom" errorMessage="false">
function(val,fieldNode,ruleValue){

}
</aui:validation>

2. what if i want a dropdown in one of the Input Values? there is no field attribute in aui:select
how to use then?

thanks one again..! emoticon
thumbnail
Rajeev Nair, modificado hace 9 años.

RE: need explanation on aui:modelContext

New Member Mensajes: 15 Fecha de incorporación: 5/03/13 Mensajes recientes
You are welcome Parth emoticon


The reason why your custom validator was not working is because you are using the wrong syntax:

<aui:validation name="custom" errorMessage="false">
function(val,fieldNode,ruleValue){

}
</aui:validation>

The actual tag is: <aui:validator name="custom" errorMessage="false" >

Example:

<aui:input name="age" type="text" label="Experience">

<aui:validator name="custom" errorMessage="You must have atleast a 5 years of working experience" >

function(val, fieldNode, ruleValue) {

var result = false;

if (val >=5) {

result = true;

}

return result;

}

</aui:validator>

</aui:input>

And I am not clear with your second question but if it is about the sytax for using aui:select. Please refer to the below given example:

Example:

<aui:select name="department" label="Department">
<aui:option value="1" label="Development" ></aui:option>
<aui:option value="1" label="Testing" ></aui:option>
</aui:select>


Thanks,
Rajeev Nair
thumbnail
Parth Ghiya, modificado hace 9 años.

RE: need explanation on aui:modelContext

Junior Member Mensajes: 35 Fecha de incorporación: 2/07/13 Mensajes recientes
Hello..

I was saying..

Say we have a dropdown, and we want that value to be filled by aui:modal context .. then what should i do..??

As for valdiations i got my mistake..

i was using the term field in aui:input instead of same name as in service builder..
so my valdiations were not working........
thumbnail
Rajeev Nair, modificado hace 9 años.

RE: need explanation on aui:modelContext

New Member Mensajes: 15 Fecha de incorporación: 5/03/13 Mensajes recientes
Hi!!!!

The <aui:model-context > takes care for aui:select tag also.

If we mention the <aui:model-context > in the required jsp page and if the aui select's name attribute matches with any column name from our service.xml then it will automatically pre-populate the correct value as the selected option and at the same time keeping the other options available.

For example:

<aui:model-context bean="<%= leave %>" model="<%= Leave.class %>" />

<aui:form action="<%= requestLeaveURL %>" method="POST">

<aui:select name="leavetitle" label="Title">
<aui:option value="Sick Leave" label="Sick Leave" ></aui:option>
<aui:option value="Casual Leave" label="Casual Leave" ></aui:option>
</aui:select>

<aui:button type="submit" value="apply-leaves" />
</aui:form>

In the above example if leavetitle (name of the aui:select tag ) is a valid column name mentioned in the service.xml then <aui:model-context> will work fine for the aui:select tag.

Thanks,
Rajeev Nair
thumbnail
Suresh Yadagiri, modificado hace 7 años.

RE: need explanation on aui:modelContext

Junior Member Mensajes: 29 Fecha de incorporación: 24/03/14 Mensajes recientes
How to use aui model context when select dropdown has multiple options selected (i.e aui:select with multiple=true)?