Foros de discusión

Changing error messages dynamically inside aui validator

Selva kumar, modificado hace 8 años.

Changing error messages dynamically inside aui validator

Junior Member Mensajes: 39 Fecha de incorporación: 23/07/15 Mensajes recientes
I have field called "material" in my JSP. I need to validate with server on tab out. So i have written a custom validator within which i would trigger an ajax call and validate on server. based on response status from server, i would return boolean from the validator.

But the problem is, i would get different error messages from the server and i need to show the appropriate error.
for eg.. "Material is invalid" in one case and "Material is not in stock" in another case.

Is there any to achieve this with single validator? I can write multiple validators for different error messages to handle this situation, but being the server method is same, tweaking error message inside the validator will reduce the server calls?

<aui:input name="Material" type="text">
<aui:validator name="custom">
Function(value){
// trigger Ajax call and get error message
// Set it to Validator.
}
</aui:validator>
</aui:input>
thumbnail
Sushil Patidar, modificado hace 8 años.

RE: Changing error messages dynamically inside aui validator

Expert Mensajes: 467 Fecha de incorporación: 31/10/11 Mensajes recientes
Hi,

As per my opinion write multiple validator and in the first validator call server method and save response in some hidden field and then in second validator use that response(saved in hidden field) to show different error message. Hope it helps.

Regards
Selva kumar, modificado hace 8 años.

RE: Changing error messages dynamically inside aui validator

Junior Member Mensajes: 39 Fecha de incorporación: 23/07/15 Mensajes recientes
There is no order for the validators to get triggered. Is there any way to say the order ?
thumbnail
Sushil Patidar, modificado hace 8 años.

RE: Changing error messages dynamically inside aui validator

Expert Mensajes: 467 Fecha de incorporación: 31/10/11 Mensajes recientes
Hi,

If not sure about the order, call the AJAX on the form load and save value in hidden field.And in each validator check the value of hidden field and the validator whose value is matching that error message will be displayed.

Regards
Selva kumar, modificado hace 8 años.

RE: Changing error messages dynamically inside aui validator

Junior Member Mensajes: 39 Fecha de incorporación: 23/07/15 Mensajes recientes
The value will be validated with the DB tables. the validation primarily occurs on tab out. keeping all the values in the hidden field while the form load is not possible. thats why emoticon
thumbnail
Byrån Zaugg, modificado hace 8 años.

RE: Changing error messages dynamically inside aui validator (Respuesta)

Expert Mensajes: 252 Fecha de incorporación: 6/04/12 Mensajes recientes
You should be able to dynamically change the fieldStrings of your field when you come back with your response from the server.

Take a look at:

In Portal you can access FormValidator via:
var myFormValidator = Liferay.Form.get('myFormId').formValidator;

And access your fieldStrings directly from there.
myFormValidator.fieldStrings.myFieldName.myCustomRuleName = 'Some dynamic message from the server.'
Selva kumar, modificado hace 8 años.

RE: Changing error messages dynamically inside aui validator (Respuesta)

Junior Member Mensajes: 39 Fecha de incorporación: 23/07/15 Mensajes recientes
Thanks Byran. It worked.

Actually i have solved using a hidden variable with small logic. But i know it was not a clean solution.
The one you preferred is neat one.
Thanks a lot.

<aui:validator name="custom" errorMessage="Dummy Message">
function(_value, fldNode, rule){
var errorMesssage = //error message from server;
var myFormValidator = Liferay.Form.get('<portlet:namespace />materialForm').formValidator;
var _ruleData = myFormValidator.get('fieldStrings')[fldNode.get('name')];
for(var i in _ruleData){
if('Dummy Message' === _ruleData){
myFormValidator.get('fieldStrings')[fldNode.get('name')] = errorMesssage;
}
}
return false;
}
</aui:validator>
thumbnail
Byrån Zaugg, modificado hace 7 años.

RE: Changing error messages dynamically inside aui validator

Expert Mensajes: 252 Fecha de incorporación: 6/04/12 Mensajes recientes
Sorry about that, should have been myFormValidator.get('fieldStrings')