Foren

SessionErrors always shows default Error Message

thumbnail
Ricardo Jorge Vandelay, geändert vor 14 Jahren.

SessionErrors always shows default Error Message

New Member Beiträge: 17 Beitrittsdatum: 24.02.10 Neueste Beiträge
I'm trying to use the SessionErrors class to handle validation messages in my configuration form.

However, when I add a custom error message my key is never shown and instead, the "you-have-entered-invalid-data" appears.

Here is how I'm adding:

SessionErrors.add(actionRequest, "insertTitleError");


and showing in the JSP:

<liferay-ui:error key="insertTitleError" message="error-please-insert-title" />


I have looked at the code in the error taglib and the "you-have-entered-invalid-data" message is shown when the key is null. Why is my session message disappearing?


	<c:when test="<%= key == null %>">
		<c:if test="<%= !SessionErrors.isEmpty(portletRequest) %>">
			<span class="portlet-msg-error">
			<liferay-ui:message key="you-have-entered-invalid-data" />
			</span>

			&lt;%= rowBreak %&gt;
		</c:if>
	</c:when>


Best Regards.
thumbnail
Ricardo J. Vandelay, geändert vor 14 Jahren.

RE: SessionErrors always shows default Error Message

New Member Beiträge: 17 Beitrittsdatum: 24.02.10 Neueste Beiträge
bump
thumbnail
Ricardo J. Vandelay, geändert vor 14 Jahren.

RE: SessionErrors always shows default Error Message

New Member Beiträge: 17 Beitrittsdatum: 24.02.10 Neueste Beiträge
bump

I spent a whole afternoon debugging this problem and I cannot figure out why this happens. Blergh!
thumbnail
Amos Fong, geändert vor 14 Jahren.

RE: SessionErrors always shows default Error Message

Liferay Legend Beiträge: 2047 Beitrittsdatum: 07.10.08 Neueste Beiträge
Your code looks correct to me. Is SessionErrors always empty?

Does the user go directly to the jsp with that error tag after you set a SessionError?
thumbnail
Ricardo J. Vandelay, geändert vor 14 Jahren.

RE: SessionErrors always shows default Error Message

New Member Beiträge: 17 Beitrittsdatum: 24.02.10 Neueste Beiträge
When it enters end.jsp in the tag's HTML key and message are null and SessionErrors is empty. I've put some breakpoints in the clear methods in the SessionErrors class and they are only called after end.jsp is executed.

I'm using SessionErrors for my portlet's configuration area. Here is the full code for configuration.jsp and the configuration class.

init.jsp

&lt;%@ page contentType="text/html; charset=UTF-8" %&gt;

&lt;%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %&gt;
&lt;%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %&gt;
&lt;%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %&gt;
&lt;%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %&gt;

&lt;%@ page import="com.liferay.portal.kernel.util.ParamUtil" %&gt;
&lt;%@ page import="com.liferay.portlet.PortletPreferencesFactoryUtil" %&gt;
&lt;%@ page import="javax.portlet.PortletPreferences" %&gt;
&lt;%@ page import="com.liferay.portal.kernel.util.Validator" %&gt;
&lt;%@ page import="com.liferay.portal.kernel.util.HtmlUtil" %&gt;
&lt;%@ page import="com.liferay.portal.kernel.util.PrefsParamUtil" %&gt;
&lt;%@ page import="com.liferay.portal.PortalException" %&gt;


&lt;%@ page import="com.liferay.portal.kernel.servlet.SessionErrors" %&gt;
&lt;%@ page import="com.liferay.portal.kernel.servlet.SessionMessages" %&gt; 

<portlet:defineobjects />
<liferay-theme:defineobjects />

&lt;%

PortletPreferences preferences = renderRequest.getPreferences();
String portletResource = ParamUtil.getString(request, "portletResource");

if (Validator.isNotNull(portletResource)) {
	preferences = PortletPreferencesFactoryUtil.getPortletSetup(request, portletResource);
}

%&gt;



configuration.jsp

&lt;%@ include file="/jsp/init.jsp" %&gt;
&lt;%
String name = PrefsParamUtil.getString(preferences, request, "name", "");

%&gt;

<liferay-ui:error key="baseErrorKey" message="error-message" />
<liferay-ui:success key="baseMessageKey" message="success-message" />

<form action="<liferay-portlet:actionURL portletConfiguration=" true">" method="post" id="<portlet:namespace />fm" name="<portlet:namespace />fm"&gt;
	<div>
		<label for="<portlet:namespace />-name"><liferay-ui:message key="insert-your-name" /></label>
    	<input name="<portlet:namespace />name" id="<portlet:namespace />-name" type="text" value="<%= HtmlUtil.escape(name) %>">
    </div>
    <input type="submit" value="<liferay-ui:message key=" save-information">" /&gt;
