1
votes

I am using liferay to develop a custom portlet (mvc portlet) my problem is that i have one form in jsp page in HTML and and what i want is to validate some field of the forms.and on submit button its redirecting to another page.the validation i have done is working but what happen is when am clicking the submit button its redirect to another page and on that page its just showing default error from liferay. What i want is to display same page if request not completed with the error message near the field which validation has false.

How can i solve this issue?

this is my code to checking validation while add method in action class

public void addRestaurant(ActionRequest request, ActionResponse response) {

    log.info("Inside addRegistration");
    List<String> errors = new ArrayList<String>();

    restaurant rest = RestaurantActionUtil
            .getRestaurantFromRequest(request);
    boolean restValid = RestaurantValidator
            .validateRestaurant(rest, errors);
    if (restValid) {
        try {
            log.info(rest);

            restaurant test = restaurantLocalServiceUtil
                    .addrestaurant(rest);
            if (test == null) {
                log.error("Restaurant was Found Null");
                response.setRenderParameter("jspPage", errorJSP);
                return;
            }
        } catch (SystemException e) {
            log.error(e);
            /*
             * If there is an error then divert the control to the error
             * page.
             */
            response.setRenderParameter("jspPage", errorJSP);
            return;
        }
        SessionMessages.add(request, "restaurant-added");
        return;
    } else {
        for (String error : errors) {
            SessionErrors.add(request, error);
        }
        SessionErrors.add(request, "error-while-adding");
        request.setAttribute("rest", rest);

        return;
    }

}

This is my validator class

public class RestaurantValidator {

public static boolean validateRestaurant(restaurant rest, List errors) {

    boolean valid=true ;
    if (Validator.isNull(rest.getName())) {
        errors.add("Name-required");
        valid = false;
    }
    if (Validator.isNull(rest.getLicensekey())) {
        errors.add("license-key-required");
        valid = false;
    }

following is my view.jsp code

Restaurant Name* " />

                                    <span class="help-block">help block</span>
                                </div>                                    
                            </div>
                            <div class="row-fluid">
                                <div class="span12">
                                    <label>License Key<span class="f_req">*</span></label>
                                   <liferay-ui:error key="license-key-required" message="license-key-required" />

                                    <input type="text" name="licensekey" class="span8" value="<%=restaurantOBJ.getLicensekey() %>"/>
                                </div>                                    
                            </div>

the error message is deisplaying on the redirected page with following way rather then on same page i want the error near textbox of name with the error of "Name_required"

The error message is displaying on the redirected page the following way rather then on same page, I want the error near the name-textbox with the error of "Name_required".

enter image description here

what I want is when name is blank then it should not submit the form and give error near text box of name in my view.jsp page.

1
can you also include the whole <form> and also the submit button code you have in the JSP? That might help. thanksPrakash K
one thing more..temporarily had used <aui:validator>User 1531343
You have again screwed up the formatting after I edited. :-) and again the <aui:form> is also gone. Please take a look at the preview once before finalizing your edit.Prakash K
oh.sorry..actually i just thinking that u suggest me to edit the code.thats yUser 1531343

1 Answers

0
votes

I would try to answer your question, try to set a actionResponse.setRenderParameter("valid", "NO"); in your addRestaurant method if the validation fails.

And get this parameter in the doView method and set the renderPage accordingly in this method: include(renderPage, renderRequest, renderResponse); at the end of the doView method.

If you can provide all the information in your question with nice formatting like the validator method, <aui:form> and the javascript method called on onSubmit then we can go ahead with looking for other solution.

In the mean-while you can try this out and see if it works. :-)