7
votes

How to make some button which can skip web required validation(but still I want to process all data, so immediate and so on cannot be true).

Important is that it must be universal. At the moment I am using in every required field condition with some request param. Code example below

<p:inputText value="#{cc.attrs.data.exampleData1}" 
             required="#{param['onlySave'] == null}"/>
<p:inputText value="#{cc.attrs.data.exampleData2}" 
             required="#{param['onlySave'] == null}"/>
<p:inputText value="#{cc.attrs.data.exampleData3}" 
             required="#{param['onlySave'] == null}"/>

<p:commandButton value="Zapisz zmiany"
                 action="#{cc.attrs.controller.save()}"
                 update="@form">
   <f:param name="onlySave" value="true"/>
</p:commandButton>

This solution is fine cause I can in every page just add this param to button and it skips validation, but when my save button not making any redirect, in case of failed some java validation in save method, I am just adding some message without redirect and then I lost all required styles from inputs.

Is there any possibility to set onlySave param to null in save method when validation failed or maybe some better solutions?

Edit: Balus answer great, but with bean validation like:

@Pattern(regexp = "^([^0-9]*)$", message = "only non numbers")
String field;

It processes to bean all data beyond that field. The best would be ignore only required field property, not validation etc.

Edit2:

<tr>
    <td class="label">
        <p:outputLabel id="label" for="#{cc.attrs.componentId}" value="#{cc.attrs.label}"/>
    </td>
    <td class="value">
        <cc:insertChildren/> --here component with componentId
    </td>
</tr>
<tr class="errorMessage">
    <td class="label"/>
    <td class="value">
        <p:message id="error" for="#{cc.attrs.componentId}" />
    </td>
</tr>
3
Your concrete functional requirement is bit hard to digest, but I believe you're looking for showcase.omnifaces.org/taghandlers/ignoreValidationFailed Is this true? - BalusC
wow, its almost perfect, only one problem. When I am using this tag and write in some field for example numbers in non number field bean: @Pattern(regexp = "^([^0-9]*)$", message = "numbers!!") it not process data as it suposed to be. But every other field is processed. It even not giving me any info. - user2771738
So, your concrete functional requirement is to set a common attribute on all components of the given type from a single place? Perhaps you need showcase.omnifaces.org/taghandlers/massAttribute? - BalusC
am I able to change value of massive argument when I click some specific button like with the f:param? And then can I change its value again in action method used in this specifiv button, after failure custom validation ? - user2771738
If it's an EL expression referencing the desired value, yes. - BalusC

3 Answers

0
votes

Please see below modified code for required fields. You can use #{empty param.onlySave} for the fields which you want to skip the validation and as per your requirements.

<p:inputText value="#{cc.attrs.data.exampleData1}" 
             required="#{not empty param.onlySave}"/>
<p:inputText value="#{cc.attrs.data.exampleData2}" 
             required="#{not empty param.onlySave}"/>
<p:inputText value="#{cc.attrs.data.exampleData3}" 
             required="#{not empty param.onlySave}"/>

<p:commandButton value="Zapisz zmiany"
                 action="#{cc.attrs.controller.save()}"
                 update="@form">
   <f:param name="onlySave" value="true"/>
</p:commandButton>
0
votes

So far the best solution i figured out is:

//p:label and p:message with property for=someId for each input above
<p:inputText id="someId" value="#{cc.attrs.data.exampleData1}" 
         required="#{param['onlySave'] == null}"/>
<p:inputText value="#{cc.attrs.data.exampleData2}" 
         required="#{param['onlySave'] == null}"/>
<p:inputText value="#{cc.attrs.data.exampleData3}" 
         required="#{param['onlySave'] == null}"/>

<p:commandButton value="save only"
             action="#{cc.attrs.controller.save()}"
             update="@(.ui-message,.ui-outputlabel)">
   <f:param name="onlySave" value="true"/>
</p:commandButton>

<p:commandButton value="accept button"
             action="#{cc.attrs.controller.accept()}"
             update="@form">
</p:commandButton>

The save button just updates only all errors and labels, so I am able to see converters errors etc, but I dont lost required styles on inputs. In this example I just lost red color of input borders.

If u re using * in labels for required fields, dont update .ui-outputlabel

-1
votes

I figured out a solution :
JSF page

<h:form id="form" prependId="false">
            <c:if test="#{param['onlySave'] eq  true}">
                <c:set  value="#{false}"  target="#{myBean}" property="required"/>
            </c:if>
            <p:inputText value="#{myBean.required}"/>
            <p:growl id="msgs" showDetail="true" />
            <p:inputText id="data1" value="#{myBean.data1}"   
                         required="#{myBean.required}" />
            <p:inputText  id="data2" value="#{myBean.data2}" 
                          required="#{myBean.required}"/>
            <p:inputText id="data3" value="#{myBean.data3}" 
                         required="#{myBean.required}"/>

            <p:commandButton value="Zapisz zmiany"
                             action="#{myBean.save}" />
        </h:form>

Bean

@ManagedBean
@ViewScoped
public class MyBean implements  Serializable{

    private String data1, data2, data3;
    private boolean required;

    @PostConstruct
    public void init(){
        required = true;
    }

    public void save() {
        System.out.println(data1+", "+data2+", "+data3);
        required = false;
        //FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Info","Saved"));
       //  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("onlySave", "false");// FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("onlySave"));
    }

// ... Getters & Setters
}