Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Bea Bravo
Form enctype="multipart/form-data" + captcha
March 21, 2012 6:21 AM
Answer

Bea Bravo

Rank: New Member

Posts: 5

Join Date: September 29, 2010

Recent Posts

Hello,

I'm use Liferay 6.0 and I'm programming a portlet who contains one form. This form has the enctype "multipart/form-data"" because has a field for upload a file. This form use the captcha of Liferay also. The form works succesfully when it has not captcha.

<form:form name="formStep1" commandName="solicitud" method="post" action="${formStep2ActionUrl}" enctype="multipart/form-data">
<form:input type="text" path="nombre" name="nombre" id="nombre"/>
....
<form:input id="file" name="file" type="file" path="file" />
...
</form:form>


But when I add the code corresponding of catcha, the portlet fails (Text verification failed.):

<form:form name="formStep1" commandName="solicitud" method="post" action="${formStep2ActionUrl}" enctype="multipart/form-data">
<form:input type="text" path="nombre" name="nombre" id="nombre"/>
....
<form:input id="file" name="file" type="file" path="file" />
....
<liferay-ui:captcha url="<%= captchaURL %>" />
...
</form:form>


If I put enctype="multipart/form-data" the upload file works fine but the catpcha no. If I don't put enctype="multipart/form-data", the captcha works fine but the upload file no. Anybody can help me, please? How can I resolve this?

Thank you.
Bea.
Asier Hernández
RE: Form enctype="multipart/form-data" + captcha
April 13, 2012 5:58 AM
Answer

Asier Hernández

Rank: New Member

Posts: 21

Join Date: February 1, 2011

Recent Posts

Have you found any solution to this isuue??

I have the same problem with captcha and multipart/form data.

Thanks in advance
Miguel Ángel Júlvez
RE: Form enctype="multipart/form-data" + captcha
May 7, 2012 7:30 AM
Answer

Miguel Ángel Júlvez

Rank: Junior Member

Posts: 56

Join Date: March 29, 2011

Recent Posts

Any update about this issue?

Regards!
Asier Hernández
RE: Form enctype="multipart/form-data" + captcha
May 7, 2012 8:20 AM
Answer

Asier Hernández

Rank: New Member

Posts: 21

Join Date: February 1, 2011

Recent Posts

Yes, here is how I have resolved this issue:

1
2    <c:if test="<%= requireCaptcha %>">       
3            <portlet:resourceURL var="captchaURL">
4                <portlet:param name="<%= Constants.CMD %>" value="captcha" />
5            </portlet:resourceURL>   
6            <liferay-ui:captcha url="<%= captchaURL %>" />       
7    </c:if>


I hope this will help you.
Miguel Ángel Júlvez
RE: Form enctype="multipart/form-data" + captcha
May 7, 2012 8:31 AM
Answer

Miguel Ángel Júlvez

Rank: Junior Member

Posts: 56

Join Date: March 29, 2011

Recent Posts

Thanks Asier but I've that piece of code and it doesn't work

This is the error line in catalina.out
ERROR [SimpleCaptchaImpl:398] Captcha text is null. User null may be trying to circumvent the captcha.

It happens when I send a multipart form with captcha

Regards!
Asier Hernández
RE: Form enctype="multipart/form-data" + captcha
May 7, 2012 8:45 AM
Answer

Asier Hernández

Rank: New Member

Posts: 21

Join Date: February 1, 2011

Recent Posts

I think youl will have to change the code in your Portlet class. In my code I have my WebFormPortlet.java with this code in the saveData method:

 1
 2
 3        if (requireCaptcha) {
 4            try {
 5                //CaptchaUtil.check(actionRequest);
 6                String enteredCaptchaText = ParamUtil.getString(uploadRequest, "captchaText");               
 7
 8                PortletSession session = actionRequest.getPortletSession();
 9                //String captchaText = getCaptchaValueFromSession(session);
10                String captchaText = "";
11                Enumeration<String> atNames = session.getAttributeNames();
12                while (atNames.hasMoreElements()) {
13                    String name = atNames.nextElement();
14                    if (name.contains("CAPTCHA_TEXT")) {
15                        captchaText = (String) session.getAttribute(name);
16                    }
17                }               
18                if (Validator.isNull(captchaText)) {
19                    throw new Exception("Internal Error! Captcha text not found in session");
20                }
21                if (!StringUtils.equals(captchaText, enteredCaptchaText)) {
22                    throw new Exception("Invalid captcha text. Please reenter.");
23                }
24            }
25            catch (CaptchaTextException cte) {
26                SessionErrors.add(
27                        actionRequest, CaptchaTextException.class.getName());
28
29                return;
30            }
31        }


And this would be my serveResource method:

 1
 2
 3    public void serveResource(
 4            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
 5        throws IOException, PortletException {
 6
 7        String cmd = ParamUtil.getString(resourceRequest, Constants.CMD);
 8       
 9        try {
10            if (cmd.equals("captcha")) {
11                //serveCaptcha(resourceRequest, resourceResponse);
12                com.liferay.portal.kernel.captcha.CaptchaUtil.serveImage(resourceRequest, resourceResponse);
13            }
14            else if (cmd.equals("export")) {
15                exportData(resourceRequest, resourceResponse);
16            }
17        }
18        catch (Exception e) {
19            _log.error(e, e);
20        }
21    }


Try if this is a valid solution for your problem.
Miguel Ángel Júlvez
RE: Form enctype="multipart/form-data" + captcha
May 7, 2012 10:16 AM
Answer

Miguel Ángel Júlvez

Rank: Junior Member

Posts: 56

Join Date: March 29, 2011

Recent Posts

It works!!!

Thanks Asier!