f:validateLongRange. Validation huge numbers - validation

I am using JSF 2 and primefaces 3.5. I have an inputText which must be a number between Long.MIN_VALUE and Long.MAX_VALUE.
<p:inputText id="startRange" value="#{attributeBean.attribute.startRange}">
<f:convertNumber />
<f:validateLongRange minimum="#{attributeBean.minimumValue}"
maximum="#{attributeBean.maximumValue}"/>
</p:inputText>
In attributeBean:
public Long getMinimumValue(){
return Long.MIN_VALUE;
}
public Long getMaximumValue(){
return Long.MAX_VALUE;
}
When I enter a huge number like 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 validation message doesn't appear. If come back to this form in an inputText field is 9,223,372,036,854,775,807 value. Can I get a validation message?

Well this is do to the f:converter. The converter tries to convert 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
To a long. The max of a long = 9,223,372,036,854,775,807 so the converter works well :-)
But this behavior is not what you want i guess. So you can validate it yourself on this way. Add a actionListener to your command button: actionListener="#{attributeBean.validateLong}" and remove this:
<f:validateLongRange minimum="#{attributeBean.minimumValue}"
maximum="#{attributeBean.maximumValue}"/>
and add a method like this in your bean:
public void validateLong()
{
if(startRange.compareTo(Long.MIN_VALUE) > 0 && startRange.compareTo(Long.MAX_VALUE) < 0)
{
//Do bussiness logic
}
else
{
//Throw message
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Not good!"));
}
}
Keep in mind that your range with above check is between: -9223372036854775807 and 9223372036854775806. But i don't know if thats a problem.

This problem is solved with help <f:converter converterId="javax.faces.Long"/>

Related

How to use modifers like Ctrl or Shift with jsf.2.x and <f:ajax>? [duplicate]

