简体   繁体   中英

Liferay webform portlet field validation

Portlet describes how should it work:

Enter JavaScript code that returns true or false to validate the field. The following implicit variables are available.

currentFieldValue: The value being validated fieldsMap: The array of all form values indexed by name

I haven't found any documentation about this.

Anyways, I have web form that send the form input to my email, but if the last checkbox is not checked then it should give an error.

So I came up with this code, but it doesn't work:

if(currentFieldValue == false){return false;}

Anybody knows how this liferay webform portlet validation works?

  1. Stop Tomcat

  2. Go to APPLICATION_SERVER_ROOT_DIRECTORY/webapps/web-form-portlet/WEB-INF/classes directory

  3. Open portlet.properties and change "validation.script.enabled" property from "false" to "true"

  4. Start Tomcat

  5. Add Web Form portlet to a page

  6. On the configuration pop-up of Web Form portlet we added a Check Box type field with the validation script you wrote in the description: "if(currentFieldValue == false){return false;}" and the validation error message: "Tick the checkbox!"

  7. Tried to send the created form without selecting the checkbox created in the previous step.

Result : "Tick the checkbox!" message appeared on the screen.

I tried it like this way but I always got the rrror You have entered invalid data. Please try again. You have entered invalid data. Please try again. .

So I started to change

if(currentFieldValue == false){return false;}

into

if(currentFieldValue != ""){return true;}

It seemed that the submit needs an true may it's set automaticaly to false .

This works with all kind of fields.

currentFieldValue始终是一个字符串,因此对我来说正确的解决方案是将验证脚本设置如下:

return (currentFieldValue == 'true')

I followed the steps mentioned in the post of Algeron. I added the custom validation in the checkbox field and when I fill the form and click the save button, the form can't be processed. In the tomcat stdout logs I found the next error:

12:45:27,628 ERROR [http-bio-8080-exec-2][WebFormUtil:257] The following script has execution errors:
return (currentFieldValue == 'true');
missing ] in index expression (Validation Script#8)

Anyone have any ideas how I could solve it? Thanks in advance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM