« Voltar para Web Content...

Web Content Field Validation

Introduction #

This article describes how to add field validations into web content (e.g. make a field mandatory for submission, or requiring that a field is numeric).

Environment #

Liferay-Tomcat Bundle 6.0 Plugins SDK - Hooks

Sample Structure #

<root>
  <dynamic-element name='name' type='text' validate='mandatory'></dynamic-element>		
</root>

List of JSP Files to be extended using Hooks #

Information about creating JSP Hooks is available in the Liferay Wiki. Create a JSP Hook for the Journal Portlet. Here is a list of files that need to be extended in order to implement validation -

1) edit_article.jsp
2) edit_article_content_xsd_el.jsp
3) edit_structure_xsd_el.jsp
4) edit_structure.jsp

Changes required in edit_structure_xsd_el.jsp #

1) Read the element in the starting the way name and type attributes are read

String elValidate = JS.decodeURIComponent(el.attributeValue("validate", StringPool.BLANK));

2) Define a new hidden element "validate" in the existing table. The table already has a hidden element "_depth".

<td>
<input id="<portlet:namespace />structure_el<%= count.getValue() %>_validate" tabindex="<%= tabIndex.getValue() %>" type="hidden" size="20" value="<%= elValidate %>" />
</td>

Changes required in edit_article_content_xsd_el.jsp #

1) Read the element in the starting

String elValidate = el.attributeValue("validate", StringPool.BLANK);

2) Define a hidden element along with the others like name, type,depth

<input id="<portlet:namespace />structure_el<%= count.getValue() %>_validate" type="hidden" value="<%= elValidate %>" />

Changes required in edit_structure.jsp #

1) Modify the existing getXsd() function and read the validate attribute like the other attributes.

Some code snippet for the same

var elValidate = document.getElementById(portletNameSpace+"structure_el" + i + "_validate");

			if (elDepth != null && elName != null && elType != null) {
				var elDepthValue = elDepth.value;
				var elNameValue = encodeURIComponent(elName.value);
				var elTypeValue = encodeURIComponent(elType.value);
				var elValidateValue = encodeURIComponent(elValidate.value);
				
				if (cmd == "add" || cmd == "remove" && elCount != i) {
					for (var j = 0; j <= elDepthValue; j++) {
						xsd += xmlIndent;
					}
						if(elValidateValue.length == 0){
							xsd += "<dynamic-element name='" + elNameValue + "' type='" + elTypeValue + "'>";
						}
						else{
							xsd += "<dynamic-element name='" + elNameValue + "' validate='" + elValidateValue + "' type='" + elTypeValue + "'>";
						}
					if (cmd == "add" && elCount == i) {
						xsd += "<dynamic-element name='' type=''></dynamic-element>\n";
					}

Changes required in edit_article.jsp #

Liferay calls the javascript function

function <portlet:namespace />saveArticle(cmd) {

before saving any article. So we would need to override this function and call a validation function before submitting the form.

Sample Validation Function -

function trim(str) {
    return str.replace(/^\s+|\s+$/g,"");
}

function validateArticleContent(portletNameSpace)
{
	var flag = "true";
	for (i = 0; i >= 0; i++) {
				var elDepth = document.getElementById(portletNameSpace+"structure_el" + i + "_depth");
				var elName = document.getElementById(portletNameSpace+"structure_el" + i + "_name");
				var elType = document.getElementById(portletNameSpace+"structure_el" + i + "_type");
				var elValidate = document.getElementById(portletNameSpace+"structure_el" + i + "_validate");
				var elContent = document.getElementById(portletNameSpace+"structure_el" + i + "_content");
				var elLanguage = document.getElementById(portletNameSpace+"structure_el" + i + "_localized");

				if (elDepth != null && elName != null && elType != null) {
					var elDepthValue = elDepth.value;
					var elNameValue = elName.value;
					var elTypeValue = elType.value;
					var elValidateValue = elValidate.value;
					var elContentValue = "";
					var elLanguageValue = elLanguage.value;
					

					if (elTypeValue == "text" || elTypeValue == "text_box" || elTypeValue == "image_gallery" || elTypeValue == "document_library" || elTypeValue == "link_to_layout") {
						
						elContentValue = elContent.value;
						elContentValue = "<![CDATA[" + elContentValue + "]]>";
						
						if(elValidateValue == "mandatory")
						{
							if(trim(elContent.value) == ""){
								alert('Please enter valid '+elNameValue+' value.');								
								flag ="false";
								break;
							}
						}

					}
				}
				else
				{
					break;
				}
	}
	
	return flag == "true";
}

This concludes the example: other kinds of validation can be added in a similar fashion.

0 Anexos
84238 Visualizações
Média (1 Votar)
A média da avaliação é 5.0 estrelas de 5.
Comentários
Respostas do tópico Autor Data
for More on Liferay ... Rohit Salecha 9 de Maio de 2011 23:53
Hi, I tried above example on Liferay 6.1. But... Padmajeet Mhaske 10 de Abril de 2012 09:18
Hi, i have a problem. I have to make required... Enrico Maria Rossi 8 de Fevereiro de 2013 03:09
Categorization fild in a new creation of ... Enrico Maria Rossi 8 de Fevereiro de 2013 03:12
How do we do the same for Liferay 6.2? Asking... Bimal Patel 8 de Janeiro de 2014 23:31

for More on Liferay

http://liferaydemystified.blogspot.com/
Postado em 09/05/11 23:53.
Hi,

I tried above example on Liferay 6.1. But it seems that structure for "edit_article.jsp" is changed.
I have manged to get workaround for other files but failing to do same with "edit_article.jsp".

Can you please provide me how get all elements name inside java-script functions?


Regards,
Padmajeet
Postado em 10/04/12 09:18.
Hi,
i have a problem. I have to make required the categorization filed. How can i do it? Thanks.

Regards,
Enrico
Postado em 08/02/13 03:09.
Categorization fild in a new creation of webcontent emoticon
Postado em 08/02/13 03:12 em resposta a Enrico Maria Rossi.
How do we do the same for Liferay 6.2? Asking since many components are changed in Liferay 6.2 and these files are not found anymore, esp. *xsd*.jsp.
Postado em 08/01/14 23:31.