I have a JSF ajax keyup event linked to an event listner in a backing bean.
The code in the JSF file is like below.
<h:inputText id="txtDescription" value="#{institutionController.current.description}" disabled="#{institutionController.modifyControlDisable}" >
<f:ajax event="keyup" listener="#{institutionController.changeDetailsEvent}" />
</h:inputText>
The code in the backing bean is like below.
public void changeDetailsEvent(AjaxBehaviorEvent event) {
}
I want to achieve different logic depending on the key presses, like shown is pseudocode below.
public void changeDetailsEvent(AjaxBehaviorEvent event) {
If (event.key = Key.enter) {
do something;
} else if (event.key = Key.Escape) {
so something else;
} else {
do nothing;
}
}
Can someone please tell me how this is done in the backing bean?
The AjaxBehaviorEvent doesn't contain any information about the JavaScript event object. You need to pass the desired information along yourself. This can be achieved by a hidden input field whose value is to be prefilled by JavaScript. For example,
<h:inputText value="#{bean.input}" onkeyup="document.getElementById('#{keyCode.clientId}').value=event.keyCode">
<f:ajax event="keyup" execute="#this keyCode" listener="#{bean.listener}" />
</h:inputText>
<h:inputHidden id="keyCode" binding="#{keyCode}" value="#{bean.keyCode}" />
(please note that the id of the hidden field is included in execute so that it get submitted along on the ajax request, please also note that the binding is used to be able to dynamically obtain the generated client ID in document.getElementById() in order to set the key code value, you could alternatively also hardcode the client ID if it's fixed)
with
private String input;
private int keyCode;
public void listener() {
switch (keyCode) {
case 13:
// Enter key was pressed.
break;
case 27:
// Escape key was pressed.
break;
default:
// Other key was pressed.
break;
}
}
You can find an overview of all valid keyCode values in the Mozilla DOM reference.

Get the validation message for a specific component

<h:inputText id="myInputText"
title="The text from validation message here"
style="#{component.valid? '' : 'border-color:red'}"
validator="#{MyBean.validate}"
required="true"
requiredMessage="required"
value="#{MyBean.value} />
<p:message for="myInputText" display="text"/>
Since I want to custom the looking for a failed validation in an inputText compoment and I know that it is possible to know whether the component was successfully validated or not, I would like to know if it is viable and how I can get the validation message, in order to display it as the tittle of my inputText component.
The problem you will have with what you're planning is that a single component can have more than one message queued. What are you going to do then? For demonstration purposes, you can use
<h:inputText id="myInputText"
title="#{facesContext.getMessageList('myInputText').get(0)}"
style="#{component.valid ? '' : 'border-color:red'}"
validator="#{MyBean.validate}"
required="true"
requiredMessage="required"
value="#{MyBean.value}" />
EDIT : You should just move the logic into your backing bean:
Implement a method that'll pull the detail from an available FacesMessage list, given a clientId
public String getComponentMessageDetail(String clientId) {
String detail = null;
FacesContext ctxt = FacesContext.getCurrentInstance();
List<FacesMessage> componentMessages = ctxt.getMessages(clientId);
if (componentMessages != null && componentMessages.isEmpty() == false) {
//returns the detail, from only the first message!
detail = componentMessages.get(0).getDetail();
}
return detail;
}
Use the utility method in your view
<h:inputText id="myInputText"
title="#{MyBean.getComponentMessageDetail('myInputText')}"
style="#{component.valid ? '' : 'border-color:red'}"
validator="#{MyBean.validate}"
required="true"
requiredMessage="required"
value="#{MyBean.value}" />
How about this java method
public String getComponentMessageDetail(String cid){
FacesContext ctxt = FacesContext.getCurrentInstance();
Iterator<FacesMessage> cm = ctxt.getMessages(cid);
List<String> msg = new ArrayList<>();
while(cm.hasNext()) {
msg.add(cm.next().getDetail());
}
return String.join(" | ", msg);
}
to show everything what's in the message cache?
Also in xhtml
<h:inputText id="myInputText" title="#{MyBean.getComponentMessageDetail('yourFormId:myInputText'}" style="#{component.valid? '' : 'border-color:red'}" validator="#{MyBean.validate}" required="true" requiredMessage="required" value="#{MyBean.value} />
it might be useful to put the name of your form-id in front of the input control's id. Otherwise the message list might have zero items, although there are some.
Here is another way to quickly show validation messages: h:messages

Ajax for valueChangeListener

I'm using the p:ajax listener to handle value change events (because valueChangeListener is launched on form submit):
<p:ajax event="change" listener="#{bean.onNameChanged}"/>
Handle method:
public void onNameChanged(final AjaxBehaviorEvent event)
The problem is, I can't find in AjaxBehaviorEvent nor its class hierarchy the place to read the old value of the input. Neither could I find hint in google, how to get the old value...
How to access the old value in the p:ajax onChange event?
The problem is, I can't find in AjaxBehaviorEvent nor its class hierarchy the place to read the old value of the input. Neither could I find hint in google, how to get the old value...
Use a valueChangeListener.
Unfortunatelly, valueChangeListener is invoked before p:ajax, so I don't have actual data from forms in that method, so in theory I could use valueChangeListener to remember the old value and then wait for p:ajax to process...
Queue the value change event to the invoke application phase.
public void valueChangeListenerMethod(ValueChangeEvent event) {
if (event.getPhaseId() != PhaseId.INVOKE_APPLICATION) {
event.setPhaseId(PhaseId.INVOKE_APPLICATION);
event.queue();
return;
}
// Do your original job here.
// It will only be invoked when current phase ID is INVOKE_APPLICATION.
}
The ValueChangeListener should work this way:
The view:
<h:form>
<h:inputText value="#{sessionBean.hello}"
valueChangeListener="#{sessionBean.valueChangeListener}">
<p:ajax/>
</h:inputText>
</h:form>
The bean:
public void valueChangeListener(ValueChangeEvent e) {
System.out.println("valueChangeListener invoked:"
+ " OLD: " + e.getOldValue()
+ " NEW: " + e.getNewValue());
}
The above code will print if I change the text field from "hello" to "world":
valueChangeListener invoked: OLD: hello NEW: world
You could try the following:
Implement the value change event in your bean
public void processValueChange(ValueChangeEvent e){
//foo the bar
}
Define a valueChangeListener on your selection component
<p:selectOneMenu value="#{yourBean.value}" onchange="submit()" valueChangeListener="{#yourBean.processValueChange}">
The key piece there is the submit() bit that processes the enclosing form on change of the value. You can then getNewValue() and getOldValue() as necessary.
EDIT: Now that I think about it, I see no reason why you cannot leave your setup as-is and simply define the valueChangeListener. It should still be processed during the change event in the <p:ajax/>, in fact, it will be processed before the listener for the ajax event itself.
you can use this:
public void onNameChanged(AjaxBehaviorEvent event)
{
String myVal = (String) ((UIOutput) event.getSource()).getValue();
System.out.println("myVal: " + myVal);
}
Workaround is possible (tested with Primefaces 10):
<p:inputText id="name" value="bean.name">
<p:ajax event="valueChange" update="name"
listener="#{bean.onNameChanged}"
onstart="cfg.ext={params:[{name:'oldValue', value:'#{bean.name}'}]};"/>
</p:inputText>
update="name" is important, to get each time the new value into the javascript event handler.
Bean Method:
public void onNameChanged(final AjaxBehaviorEvent event) {
String oldValue = getFacesContext().getExternalContext().getRequestParameterMap()
.get("oldValue");
//Do with oldValue, whatever you want
}

<f:validator disabled flag not working

I've created a custom validator for my project, it simply checks the select ones value and 'validates' the value is not '0'. We have a standard (I'm sure not uncommon) of manually setting the first value of our selectOneMenu compents to:
<f:selectItem itemValue="0"
itemLabel="-- Select One --"/>
Which works fine, but then makes the component always pass the required check. So this validator simply treats this value as if there was no selection made.
SelectOneMenu example:
<h:selectOneMenu id="eligibility"
value="#{reg.eligibility}"
required="#{reg.fieldsRequired}">
<f:selectItem itemValue="0"
itemLabel="-- Select One --"/>
<f:selectItems value="#{reg.eligibilityList}" />
<f:validator validatorId="selectOneValidator"
disabled="#{!reg.fieldsRequired}"/>
Custom Validator:
#FacesValidator("selectOneValidator")
public class SelectOneValidator implements Validator {
#Override
public void validate(FacesContext context, UIComponent uiComponent, Object o) throws ValidatorException {
String val = null;
if (uiComponent instanceof HtmlSelectOneMenu) {
HtmlSelectOneMenu oneMenu = (HtmlSelectOneMenu) uiComponent;
if (oneMenu.isRequired() && !oneMenu.isDisabled()) {
if (o instanceof String) {
val = (String) o;
} else if (o instanceof Number) {
val = String.valueOf(o);
}
if ("0".equals(val)) {
FacesMessage msg = new FacesMessage();
msg.setSummary("Please select a value from the list.");
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(msg);
}
}
}
}
}
This has worked just fine thus far, however, the current use-case I'm running into trouble.
My page has multiple SelectOneMenus one of which toggles the required and disabled attributes via an ajax call for the page. I'm not having any issues with the required="#{reg.fieldsRequired}", however; the disabled="#{!reg.fieldsRequired}" attribute on my custom validator does not seem to make a difference. I'm just thinking out loud, but when the page first loads the #{reg.fieldsRequired} expression is false. If I then change the SelectOneMenu to set this boolean value to true, then press the submit button, the disabled attribute doesn't seem to have been set. I wondered if this is simply a ajax issue and that all of components simply needed to be re-renderd so I added the #form in my ajax call: <a4j:ajax render="#form" listener="#{reg.saveActionChanged}"/> in hopes that would fix the problem, but no difference in the behavior.
Environment:
JSF 2.0.3
Tomcat 6.0.14
ajax call being made with RichFaces 4.0
Any help is most appreciated!
Sorry, I can't reproduce your problem with JSF 2.1.3. The <f:validator disabled> works as expected.
However, as a completely different alternative, you can in this particular case just make use of the standard required validator. You only need to set the item value to #{null} instead of 0.
<f:selectItem itemValue="#{null}" itemLabel="-- Select One --"/>
This way you don't need the custom validator. The message can be set as requiredMessage attribute of the input component.
<h:selectOneMenu requiredMessage="Please select a value from the list.">

