Reputation: 127
I have a problem in implementing a mechanism for checking the mandatory input fields in the JSF form using PrimeFaces are filled by the user or not before the submission of the form.
I have used required="true" mechanism in inputTexts for checking and firing a custom message before submitting for prompting the user to enter some value to the mandatory fields such as this:
<p:inputText id="exp" required="true" requiredMessage="#{lang.dailyCurrencyValues_exp_req_txt}" value="#{marketDataDefinitionProcesses.currencyType.explanation}"/>
My boss asked me to reset the page if the user successfully accomplished the submission and stay in the same page. For implementing this, I have used oncomplete="document.myForm.reset();" on the submission button which is successfully resetting the form and all the fields in the form.
However, if the user doesn't enter all the mandatory fields and press the submit button, the form is giving a message that is specified in the requiredMessage and resetting the form and all the elements in it. I want to prevent this situation, for resetting the values, if the submission fails. I tried to implement a JavaScript function that is trying to prevent to reset the form if the mandatory fields are not filled. However, again I face a problem regarding to the retrieval of the element value in my form.
<p:calendar id="dateValueId" value="#{marketDataDefinitionProcesses.currencyType.dateValue}" pattern="dd.MM.yyyy" required="true" requiredMessage="#{lang.dailyCurrencyValues_dateValueId_req_txt}"/>
I didn't succeed to retrieve the calendar element value by JavaScript.
And now, I am waiting for your opinions in solving this problem. I think, the solutions about retrieving the calendar value using JavaScript or any JSF/PrimeFaces element that retrieves the information about if the required="true" field/s are filled or not, are options.
Yours Sincerely...
Faruk
Upvotes: 2
Views: 2391
Reputation: 1
maybe you can try to this way
public class MarketDataDefinitionProcesses{
private CurrencyType currencyType;
Boolean createStatus;
public String create(){
if(service.checkData(currencyType)){
//insert data
//show statusMessage or not
createStatus=Boolean.TRUE;
reflesh(create,createStatus);
}
return null;
}
public void reflesh(CurrencyType preNextCreateCurrency,Boolean createStatus){
preNextCreateCurrency = new CurrencyType();
this.currencyType = preNextCreateCurrency ;
RequestContext rc = RequestContext.getCurrentInstance();
rc.update("form:id"); // if the inputText in the panelGrid, you can put the panelGrid id
createStatus = Boolean.FALSE;
}
}
when the user submit the data , the page need a new instance and use ReuqestContext to update the form or which part you need
Upvotes: 0
Reputation: 1108632
Send a redirect to the same view after post. It will create a brand new request with all input values blank or at least to their defaults as definied in a request/view scoped bean.
public String submit() {
// ...
return FacesContext.getCurrentInstance().getViewRoot().getViewId() + "?faces-redirect=true";
}
Upvotes: 2
Reputation: 9266
I think you can achieve your goal with PrimeFaces's RequestContext:
<h:form id="myForm">
...
</h:form>
@ManagedBean
@RequestScoped
public class MrBean {
public void doSomething() {
// Do your thing
RequestContext context = RequestContext.getCurrentInstance();
context.execute("document.getElementById("myForm").reset()");
}
}
Upvotes: 0