Liferay Form post activated only on clicking the submit button 2 times? - forms

I'm posting password via Liferay form and storing it in session called "password",
when I'm putting the correct password which I made it as a default for now "1234" the session gets activated
and when I browse across the pages it's working fine.
But when I'm posting it for the first time, it doesn't work, I need to click 2 times on the button so I can see the data or if I go to another page.
Can anyone here support me in this. This is my full code:
<%
String value = BeanParamUtil.getString(article, request, "structureId");
%>
<portlet:actionURL secure="<%= PropsValues.COMPANY_SECURITY_AUTH_REQUIRES_HTTPS || request.isSecure() %>" var="SecondloginURL">
<portlet:param name="saveLastPath" value="0" />
<portlet:param name="struts_action" value="/journal_content/view" />
</portlet:actionURL>
<c:choose>
<c:when test="<%= value.equals(\"10801\") %>">
<%
HttpSession session1 = request.getSession(false);
out.print(session1 + "<br>");
String sessionId = session1.getId();
out.print(sessionId + "<br>");
String foo = (String) session1.getAttribute("password");
out.print(foo + "<br>");
%>
<c:choose>
<c:when test="<%= !Validator.isNull(foo) %>">
<h2>this is the second password and it's working</h2>
<div class="journal-content-article"
id="article_<%= articleDisplay.getCompanyId() %>_<%= articleDisplay.getGroupId() %>_<%= articleDisplay.getArticleId() %>_<%= articleDisplay.getVersion() %>">
<%= RuntimePortletUtil.processXML(application, request, response, renderRequest, renderResponse, articleDisplay.getContent()) %>
</div>
</c:when>
<c:otherwise>
<aui:form action="<%= SecondloginURL %>" name="auth" method="POST">
<aui:input label="Second Password" type="password" name="password" />
<aui:button type="submit" value="authenticate" onClick="location.reload(true)" />
</aui:form>
<%
String pass = request.getParameter("password");
out.println(pass+" = 1234");
%>
<c:if test="<%= Validator.equals(pass, \"1234\") %>">
<%
session1.setAttribute("password","authenticated");
%>
</c:if>
</c:otherwise>
</c:choose>
</c:when>
<c:otherwise>
<div class="journal-content-article"
id="article_<%= articleDisplay.getCompanyId() %>_<%= articleDisplay.getGroupId() %>_<%= articleDisplay.getArticleId() %>_<%= articleDisplay.getVersion() %>">
<%= RuntimePortletUtil.processXML(application, request, response, renderRequest, renderResponse, articleDisplay.getContent()) %>
</div>
</c:otherwise>
</c:choose>
I'm doing a hook on the journal_content/view

What is happening is when the page is refreshed for the first time after (using location.reload()) the session is has not been set till the time you are checking for it and it is being set in the <otherwise> block below the condition.
So when you click the second time to reload the session has already set. Even if you just refresh the page the second time without clicking on the button you will find that the session is set.
So one way I can think of is to use struts-action hook (if you are using Liferay 6.1) to move the setting of the session code in the respective View-action class.
Or else
Can try to move the setting of the session code before you check for the foo string.

Related

Web2py - How to process custom register form?

