Tapestry 5 ByPass Validation - java

I want to ask about bypass validation in tapestry 5
In my form i have some field that have validation required. and i have two submit button. one if i click do some validation for save object. and the other must not validation the form or bypass the validation for add to detail object by Ajax (using zone)
Thank's B4

Though I don't use client validation by setting the t:clientvalidation="false" on the t:form, I believe you can bypass it with a cancel button next to your submit button as follows:
<input t:type="submit" t:mode="cancel" value="Cancel" t:id="cancel" />
<input t:type="submit" value="Submit" t:id="submit" />
Have a look at the component reference for submit. There it states "SubmitMode#CANCEL indicates the client-side validation should be omitted (though server-side validation still occurs)." So you will still need to stop the server side validation. You can do this with:
private boolean cancelCalled;
void onSelectedFromSubmit() {
cancelCalled = false;
}
void onSelectedFromCancel() {
cancelCalled = true;
}
#OnEvent(component = "theIdOfYourForm", value = EventConstants.VALIDATE)
private void validateForm() {
if(cancelCalled) {
newContactForm.clearErrors();
}
}

Trying disabling the input field using javascript on the client side. It works for me. And as joostschouten said, you still need to bypass your server-side validation.

Related

How to get the value of disabled check box in Spring MVC

I am working on Spring MVC application and I am having a problem of getting the value of the check box when it is disabled.
in report.jsp page :
<form:checkbox type="checkbox" path="corporateColumn" id="corporateColumn" value="true" checked="checked" disabled="true" />
in ReportForm.java :
boolean corporateColumn ;
public boolean isCorporateColumn() {
return corporateColumn;
}
public void setCorporateColumn(boolean corporateColumn) {
this.corporateColumn = corporateColumn;
}
In ReportController.java ;
boolean corporateColumn = reportDTO.isCorporateColumn(); // this evaluates to false
//Which expected as true when corporateColumn checkbox is checked
Everything works fine unless it is used as disabled="true" (/ disabled="${'true'}") for checkbox field.
I had the similar issue previously also when getting the value of a disabled textfield and overcome it by making the field readonly.
So I am not sure in Spring MVC whether it is not possible to get the value of input field when it is disabled.
Any guidance would be really appreciated.
Thanks!
It's sorry to say that but that's impossible.
Use developer tool(like in Chrome) to check posted data, you can confirm browser will not send your checkbox's data if it's disabled or unchecked.
Why don't you simply use 'readonly' instead of 'disabled'?
Disabled field data is not passed on to server.

Disable/enable commandButton depending on required fields of current form?

What is the best approach to disable a commandButton if the fields with required=true are not filled with values?
The button should be disabled on first page load, and only be enabled in case all required fields are fields with values.
By best approach i mean no javascript and minimum-code :-)
In addition it would be excellent if the commandButton is only enabled when all validators evaluate to true.
Using PF 3.2
EDIT
by best approach i also mean, it should only be evaluate on client-side
This is not possible for two reasons.
For client side validation, you would definitely require javascript.
The required attribute of components is stored server side ONLY, the client has no idea of which fields are required by default.
Without using required, you could achieve this in client-side as following. validateContent should contain the logic to disable the commandButton.
<h:inputText value="#{bean.text}" >
<pe:javascript event="keyup" execute="validateContent();"/>
</h:inputText>
If going to server is okay, then you could do this:
<h:inputText id="test1" value="#{bean.text}" required="true" immediate="true">
<f:ajax listener="#{bean.makeDisable()}" render="test2" />
</h:inputText>
</h:commandButton id="test2" value="commandButton1" disabled="#{bean.disable}" />
And in the bean:
private String text;
private boolean disable;
// setter & getter of text
public boolean isDisable()
{
return disable;
}
public void makeDisable(AjaxBehaviorEvent event)
{
if(text == null || text.equals(""))
this.disable=true;
else
this.disable=false;
}
This basically will load the commandButton disabled on initial load and it will only be enabled on entering any value in text field.
It is possible but I would hardly call it the "best way".
You would need to supply an ajax tag for change events on each field. Each field would have to be immediate to skip initial validation and process will need to be set to #this.
In an event listener you can check if values exist for each of the required fields and if so then set a boolean field that determines if the commandButton is disabled or not.
These ajax tags will need to render #this as well as the commandButton.
But even then there is a LOT of Javascript actually going on, just none that you would have to write directly.

