< f:validateLength>的禁用属性不评估请求参数 [英] Disabled attribute of <f:validateLength> does not evaluate request parameters

查看:79
本文介绍了< f:validateLength>的禁用属性不评估请求参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在按下按钮添加和按钮删除时禁用验证,所以我尝试了

i want to disable the validation when the button add and button delete is pressed ,so i tried this

<f:validateLength maximum="500" disabled="#{!empty param['mainForm:add_Button'] or !empty param['mainForm:delete_Button']}" />

按钮添加已被禁用,但是按钮删除没有.而且我不知道出什么问题了! 这是我的代码,你们可以帮我检查一下.对不起,我的英语不好

The button add have already been disabled , however the button delete havent . And i dont know what's the problem ! This is my code , Can you guys help me to check it .Sorry for my bad english

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
            xmlns:h="http://java.sun.com/jsf/html"
            xmlns:f="http://java.sun.com/jsf/core"
            xmlns:ui="http://java.sun.com/jsf/facelets" template="../main.xhtml">
<ui:define name="content">


    <div id="question">

        <div id="mainForm" class="form">

            <span>ID:#<h:outputText value="#{editQuestion.questionData.question.id}" /></span>
            <br />
            <div>
                <span><h:outputText value="#{i18n['admin.edit.questiontitle']}" />:</span>
                <h:inputTextarea id ="title" rows="3" style="width: 100%" value="#{editQuestion.questionData.title}" required="#{!empty param['mainForm:Save_btn']}" label="Question Title">
                    <f:validateLength maximum="500" disabled="#{!empty param['mainForm:add_Button'] or !empty param['mainForm:delete_Button']}" />
                </h:inputTextarea> 
                <h:message for="title" style="color: red"/>
            </div>
            <div>
                <span><h:outputText value="#{i18n['admin.edit.questiontext']}" />:</span><br />
                <h:inputTextarea rows="6" id="name" style="width: 100%" value="#{editQuestion.questionData.text}" required="#{!empty param['mainForm:Save_btn']}" label="Question Text">
                    <f:validateLength maximum="1000" disabled="#{!empty param['mainForm:add_Button'] }"/>
                </h:inputTextarea>
                <h:message for="name" style="color:red"/>
            </div>

            <div class="list">

                <div class="title"><h:outputText value="#{i18n['admin.edit.answers']}" /></div>
                <div class="btn_add">
                    <h:commandButton image="/resources/imgs/#{editQuestion.buttonAdd}" alt="add" id="add_Button"
                                     title="#{i18n['img.add']}"  action="#{editQuestion.addAnswer}" 
                                     disabled="#{!editQuestion.possibleToAdd}">
                        <f:param name="id" value="#{editQuestion.id}"/>
                    </h:commandButton>
                </div>
                <h:dataTable cellspacing="0" value="#{editQuestion.answersData}" var="answer">
                    <h:column>
                        <f:facet name="header">ID</f:facet>
                        <h:outputText value="#{answer.answer.id}" />
                    </h:column>

                    <h:column>
                        <f:facet name="header"><h:outputText value="#{i18n['admin.edit.rightanswer']}"  /></f:facet>
                        <h:selectBooleanCheckbox value="#{answer.answer.isRight}"/>
                    </h:column>

                    <h:column>
                        <f:facet name="header"><h:outputText value="#{i18n['admin.edit.answers']}"  /></f:facet>
                        <h:inputTextarea id="answer" rows="3" cols="40" value="#{answer.text}" required="#{!empty param['mainForm:Save_btn']}" label ="Answer">
                            <f:validateLength maximum="500" disabled="#{!empty param['mainForm:add_Button'] }"/>
                        </h:inputTextarea>
                        <div>
                           <h:message for="answer" style ="color:red"/>  
                        </div>

                    </h:column>

                    <h:column>
                        <h:commandButton image="/resources/imgs/#{editQuestion.buttonDelete}" 
                                         action="#{editQuestion.deleteAnswer(answer)}" disabled="#{!editQuestion.possibleToDelete}"
                                         alt="delete" id="delete_Button" title="#{i18n['img.delete']}">
                            <f:param name="id" value="#{editQuestion.id}"/>
                        </h:commandButton>
                    </h:column>
                </h:dataTable>
            </div>

            <div class="btn_block" style="float: inherit" >
                <center>
                    <h:commandButton value="#{i18n['btn.save']}" styleClass="button bg_green" id="Save_btn"  action="#{editQuestion.saveAction}">
                        <f:param name="id" value="#{editQuestion.id}" />
                    </h:commandButton>

                    <h:commandButton value="#{i18n['btn.cancel']}" styleClass="button bg_red" action="#{editQuestion.calcelAction}">
                        <f:param name="id" value="#{editQuestion.id}" />
                    </h:commandButton>
                </center>
            </div>
        </div>
    </div>


</ui:define>

推荐答案

<f:validateLength>是标记处理程序,而不是UI组件.根据规范,所有标记处理程序属性都是在视图构建时(而不是视图渲染时)评估的.因此,该属性将在同一视图的整个生命周期中保持与首次构建该视图时相同的值.

The <f:validateLength> is a tag handler, not an UI component. All taghandler attributes are by specification evaluated during view build time, not during view render time. So the attributes will throughout the lifetime of the same view keep the same value as it was when the view was built for the first time.

您基本上需要创建一个自定义验证器,该验证器检查validate()方法中的请求参数,然后委派给标准LengthValidator类.

You basically need to create a custom validator which checks the request parameter inside the validate() method and then delegates to the standard LengthValidator class.

<f:validator validatorId="delegateLengthValidator" />
<f:attribute name="maximum" value="1000" />
<f:attribute name="buttonId" value="mainForm:add_Button" />

使用

String buttonId = component.getAttributes().get("buttonId");

if (!context.getExternalContext().getRequestParameterMap().containsKey(buttonId)) {
    LengthValidator validator = new LengthValidator();
    validator.setMaximum(Integer.valueOf(component.getAttributes().get("maximum")));
    validator.validate(context, component, value);
}

请注意, OmniFaces 最近添加了一个新的<o:validator>标记,该标记可以完全解决以下问题:

Note that OmniFaces has recently added a new <o:validator> tag which should solve exactly this problem as follows:

<o:validator validatorId="javax.faces.Length" maximum="1000" disabled="#{!empty param['mainForm:add_Button']}" />

此处中查看展示示例.

这篇关于&lt; f:validateLength&gt;的禁用属性不评估请求参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