Permalink
Browse files

Bypass f:viewParam skipping (bean) validation and pre/post validate

events when parameter is not specified.
  • Loading branch information...
1 parent cbf2c72 commit f8f84aab2b7629e3673e4721c97b9ad864f555bc @BalusC BalusC committed Jul 16, 2014
@@ -22,13 +22,16 @@
import javax.faces.component.UIViewParameter;
import javax.faces.context.FacesContext;
import javax.faces.convert.ConverterException;
+import javax.faces.event.PostValidateEvent;
+import javax.faces.event.PreValidateEvent;
import org.omnifaces.util.MapWrapper;
/**
* <strong>ViewParameter</strong> is a component that extends the standard {@link UIViewParameter} and provides a
* stateless mode of operation and fixes the issue wherein null model values are converted to empty string parameters
- * in query string (e.g. when <code>includeViewParams=true</code>).
+ * in query string (e.g. when <code>includeViewParams=true</code>) and the (bean) validation never being triggered
+ * when the parameter is completely absent in query string, causing e.g. <code>@NotNull</code> to fail.
* <p>
* The standard UIViewParameter implementation calls the model setter again after postback. This is not always desired
* when being bound to a view scoped bean and can lead to performance problems when combined with an expensive converter.
@@ -39,6 +42,11 @@
* <code>null</code> or not. As converters by specification return an empty string in case of <code>null</code> value,
* this is being added to the query string as an empty parameter. This is not desired.
* <p>
+ * The standard UIViewParameter implementation uses an internal "is required" check when the submitted value is
+ * <code>null</code>, hereby completely bypassing the standard <code>UIInput</code> validation, including any bean
+ * validation annotations and even the {@link PreValidateEvent} and {@link PostValidateEvent} events. This is not
+ * desired.
+ * <p>
* You can use it the same way as <code>&lt;f:viewParam&gt;</code>, you only need to change <code>f:</code> to
* <code>o:</code>.
*
@@ -81,6 +89,10 @@ public void processDecodes(FacesContext context) {
@Override
public void processValidators(FacesContext context) {
if (!context.isPostback()) {
+ if (getSubmittedValue() == null) {
+ setSubmittedValue(""); // Workaround for it never triggering the (bean) validation when unspecified.
+ }
+
super.processValidators(context);
}
}
@@ -1766,7 +1766,8 @@ public class ValidateValuesBean implements MultiFieldValidator {
<![CDATA[
<strong>ViewParameter</strong> is a component that extends the standard {@link UIViewParameter} and provides a
stateless mode of operation and fixes the issue wherein null model values are converted to empty string parameters
- in query string (e.g. when <code>includeViewParams=true</code>).
+ in query string (e.g. when <code>includeViewParams=true</code>) and the (bean) validation never being triggered
+ when the parameter is completely absent in query string, causing e.g. <code>@NotNull</code> to fail.
<p>
The standard UIViewParameter implementation calls the model setter again after postback. This is not always desired
when being bound to a view scoped bean and can lead to performance problems when combined with an expensive converter.
@@ -1777,6 +1778,11 @@ public class ValidateValuesBean implements MultiFieldValidator {
<code>null</code> or not. As converters by specification return an empty string in case of <code>null</code> value,
this is being added to the query string as an empty parameter. This is not desired.
<p>
+ The standard UIViewParameter implementation uses an internal "is required" check when the submitted value is
+ <code>null</code>, hereby completely bypassing the standard <code>UIInput</code> validation, including any bean
+ validation annotations and even the <code>PreValidateEvent</code> and <code>PostValidateEvent</code> events. This is not
+ desired.
+ <p>
You can use it the same way as <code>&lt;f:viewParam&gt;</code>, you only need to change <code>f:</code> to
<code>o:</code>.
]]>

0 comments on commit f8f84aa

Please sign in to comment.