validating decimals inputs in JSF

I want to validate an input as an integer. Thus, any value with decimal fractions should be filtered. But I don't know how to achieve that. I have tried many things, but with the code below, if the input is "61.2", the value is converted to 61, just truncating decimal part. But I want to force a validation error. I am sure I can do it without a custom validator. Thanks
<p:inputText size="5" value="#{bean.intValue}" converter="#{Integer}">
<f:convertNumber pattern="#0" integerOnly="true" maxFractionDigits="0"/>
</p:inputText>
That's not possible as validation runs after conversion. You'd basically need to bind it to a String property instead of an Integer one in order to validate the unconverted value. You'd need to convert it afterwards in the property setter or the managed bean action method. So, if you make it a String property, you could use <f:validateRegex> for this.
<h:inputText value="#{bean.input}" validatorMessage="Please enter digits only">
<f:validateRegex pattern="\d*" />
</h:inputText>
Alternatively, and IMO better than manually converting it in the setter or action method, you could bring in a custom converter which extends the JSF standard IntegerConverter and validate the pattern in there right before conversion takes place.
#FacesConverter("digitsOnly")
public class DigitsOnlyConverter extends IntegerConverter {
#Override
public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
if (!value.matches("\\d*")) {
throw new ConverterException();
}
return super.getAsObject(context, component, value);
}
}
with
<h:inputText value="#{bean.input}" converterMessage="Please enter digits only">
<f:converter converterId="digitsOnly" />
<h:inputText>
place minFractionDigits = "0" and pattern=" # # #"

Resources