</form>



<liferay-ui:error> doesn't work
<liferay-ui:success> works well

class

package com.jump.base.configuration;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletPreferences;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import com.liferay.portal.PortalException;
import com.liferay.portal.kernel.portlet.BaseConfigurationAction;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portlet.PortletPreferencesFactoryUtil;

public class Configuration extends BaseConfigurationAction {
    public void processAction(PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {
        String portletResource = ParamUtil.getString(actionRequest, "portletResource");
        PortletPreferences preferences = PortletPreferencesFactoryUtil.getPortletSetup(actionRequest, portletResource);
        
        String name = ParamUtil.getString(actionRequest, "name");
        preferences.setValue("name", name);
        preferences.store();
        
        SessionErrors.add(actionRequest, "baseErrorKey");
        SessionMessages.add(actionRequest, "baseMessageKey");
    }

    public String render(PortletConfig portletConfig, RenderRequest renderRequest, RenderResponse renderResponse) throws Exception {
        return "/jsp/backend/configuration.jsp";
    }
}


Thank you.
thumbnail
Amos Fong, geändert vor 14 Jahren.

RE: SessionErrors always shows default Error Message

Liferay Legend Beiträge: 2047 Beitrittsdatum: 07.10.08 Neueste Beiträge
Strange...but SessionErrors can't be empty because this:

