You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Pedro Emanuel de Castro Faria Salgado <pe...@junifeup.pt> on 2003/06/18 02:02:41 UTC
Re: validator framework Help needed
This is way beyond my skills (i ve been working w struts just for
week... and done simple validations... this is more customized
validation)... i can check it up on some book or documentation that i
have if nobody else gives you an answer.
Pedro Salgado
------------
Now I am using the validator framework, for client side validation, I
probably cant have field based validation, coz I have to validate
certain indexed fields, conditionally. So I did it the following way(I
have pasted the code below).
It working fine but I wanna know ..
> if this is the right way to use it.
> by using it this way how can i pick up messages from
ApplicationResources.properties
Thanks & regards,
Prasenjit.
********************************************************************************************************
<validator name="checkOfficerDetailsForm"
classname="com.hsbc.gcc.gpm.formbean.OfficerDetailsForm"
method="doOffDetailsFormValidation"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
javax.servlet.http.HttpServletRequest"
msg="errors.required">
<javascript><![CDATA[
function doOffDetailsFormValidation(form)
{
if (! validateForm(form))
{
return false;
}
if ( !validateSelectedRows(form) )
{
alert ('Atleast one Officer needs to be selected for this operation.');
alert(fields.join('\n'));
return false;
}
if ( !isIdUnique(form) )
{
alert('ID selected for Officer already exists. Please selected a different
ID.');
alert(fields.join('\n'));
return false;
}
return true;
}
function trimString (str)
{
str = this != window? this : str;
return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}
function validateSelectedRows(form)
{
var thisCheckId;
for (i=0;i<document.forms[0].checkId.length;i++)
{
thisCheckId = document.forms[0].checkId[i];
if (thisCheckId.checked)
{
return true;
}
}
return false;
}
function isIdUnique(form)
{
var selElement;
var chkValue;
for (i=0;i<document.forms[0].checkId.length;i++)
{
if ( document.forms[0].checkId[i].checked )
{
chkValue = document.forms[0].checkId[i].value;
selElement = eval(
"document.all['officerDetailsList["+chkValue+"].officerCode']" );
if ( ! checkInExistingList(form,selElement, chkValue) )
{
return false;
}
}
}
return true;
}
function checkInExistingList(form,selectedElement, selectedCheckBox)
{
var elem;
selectedCheckBox = parseInt(selectedCheckBox);
maxCount = parseInt(maxCount);
for (var i=0;i<maxCount;i++)
{
if (i != selectedCheckBox)
{
elem = eval("document.all['officerDetailsList["+i+"].officerCodeKey']");
if ( trimString(elem.value) == '')
{
elem = eval("document.all['officerDetailsList["+i+"].officerCode']");
}
if ( trimString(elem.value) == trimString(selectedElement.value) )
{
selectedElement.focus();
return false;
}
}
}
return true;
}
function validateForm(form)
{
var selOfficerCode;
var selProfitCenter;
var selOfficerName;
var selAreaCode;
var selAreaName;
for (i=0;i<document.forms[0].checkId.length;i++)
{
if ( document.forms[0].checkId[i].checked )
{
chkValue = document.forms[0].checkId[i].value;
selOfficerCode = eval(
"document.all['officerDetailsList["+chkValue+"].officerCode']" );
selProfitCenter = eval(
"document.all['officerDetailsList["+chkValue+"].profitCenter']" );
selOfficerName = eval(
"document.all['officerDetailsList["+chkValue+"].officerName']" );
selAreaCode = eval(
"document.all['officerDetailsList["+chkValue+"].areaCode']" );
selAreaName = eval(
"document.all['officerDetailsList["+chkValue+"].areaName']" );
if ( trimString(selOfficerCode.value) == '' )
{
alert(' Officer Code cannot be blank.');
selOfficerCode.focus();
return false;
}
else if ( trimString(selProfitCenter.value) == '' )
{
alert(' Profit Center cannot be blank.');
alert(fields.join('\n'));
selProfitCenter.focus();
return false;
}
else if ( trimString(selOfficerName.value) == '' )
{
alert(' Officer Name cannot be blank.');
selOfficerName.focus();
return false;
}
else if ( trimString( selAreaCode[selAreaCode.selectedIndex].value ) == '' )
{
alert(' Please select Area Code.');
selAreaCode.focus();
return false;
}
else if ( trimString(selAreaName.value) == '' )
{
alert(' Area Name cannot be blank.');
selAreaName.focus();
return false;
}
}
}
return true;
}
]]>
</javascript>
</validator>
**********************************************************************
In the jsp i have...
function submitForm(commandVal)
{
document.forms[0].command.value = commandVal;
if (commandVal != 'ADD_ROW')
{
if (! validateOfficerDetailsForm(document.forms[0]) )
{
return;
}
}
document.forms[0].action = "officerDetails.do";
document.forms[0].submit();
}
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org