8

I would like to have an illustrative explanation about the difference between getValue() and getLocalValue() methods of UIInput components in the aspect of performing multiple field validation: jsf validate two fields in one time.

What is the difference in usage of this methods if the fields are already validated? The ValueHolder API documentation is not very helpful in answering this.

Community
  • 1
  • 1
1337
  • 338
  • 1
  • 6

1 Answers1

9

If the UIInput component has been validated beforehand and is marked invalid (i.e. isValid() method returns false), then the getLocalValue() returns null, but the getValue() returns the old model value, if any. If it's marked valid, then both returns the same value, namely the submitted, converted and validated value.

You'd like to add a check on isValid() beforehand. You may find the source code examples of the OmniFaces multiple field validators helpful. See the ValidateMultipleFields source code link at the bottom of the <o:validateEqual> showcase page.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • when generally we should use getValue()? i saw in a blog that we "must use getValue() during encoding", is that the only place where we should use it ? – Tarik Feb 01 '15 at 13:52
  • @Tarik: when it returns the value you actually need for the concrete functional requirement you had in mind but didn't tell about. The answer explains when exactly which value exactly it will return. – BalusC Feb 01 '15 at 15:19
  • Ok so i understand that it depends on each functionnal case we have, but i can't see it clearly because for local values i know that they are setted after validation phase (if succeeded), but for values i dont know wich they are setted? – Tarik Feb 01 '15 at 15:56
  • 1
    @Tarik: Oh, you mean the model values? That happens during update model values phase. See also a.o. http://stackoverflow.com/questions/4749451/difference-between-apply-request-values-and-update-model-values – BalusC Feb 01 '15 at 16:09
  • i am talking about the values from uicomponent.getvalues () – Tarik Feb 01 '15 at 16:13
  • 1
    @Tarik: during validation. See also the current question, the "See also" link and the `UIInput` source code: http://stackoverflow.com/questions/15691126/when-are-setvalue-and-setsubmittedvalue-called-on-uicomponent/ – BalusC Feb 01 '15 at 16:18
  • Sorry @BalusC, but when i saw the UIInput source code i more confused, i thought that after validation its localValue which must be set, now i see that in the validate method we are calling setValue(newValue); so where we call the setLocalValue for UIInput ? – Tarik Feb 01 '15 at 18:10
  • 1
    @Tarik: There's no `setLocalValue()`. The `setValue()` sets the local value (i.e. in component state), which gets reset during update model values phase (the `updateModel()` method). The `getValue()` returns the local value if set, else the model value. Again, see the source (and javadoc). – BalusC Feb 01 '15 at 22:22
  • i didn't understood the source code , but now after your explanations i went back to the code and its very clear now.... thanks again :) – Tarik Feb 01 '15 at 22:46
  • @BalusC: Could you elaborate on this line - "If the UIInput component has been validated beforehand...". Beforehand in plain english means IN ADVANCE.How can a component gets validated beforehand? – Farhan stands with Palestine Apr 28 '16 at 07:20
  • Beforehand means "before the current moment", in this context it means "has taken place in a previous postback request". – BalusC Apr 28 '16 at 07:22
  • @BalusC: So to consolidate, isLocalValueSet() will be false, and getValue() will return the super.getValue() which is nothing but the model value. Right. – Farhan stands with Palestine Apr 28 '16 at 07:32
  • 1
    @Shirgil: keep in mind that submitted value, "is valid", local value and "is local value set" are saved in JSF view state. I.e. they are effectively view scoped. – BalusC Apr 28 '16 at 07:34