Forums de discussion

Changing error messages dynamically inside aui validator

Selva kumar, modifié il y a 7 années.

Changing error messages dynamically inside aui validator

Junior Member Publications: 39 Date d'inscription: 23/07/15 Publications récentes
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, modifié il y a 7 années.

RE: Changing error messages dynamically inside aui validator

Expert Publications: 467 Date d'inscription: 31/10/11 Publications récentes
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, modifié il y a 7 années.

RE: Changing error messages dynamically inside aui validator

Junior Member Publications: 39 Date d'inscription: 23/07/15 Publications récentes
There is no order for the validators to get triggered. Is there any way to say the order ?
thumbnail
Sushil Patidar, modifié il y a 7 années.

RE: Changing error messages dynamically inside aui validator

Expert Publications: 467 Date d'inscription: 31/10/11 Publications récentes
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, modifié il y a 7 années.

RE: Changing error messages dynamically inside aui validator

Junior Member Publications: 39 Date d'inscription: 23/07/15 Publications récentes
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, modifié il y a 7 années.

RE: Changing error messages dynamically inside aui validator (Réponse)

Expert Publications: 252 Date d'inscription: 06/04/12 Publications récentes
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, modifié il y a 7 années.

RE: Changing error messages dynamically inside aui validator (Réponse)

Junior Member Publications: 39 Date d'inscription: 23/07/15 Publications récentes
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, modifié il y a 7 années.

RE: Changing error messages dynamically inside aui validator

Expert Publications: 252 Date d'inscription: 06/04/12 Publications récentes
Sorry about that, should have been myFormValidator.get('fieldStrings')