stwissel
stwissel

Reputation: 20384

getSubmittedValue() vs. getValue()

Is that correct: When I query a value before validation (or if validation failed) I have to use getSubmittedValue();. Once the value is validated, even if I query it in another validation later in the page/control I have to use .getValue(); since getSubmittedValue(); returns null after successful validation?

Upvotes: 1

Views: 4164

Answers (3)

Per Henrik Lausten
Per Henrik Lausten

Reputation: 21709

This xsnippet makes it easier to handle this. It allows you to just call getComponentValue("inputText1") to get either value or submittedValue.

Here's the function for reference:

function getComponentValue(id){
  var field = getComponent(id);
  var value = field.getSubmittedValue(); 
  if( null == value ){ 
         // else not yet submitted 
         value = field.getValue(); 
  }
 
  return value
}

Upvotes: 3

Tim Tripcony
Tim Tripcony

Reputation: 8086

There's a slightly easier way: if you're just expecting a simple single-value String, just call:

var compare = firstField.getValueAsString();

Otherwise, call:

var compare = com.ibm.xsp.util.FacesUtil.convertValue(facesContext, firstField);

The former calls the latter anyway, but is obviously a terser syntax. This does what you're looking for and more:

  1. If the value hasn't yet been validated, returns the submitted value
  2. If validation has already passed, returns the value after it's already been processed by any converters and / or content filters, so particularly in cases where you're trying to compare two field values, this should ensure that both values have been properly trimmed, etc., and is therefore less likely to return a false positive than just comparing the raw submitted values.

Upvotes: 2

stwissel
stwissel

Reputation: 20384

Found the answer here. So when you want to ensure that 2 text fields have the same value (use case: please repeat your email) and the first box already has a validation that might fail, you need to use submittedValue unless it is null, then you use the value. Code in the validation expression for the second field looks like this:

var firstField = getComponent("inputText1");
var compare = firstField.getSubmittedValue() || firstField.getValue();
compare == value;

You have to love it.

Upvotes: 0

Related Questions