001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.taglib;
016    
017    import com.liferay.portal.kernel.servlet.taglib.aui.ValidatorTag;
018    import com.liferay.portal.kernel.util.ListUtil;
019    import com.liferay.taglib.aui.ValidatorTagImpl;
020    import com.liferay.taglib.util.IncludeTag;
021    
022    import java.util.HashMap;
023    import java.util.List;
024    import java.util.Map;
025    
026    import javax.servlet.http.HttpServletRequest;
027    import javax.servlet.jsp.JspException;
028    
029    /**
030     * @author Julio Camarero
031     * @author Jorge Ferrer
032     * @author Brian Wing Shun Chan
033     */
034    public abstract class BaseValidatorTagSupport extends IncludeTag {
035    
036            public void addRequiredValidatorTag() {
037                    ValidatorTag validatorTag = new ValidatorTagImpl(
038                            "required", null, null, false);
039    
040                    addValidatorTag("required", validatorTag);
041            }
042    
043            public void addValidatorTag(
044                    String validatorName, ValidatorTag validatorTag) {
045    
046                    if (_validatorTags == null) {
047                            _validatorTags = new HashMap<>();
048                    }
049    
050                    _validatorTags.put(validatorName, validatorTag);
051            }
052    
053            @Override
054            public int doEndTag() throws JspException {
055                    updateFormValidatorTags();
056    
057                    return super.doEndTag();
058            }
059    
060            public abstract String getInputName();
061    
062            @Override
063            protected void cleanUp() {
064                    super.cleanUp();
065    
066                    _validatorTags = null;
067            }
068    
069            protected Map<String, ValidatorTag> getValidatorTags() {
070                    return _validatorTags;
071            }
072    
073            protected void updateFormValidatorTags() {
074                    if (_validatorTags == null) {
075                            return;
076                    }
077    
078                    HttpServletRequest request =
079                            (HttpServletRequest)pageContext.getRequest();
080    
081                    Map<String, List<ValidatorTag>> validatorTagsMap =
082                            (Map<String, List<ValidatorTag>>)request.getAttribute(
083                                    "aui:form:validatorTagsMap");
084    
085                    if (validatorTagsMap != null) {
086                            List<ValidatorTag> validatorTags = ListUtil.fromMapValues(
087                                    _validatorTags);
088    
089                            validatorTagsMap.put(getInputName(), validatorTags);
090                    }
091            }
092    
093            private Map<String, ValidatorTag> _validatorTags;
094    
095    }