I am trying to create a custom register view/form and have it work with auth.register(), but so far I've had no luck. I mainly have two problems.
If I don't include response.view = 'register.html' in my controller the link register.html defaults to web2py's default register page (which works, but I don't want to use).
If I removed the response.view = 'register.html' from my controller func, then my custom form/view appears, but doesn't submit/validate. It just keeps popping the on-fail message "Oops something went wrong submitting the form"
Every thread I've read about this just says to specify the form=auth.register() in the controller and use the {{=form.custom.end}} in the view, but that doesn't seem to work. Am I missing something?
I also tried specifying the callback action for the form to action="{{=URL('default', 'register')}}, but if I add this the page won't do anything on submit either.
Here's my code:
Controller: (In default.py)
def register():
response.view = 'register.html'
db.auth_user.first_name.readable = db.auth_user.first_name.writable = False
db.auth_user.last_name.readable = db.auth_user.last_name.writable = False
return dict(form=auth.register())
View (default/register.html)
<body class="body-4">
<div class="form-block w-form">
<form id="email-form" name="email-form" data-name="Email Form">
<h1 class="heading-5">Register</h1>
<input type="email" class="text-field w-input" maxlength4="256" name="Email" data-name="Email" placeholder="Enter your email" id="Email" required="">
<input type="password" class="text-field w-input" maxlength="256" name="Password" data-name="Password" placeholder="Enter your password" id="Password" required="">
<input type="password" class="text-field w-input" maxlength="256" name="Password-2" data-name="Password 2" placeholder="Confirm your password" id="Password-2" required="">
<input type="submit" value="Register" data-wait="Please wait..." class="submit-button w-button">
{{=form.custom.end}}
<div class="w-form-done">
<div>Thank you! Your submission has been received!</div>
</div>
<div class="w-form-fail">
<div>Oops! Something went wrong while submitting the form.</div>
</div>
</div>
<script src="{{=URL('static','js/webflow.js')}}" type="text/javascript"></script>
<!-- [if lte IE 9]><script src="https://cdnjs.cloudflare.com/ajax/libs/placeholders/3.0.2/placeholders.min.js"></script><![endif] -->
</body>
You need to put the viewer code into the file default/register.html instead of register.html .
Then it works for me. You can see the resulting entry in the database.

redirect to renderURL in a MVCPortlet not working

I am building a MVCPortlet Liferay 6.2 portlet. I have a form with 2 buttons. First button validates the form (submit). The second one redirects to a jsp. I have a difficulty to make it work:
<portlet:actionURL name="mainSubmit" var="mainSubmitURL"></portlet:actionURL>
<portlet:renderURL var="viewLogsURL">
<portlet:param name="mvcPath" value="/jsp/logs.jsp"/>
</portlet:renderURL>
<form ... action="<%=mainSubmitURL%>">
...
<div class="controls-row ">
<label class="span1" ></label>
<input class="span2 btn " type="submit" value="Submit Form" >
<label class="span4" ></label>
<input class="span2 btn " onClick="<%=viewLogsURL.toString()%>" value="View Logs" />
</div>
</form>
First button works fine but second button does not redirect to viewLogsURL.
If I replace:
<input class="span2 btn " onClick="<%=viewLogsURL.toString()%>" value="View Logs" />
with:
<buttonclass="span2 btn " onClick="<%=viewLogsURL.toString()%>" value="View Logs" />
then the button also makes a submits the form url (mainSubmitURL) instead of the button URL (viewLogsURL).
And if I use:
<aui:button class="span2 btn " onClick="<%=viewLogsURL.toString()%>" value="View Logs" />
This actually performs the correct redirection but I would like to avoid using it as it generates some css style issues and some filtering-proxy issues that I don't have with or .
So I would rather use or if possible (and I m pretty sure it is).
I also tried to change :
<portlet:renderURL var="viewLogsURL">
<portlet:param name="mvcPath" value="/jsp/logs.jsp"/>
</portlet:renderURL>
with:
<portlet:renderURL var="viewLogsURL">
<portlet:param name="jspPage" value="/jsp/logs.jsp"/>
</portlet:renderURL>
No chance...
Thx in advance.
HTML onclick attribute is used to execute JavaScript code, when the element is clicked. The value must be valid JavaScript code, not a URL. You've probably mistaken it with <a href='url'>...</a> attribute.
That's a basic HTML concept - see for example w3schools description for more details.
To make the redirect work, you can set the redirect URL as the current location:
<input class="span2 btn" onClick="location.href = '<%= viewLogsURL.toString() %>'" value="View Logs"/>
Or call a JavaScript function instead:
<input class="span2 btn" onClick="<portlet:namespace/>doRedirect()" value="View Logs"/>
<script>
function <portlet:namespace/>doRedirect() {
location.href = '<%= viewLogsURL.toString() %>';
}
</script>

Open liferay's portlet in pop-up and after success refresh the calling portlet

We have a requirement to open liferay's add/edit folder/document screen in a dialog pop-up of our custom plugin portlet.
And after successful (no exceptions or errors) save it should automatically close the pop-up and refresh just our plugin portlet on the page.
Following is the code we have used to open liferay's pop-up:
<liferay-portlet:renderURL var="addFolderURL" portletName="<%=PortletKeys.DOCUMENT_LIBRARY %>" windowState="<%=LiferayWindowState.POP_UP.toString() %>">
<liferay-portlet:param name="struts_action" value="/document_library/edit_folder" />
<liferay-portlet:param name="redirect" value="<%= currentURL %>" />
<liferay-portlet:param name="repositoryId" value="<%= String.valueOf(repositoryId) %>" />
<liferay-portlet:param name="parentFolderId" value="<%= String.valueOf(folderId) %>" />
</liferay-portlet:renderURL>
<%
String addFolderJavascript = "javascript:Liferay.Util.openWindow({dialog: {destroyOnHide: true}, id: 'addFolderPopUpId', title: '"
+ ((folder != null) ? UnicodeLanguageUtil.get(pageContext, "add-subfolder") : UnicodeLanguageUtil.get(pageContext, "add-folder"))
+ "', uri: '" + HtmlUtil.escapeJS(addFolderURL) + "'});";
%>
<aui:a href="<%= addFolderJavascript %>" label='<%= (folder != null) ? "subfolder" : "folder" %>' />
The difficulty is how would we know when the add/edit document is successful, how would we close the pop-up and then refresh the portlet since we don't have any control over the add/edit screen coming from liferay. And even if we do it through hook the problem is how to know the success/failure of the document.
Thanks
I never thought the answer would be in Liferay's code itself and was pretty simple.
Liferay also uses such a functionality in Asset Publisher portlet, as shown below:
So liferay after adding the blogs, document etc closes the pop-up and refreshes the asset-publisher portlet. It does this with a simple redirect trick.
Three things:
you require a jsp which would have the redirect and close script.
a redirect url which will call the above jsp after successful save.
In liferay redirect parameter is always passed as a parameter to almost all action requests and on successful save it redirects to the URL passed in this parameter
the popup-id which can be referenced from the jsp.
The jsp is /asset_publisher/add_asset_redirect.jsp and its code is as shown below which can be used in your plugin as it is (I have used comments to explain somethings):
<%
String redirect = request.getParameter("redirect");
redirect = PortalUtil.escapeRedirect(redirect);
Portlet selPortlet = PortletLocalServiceUtil.getPortletById(company.getCompanyId(), portletDisplay.getId());
%>
<aui:script use="aui-base">
Liferay.fire(
'closeWindow', // closes the pop-up window
{
id: '<portlet:namespace />editAsset', // the id of the pop-up which we gave
portletAjaxable: <%= selPortlet.isAjaxable() %>,
<c:choose>
<c:when test="<%= redirect != null %>">
redirect: '<%= HtmlUtil.escapeJS(redirect) %>' // redirects the page to this URL
</c:when>
<c:otherwise>
refresh: '<%= portletDisplay.getId() %>' // refreshes only the portlet
</c:otherwise>
</c:choose>
}
);
</aui:script>
So now lets achieve what we intend to step-by-step in our plugin:
have a jsp similar to add_asset_redirect.jsp in your plugin.
build the redirect url like:
<%
PortletURL customRedirectURL = liferayPortletResponse.createLiferayPortletURL(themeDisplay.getPlid(), portletDisplay.getId(), PortletRequest.RENDER_PHASE, false);
customRedirectURL.setParameter("mvcPath", "/html/common/add_asset_redirect.jsp"); // mvcPath so that your custom portlet's render() calls this jsp when a redirect from liferay's page happens
/* customRedirectURL.setParameter("redirect", themeDisplay.getURLCurrent()); // set this if you want to redirect to a certain page, if only refresh is your motto then leave this commented. */
customRedirectURL.setWindowState(LiferayWindowState.POP_UP);
String customRedirect = customRedirectURL.toString();
// popUp-id while opening the pop-up. should be same as that in add_asset_redirect.jsp
String customPopUpId = renderResponse.getNamespace() + "editAsset";
%>
build the code to open the pop-up:
<liferay-portlet:renderURL var="addFolderURL" portletName="<%=PortletKeys.DOCUMENT_LIBRARY %>" windowState="<%=LiferayWindowState.POP_UP.toString() %>">
<liferay-portlet:param name="struts_action" value="/document_library/edit_folder" />
<liferay-portlet:param name="redirect" value="<%= customRedirect %>" />
<liferay-portlet:param name="repositoryId" value="<%= String.valueOf(repositoryId) %>" />
<liferay-portlet:param name="parentFolderId" value="<%= String.valueOf(folderId) %>" />
</liferay-portlet:renderURL>
<%
String addFolderJavascript = "javascript:Liferay.Util.openWindow({dialog: {destroyOnHide: true}, id: '"
+ customPopUpId +"', title: '"
+ ((folder != null) ? UnicodeLanguageUtil.get(pageContext, "add-subfolder") : UnicodeLanguageUtil.get(pageContext, "add-folder"))
+ "', uri: '" + HtmlUtil.escapeJS(addFolderURL) + "'});";
%>
<aui:a href="<%= addFolderJavascript %>" label='<%= (folder != null) ? "subfolder" : "folder" %>' />
that's all. Go check-it out.
Hope this helps.

(ASP.NET ) multiple buttons in a single form and want to access the clicked button id

the idea is to register an employee in one of the listed courses . each course has and id
retrieved from my DB and display a register button . if the course is full a waiting button shows with the id of the course.
now i want to get the clicked button id to insert it in the DB .
<form method="post" action="**to current page**">
<input type="submit" name="btnWait"" value="register" ID="003"/>
<input type="submit" name="btnWait"" value="register" ID="004"/>
<input type="submit" name="btnWait"" value="wait" ID="005"/>
<input type="submit" name="btnWait"" value="wait" ID="006"/>
I TRIED THIS , BUT DID NOT WORK
i know it is not right but :(
<%
If (Request.Form("btnRegister") = "register") Then
MsgBox(Request("id"))
ElseIf (Request.Form("btnWait") = "wait") Then
MsgBox("wait")
End If
%>
</form>
instead pf <input type="submit" name="btnWait"" value="register" ID="003"/> you can use:
<asp:button id="003" runat="server" text=register" />
and then in the code behind file:
Button clickedbutton = (Button)sender;
if(clickedbutton.ID.Equals(003))
{
//your piece of code
}
Hope it helps

MVC Form in MasterPage not working

Ok, This is a bit confusing and frustrating.
Here's what I have in my masterpage :
<%using (Html.BeginForm("Index", "SearchController", FormMethod.Post, new { #name = "dosearch" }))
{%>
<input type="text" name="ssearch" class="search"><input id="Image1" type="image" runat="server" src="~/App_Themes/DefaultTheme/images/btn_search.gif" width="74" height="29" style="border:none" />
<%} %>
But the corresponding generated html is:
<form action="" method="post" name="dosearch">
<input type="text" name="ticketSearch" class="search"><input src="../App_Themes/DefaultTheme/images/btn_search.gif" name="ctl00$Image1" type="image" id="ctl00_Image1" width="74" height="29" style="border:none" />
</form>
Question is WHY action is empty whereas I have mention action and controllername when declaring the HTML.Helper ????
as a reasult, the seach is not working for obvious reason.
Please help. Thanks.
I think your problem is that you are using "SearchController" but you only need to use "Search" as the MVC framework will add the word "controller".
Also, not that it matters but you are missing a closing input tag