Validation of (Measurement of units)

I have a text box that allow users to key in their desired unit,
and i would have to validate if this unit does exist.
Example 1: KM/H (Correct)
Example 2: KA/H (Wrong)
I know that emails address can be validated using codes like C#, JavaScript, Etc.
Is there any ways that I can validate (Units of measurement) through any codes, web service etc ? using Javascript, JSON?
P.s I am working on a JSP page :)
JavaScript is fully capable of validation (though it should not be used to validate at the expense of server-side validation, for security reasons). You can validate either by simple string checking (as below, which seems suitable to your case) or, for more complex, pattern-based validation, regular expressions.
Assumed HTML:
<form id='unit_form'>
<input type='text' name='unit' />
<input type='submit' value='go' />
</form>
Then, when the button is clicked (using jQuery...)
JS
$(function() {
var valid_units = ['KM/H', 'KM']; //etc...
$('#unit_form').on('submit', function() {
var unit = $('input[name=unit]').val();
if ($.inArray(unit, valid_units) == -1) {
alert('bad unit!');
return false; //cancel form submission
}
});
});
JS Fiddle
GNU publishes a nice little program called Units. It neatly does what you want.

Multiply submit in one form. More than 1 submit in one form. Or more then one form in 1 JSP

I'm doing web-application. And I have a problem when I edit data
I want to create smth like this
I hope you can help me to find way to do this. I found next variant but I don't know as people usually do this.
Multiply submit in one form.
More than 1 submit in one form.
Or more then one form in 1 JSP
And I should't use any framework. And without javascript.
Thanks
OK, If It helps to understand what I want to get on servlet
I show some part of selvlet
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String page = new String();
Command command = CommandFactory.getCommand(request);
page = command.execute(request);
if (page != null) {
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(page);
dispatcher.forward(request, response);
}
....
}
And CommandFactory
public static Command getCommand(HttpServletRequest request){
Command current = new NoCommand();
String action = request.getParameter("command");
if(action == null || action.isEmpty()){
return current;
}
try{
CommandEnum currentState = CommandEnum.valueOf(action.toUpperCase());
current = currentState.getCurrentCommand();
}
...
}
And CommandEnum
public enum CommandEnum {
EDIT_QUESTION {
{
this.command = new EditQuestionCommand();
}
};
}
And in particular command I do some business logic
Please, help me to find way getting from jsp value as command="delete". With single submit I use hidden field. How we can do the same for several button?
You can use any number of submit buttons in a single form. But name them uniquely so that you can identify easily which button is clicked. Server will not receive name, value pair of buttons that are not clicked. It becomes easy for you to find which button clicked is available as request parameter. Using that you can implement your requirement.
Sample HTML:
<form>
<input type="submit" name="s1" value="S1"/><br/>
<input type="submit" name="s2" value="S2"/>
</form>
On clicking any of these buttons you will see query string as either ?s1=S1 or ?s2=S2
UPDATE 1:
You can have same name for all submit buttons and to identify uniquely, they MUST have different values.
<input type="submit" name="modeOfAction" value="Delete"/><br/>
<input type="submit" name="modeOfAction" value="Update"/>
UPDATE 2:
If you really don't care what the value of each button, then it would be better using unique names for each of the submit buttons.
<input type="submit" name="delete" value="Удалить (Err! I don't know what this text is!)"/><br/>
<input type="submit" name="update" value="Update"/>
There was a time when indeed the optional JavaScript meant: gracefully accept when no JavaScript is present. With the small devices one might think this position might be strengthened again, but:
HTML forms are limited in their usage, as shown above a bit.
DOM usage, like AJAX calls updating part of the page, need JS.
JavaScript user interface possibilities (effects, table sorting, paging, themes, validation) with libraries like jQuery.
I still now a way to get a command=edit: write a servlet filter that translates request parameters. For instance translate a parameter command-NAME = TEXT into command=NAME.

Checking some mandatory input fields are inputted before resetting the form in JSF + PrimeFaces

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
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";
}
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()");
}
}
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

Categories

Resources