3

ボタンの追加と削除が押されたときに検証を無効にしたいので、これを試しました

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

ボタンの追加はすでに無効になっていますが、ボタンの削除は無効になっています。そして、私は何が問題なのかわかりません!これは私のコードです、あなたたちは私がそれをチェックするのを手伝ってくれますか?私の悪い英語でごめんなさい

<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>

4

1 に答える 1

1

これ<f:validateLength>はタグハンドラーであり、UIコンポーネントではありません。すべてのtaghandler属性は、ビューのレンダリング時にではなく、ビューのビルド時に評価される仕様によるものです。したがって、属性は、同じビューの存続期間を通じて、ビューが最初に作成されたときと同じ値を保持します。

基本的に、メソッド内のリクエストパラメータをチェックしてvalidate()から、標準LengthValidatorクラスに委任するカスタムバリデーターを作成する必要があります。

<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>この問題を次のように正確に解決する新しいタグを追加したことに注意してください。

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

こちらのショーケースの例をご覧ください

于 2012-05-25T12:25:54.213 に答える