	<c:when test="<%= key == null %>">
		<c:if test="<%= !SessionErrors.isEmpty(portletRequest) %>">
			<div class="portlet-msg-error">
				<liferay-ui:message key="you-have-entered-invalid-data" />
			</div></c:if></c:when>


What version of Liferay are you using? Is this in a plugin?
thumbnail
Ricardo J. Vandelay, geändert vor 14 Jahren.

RE: SessionErrors always shows default Error Message

New Member Beiträge: 17 Beitrittsdatum: 24.02.10 Neueste Beiträge
I'm developing a Portlet using Liferay 5.2.3 with the Plugins SDK. I've also compiled and deployed the Portal and Ext (without any modifications).

You are correct but in the beginning of that JSP if I do a

System.out.println(SessionErrors.isEmpty(portletRequest));


Its returns true and if I print

SessionErrors.print(portletRequest);


it's empty.

I've also tried this portlet with a standard Liferay bundle (downloaded from the Website) and it's the same problem.

Thank you.
thumbnail
Sandeep Nair, geändert vor 13 Jahren.

RE: SessionErrors always shows default Error Message

Liferay Legend Beiträge: 1744 Beitrittsdatum: 06.11.08 Neueste Beiträge
Did you find a solution?

Regards,
Sandeep
thumbnail
Ricardo J. Vandelay, geändert vor 13 Jahren.

RE: SessionErrors always shows default Error Message

New Member Beiträge: 17 Beitrittsdatum: 24.02.10 Neueste Beiträge
No I didn't. Still have the same issue.
thumbnail
Sandeep Nair, geändert vor 13 Jahren.

RE: SessionErrors always shows default Error Message

Liferay Legend Beiträge: 1744 Beitrittsdatum: 06.11.08 Neueste Beiträge
It only creates problem for configuration.jsp. For normal jsps it works fine.

Regards,
Sandeep
Babu Janarthanan, geändert vor 13 Jahren.

RE: SessionErrors always shows default Error Message

Regular Member Beiträge: 128 Beitrittsdatum: 31.07.08 Neueste Beiträge
Hi,
you can use the below mentioned code snippet.

SessionErrors.add(httpRequest, "baseErrorKey");

<c:if test="<%= SessionErrors.contains(request, "baseErrorKey") %>">

hope it works for you.
thumbnail
Loïc Dumont, geändert vor 13 Jahren.

RE: SessionErrors always shows default Error Message

Junior Member Beiträge: 43 Beitrittsdatum: 27.04.10 Neueste Beiträge
I'm having the same problem (Liferay 5.2.3, plugin-sdk). liferay-ui:error is not working in a configuration jsp but works in other jsps...

Is it a bug?
thumbnail
Denis Signoretto, geändert vor 13 Jahren.

RE: SessionErrors always shows default Error Message

Expert Beiträge: 375 Beitrittsdatum: 21.04.09 Neueste Beiträge
http://issues.liferay.com/browse/LPS-12402

Did you find any workaround?
Arne Timmerman, geändert vor 13 Jahren.

RE: SessionErrors always shows default Error Message

New Member Beitrag: 1 Beitrittsdatum: 29.03.11 Neueste Beiträge
This issue is still not resolved. If, for example, a required field in a configuration.jsp is not filled in and the error is added to the ActionRequest it is gone when the configuration.jsp is re-loaded (after submitting). This is a really annoying bug and affects all Liferay portlets. Did anyone found a solution?
thumbnail
Prakash Khanchandani, geändert vor 12 Jahren.

RE: SessionErrors always shows default Error Message

Expert Beiträge: 329 Beitrittsdatum: 10.02.11 Neueste Beiträge
I am using Liferay 6.0.

This is a really annoying bug and affects all Liferay portlets.


It does not affect all liferay portlets, coz I can see it working in blogs portlet, you can check out the source of com.liferay.portlet.blogs.action.ConfigurationActionImpl and html/portlets/blogs/configuration.jsp.

But somehow I can't figure out why its not working in my portlet!!emoticon

is there a possibility that we might be missing some kind of configuration in the liferay-portlet.xml?? emoticon

Currently I have handled it manually by setting error key attributes directly in request, the following is a skeleton of the jsp code:
<c:if test="${not empty customErrorKey}">
  <div class="portlet-msg-error">
    <liferay-ui:message key="my-custom-error-msg" />
  </div>
<c:if></c:if></c:if>


but this is a really crude method and hamper's code cleanliness, writing 4 lines of code and css instead of just one.

Also I have used a hook to customize the taglib/ui/error/end.jsp to not show the default error message
<liferay-ui:message key="you-have-entered-invalid-data" />


Anyone's help is much appreciated.

Thank You
olivier spieser, geändert vor 12 Jahren.

RE: SessionErrors always shows default Error Message

New Member Beiträge: 13 Beitrittsdatum: 06.06.11 Neueste Beiträge
Hi,

I found a solution that worked for me.

1 - I overrided the file "porlet_message.jsp", I added some code to iterate over SessionErrors at the bottom of the file :

<liferay-ui:success key="<%= portletName + &quot;.doConfigure&quot; %>" message="you-have-successfully-updated-the-setup" />
<liferay-ui:success key="<%= portletName + &quot;.doEdit&quot; %>" message="you-have-successfully-updated-your-preferences" />
&lt;%
Iterator<string> itErrors = SessionErrors.iterator(renderRequest);
while (itErrors.hasNext()){
	String errorKey =  itErrors.next();
	if (StringUtils.startsWith(errorKey , portletName)
			&amp;&amp; StringUtils.endsWith(errorKey , ".doConfigure")) {
		
		String errorMessage = StringUtils.substringBetween(errorKey ,portletName+".", ".doConfigure");
		
		%&gt;
			<liferay-ui:error key="<%=errorKey%>" message="<%=errorMessage%>" />
		&lt;%
	}
}
%&gt;

<liferay-ui:error /></string>


2 -Then, in the configure action I simply add SessionErrors I want to display :

SessionErrors.add(actionRequest, portletConfig.getPortletName() + ".my-custom-error.doConfigure");


As you can see the sessionError key contains :
- the portlet Name
- The message
- The ".doConfigure" token

So, with these elements, you are sure the configure errors are only displayed in the configuration page of your portlet.
Harish Dhulipalla, geändert vor 12 Jahren.

RE: SessionErrors always shows default Error Message

New Member Beiträge: 3 Beitrittsdatum: 29.07.09 Neueste Beiträge
In liferay 6.1, default error message not get displayed after adding a key "portletName + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE" to SessionMessages.
This gives facility to disable default error messages at portlet level on need basis.

PortletConfig portletConfig = (PortletConfig)request.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
SessionMessages.add(request, portletConfig.getPortletName() + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);

Harish
thumbnail
Prakash Khanchandani, geändert vor 11 Jahren.

RE: SessionErrors always shows default Error Message

Expert Beiträge: 329 Beitrittsdatum: 10.02.11 Neueste Beiträge
Thanks Harish for the info.
Rob Hall, geändert vor 11 Jahren.

RE: SessionErrors always shows default Error Message

Junior Member Beiträge: 47 Beitrittsdatum: 30.11.11 Neueste Beiträge
Is there any way to define this through the portlet.properties configuration?
thumbnail
Abdelrahman Mohamed El Ghanam, geändert vor 11 Jahren.

RE: SessionErrors always shows default Error Message

Junior Member Beiträge: 41 Beitrittsdatum: 30.09.10 Neueste Beiträge
Does this have to be in a certain phase ? cause it doesnt work for me in the render phase .. emoticon
thumbnail
Atin Agarwal, geändert vor 11 Jahren.

RE: SessionErrors always shows default Error Message

Junior Member Beiträge: 86 Beitrittsdatum: 20.02.12 Neueste Beiträge

PortletConfig portletConfig = (PortletConfig)request.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
SessionMessages.add(request, portletConfig.getPortletName() + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);


Not working in Custom portlets.
I have tried this in hooks. It Worked perfectly.
However when i have tried this in my Custom portlet's Process Action it didn't worked..I wonder Why??
Any help wud be appreciated.


Regards
thumbnail
Manish Kumar Jaiswal, geändert vor 11 Jahren.

RE: SessionErrors always shows default Error Message

Regular Member Beiträge: 153 Beitrittsdatum: 25.11.08 Neueste Beiträge
Hi Atin ,

Following didnt work for me as well in plugin portlet . Does any one have solved it ?

PortletConfig portletConfig = (PortletConfig)request.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
SessionMessages.add(request, portletConfig.getPortletName() + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);

Any help wud be appreciated.

Regards
Manish
thumbnail
Philipp Schwarz, geändert vor 10 Jahren.

RE: SessionErrors always shows default Error Message

Junior Member Beiträge: 29 Beitrittsdatum: 19.10.12 Neueste Beiträge
Hey folks,

same here. I'm developing on Liferay 6.1.1 CE / emoticonTomcat 7.0.27 and followed the documentation here.

Here is my jsp:
<liferay-ui:error key="tos-required" message="error.required.terms-of-service" />
<liferay-ui:success key="form-processed" message="success.form-processed" />

<liferay-portlet:actionurl var="processFormURL" name="processForm" />
<aui:form action="<%= processFormURL.toString() %>">
	<aui:input name="termsofservice" type="checkbox" label="label.terms-of-service" checked="false" />				
	<aui:button value="button.submit" type="submit" />
</aui:form>


Here are two methods from my action class:
public void processForm(ActionRequest actionRequest, ActionResponse actionResponse) 
		throws IOException, PortletException{		
	
	if(ParamUtil.getBoolean(actionRequest, "termsofservice") == false)
		SessionErrors.add(actionRequest, "tos-required");
		
	else
		SessionMessages.add(actionRequest, "form-processed");
}


@Override
public void processAction(ActionRequest actionRequest,	ActionResponse actionResponse)
        throws IOException, PortletException {

	LiferayPortletConfig config = 
		(LiferayPortletConfig)actionRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);

	String hideDefaultMessage = 
                config.getPortletName() + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE;
	
	SessionMessages.add(actionRequest, hideDefaultMessage);
	
	super.processAction(actionRequest, actionResponse);
	
}


And this is what it looks like when the form is submitted without ticking the checkbox:


Strangely enough, the default success message doesn't get displayed, because when I do tick the checkbox, the result is the following:


It would be really helpful if someone from the liferay staff could shed some light onto this.

Thanks and happy coding,
Phil
Pradeep P, geändert vor 10 Jahren.

RE: SessionErrors always shows default Error Message

New Member Beiträge: 7 Beitrittsdatum: 17.04.12 Neueste Beiträge
Hi phil,


Try using the below code :

SessionMessages.add(request,
PortalUtil.getPortletId(actionRequest)
+ SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);
thumbnail
Harish Kumar, geändert vor 10 Jahren.

RE: SessionErrors always shows default Error Message

Expert Beiträge: 483 Beitrittsdatum: 31.07.10 Neueste Beiträge
Use the below code snippet to hide SessionErrors default error message


PortletConfig portletConfig = (PortletConfig)renderRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
LiferayPortletConfig liferayPortletConfig = (LiferayPortletConfig) portletConfig;
SessionMessages.add(renderRequest, liferayPortletConfig.getPortletId() + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);


hope this helps!
thumbnail
Philipp Schwarz, geändert vor 10 Jahren.

RE: SessionErrors always shows default Error Message

Junior Member Beiträge: 29 Beitrittsdatum: 19.10.12 Neueste Beiträge
Hey Pradeep and Harish,

big thanks for your proposals!

I will have to wait for the next release cycle before I can test this, but it's definitely worth a shot. If it works we should also make an effort to update the documentation which clearly states to use the portlet name instead of the portlet id.

Anyways, thanks again,
happy coding,
Phil
thumbnail
Philipp Schwarz, geändert vor 9 Jahren.

RE: SessionErrors always shows default Error Message

Junior Member Beiträge: 29 Beitrittsdatum: 19.10.12 Neueste Beiträge
Hey Harish,

I know it's been a while but I just wanted to let you know that your snippet did the job for me!

Thanks again,
Philipp