You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pivot.apache.org by Greg Brown <gk...@mac.com> on 2009/03/26 14:26:06 UTC

Validation classes

Hi Noel,

While attempting to add a validator example to the Kitchen Sink demo, I noticed a couple of things that still need a bit more work:

1) The validation classes need more Javadoc. I'll admit that we're pretty lax about method Javadoc a lot of the time, but we do try to make sure that every class has at least a minimal description. Otherwise, the documentation tends to look a bit unprofessional.

2) Each concrete validator should have a no-arg constructor and bean properties for configuring its behavior so it can be used in WTKX. For example, RegexTextValidator should have a "pattern" property:

Pattern getPattern()
void setPattern(Pattern pattern)
void setPattern(String pattern)

The string setter allows us to use it from WTKX like this:

<TextInput>
    <validator>
        <validation:RegexTextValidator pattern="[0-9]"/>
    </validator>
</TextInput>

WTKX automatically handles setters for primitive types (via BeanDictionary), so you don't need to provide String overloads for those. However, any non-primitive setters will require a String overload.

We'd very much like to get these updates in before we release version 1.1. Do you think you will have time to do it in the very near future?

Thanks,
Greg



Re: Validation classes

Posted by Greg Brown <gk...@mac.com>.
><TextInput>
>   <validator>
>       <validation:RegexTextValidator>
>           <pattern><![CDATA[
>               *(["'])(?:\\\1|.)*?\1*
>          ]]></pattern>
>       <validation:RegexTextValidator>
>   </validator>
></TextInput>

I just applied Noel's patch, and this works. You can see an example of it in the WTKX for the Kitchen Sink demo (tutorials/src/pivot/tutorials/text.wtkx).

G



Re: Validation classes

Posted by Greg Brown <gk...@mac.com>.
>My proposal:
><TextInput>
>   <validator>
>       <validation:RegexTextValidator>
>           <validation:pattern><![CDATA[
>*(["'])(?:\\\1|.)*?\1*
>          ]]></validation:pattern>
>       <validation:RegexTextValidator>
>   </validator>
></TextInput>

This should work (the property element doesn't need the "validation" prefix):

<TextInput>
   <validator>
       <validation:RegexTextValidator>
           <pattern><![CDATA[
               *(["'])(?:\\\1|.)*?\1*
          ]]></pattern>
       <validation:RegexTextValidator>
   </validator>
</TextInput>

However, support for setting property values via sub-elements is still fairly new, so there may be some unforeseen issues. I'll try it once Noel's updates are in.

G



Re: Validation classes

Posted by Christopher Brind <br...@brindy.org.uk>.
Do you know what I hate about Regular Expressions in most languages?  The
fact that you have to escape things.  Regular Expressions are complicated
enough without then having to worry about escaping.

Flex doesn't have this problem though as you can code a regular expression
in-line.  It recognises anything starting and ending with a solidus (forward
slash) as a regular expression and in the background (pre-compiler stage I
suppose) converts it to RegExp object.

http://livedocs.adobe.com/flex/3/langref/RegExp.html

Obviously we're not having any precompiler stage in Pivot, but since we're
using XML would it be possible to allow the pattern to be specified in a
CDATA block?  Then there's no escaping required.

For example, lets say we want the user to enter a quoted string, the regex
would be:
(["'])(?:\\\1|.)*?\1

(from : http://blog.stevenlevithan.com/archives/match-quoted-string)

Hopefully you can see the problem with that, if passing it in to an XML
attribute... the XML is broken because of the double quote:
<TextInput>
   <validator>
       <validation:RegexTextValidator pattern="*([*"'])(?:\\\1|.)*?\1" />
   </validator>
</TextInput>

To fix it:
<TextInput>
   <validator>
       <validation:RegexTextValidator pattern="*([&quot;'])(?:\\\1|.)*?\1*"
/>
   </validator>
</TextInput>

But how ugly is that?  &quot; is not part of the regex and makes the regex
look even worse.  My proposal:
<TextInput>
   <validator>
       <validation:RegexTextValidator>
           <validation:pattern><![CDATA[
*(["'])(?:\\\1|.)*?\1*
          ]]></validation:pattern>
       <validation:RegexTextValidator>
   </validator>
</TextInput>

The pattern would need to be 'trimmed', but at least that aspect is hidden
from the developer and no need to do any escaping, etc.

Any thoughts?  Would this be useful, or is it too much at this stage?

Cheers,
Chris


2009/3/26 Greg Brown <gk...@mac.com>

> Hi Noel,
>
> While attempting to add a validator example to the Kitchen Sink demo, I
> noticed a couple of things that still need a bit more work:
>
> 1) The validation classes need more Javadoc. I'll admit that we're pretty
> lax about method Javadoc a lot of the time, but we do try to make sure that
> every class has at least a minimal description. Otherwise, the documentation
> tends to look a bit unprofessional.
>
> 2) Each concrete validator should have a no-arg constructor and bean
> properties for configuring its behavior so it can be used in WTKX. For
> example, RegexTextValidator should have a "pattern" property:
>
> Pattern getPattern()
> void setPattern(Pattern pattern)
> void setPattern(String pattern)
>
> The string setter allows us to use it from WTKX like this:
>
> <TextInput>
>    <validator>
>        <validation:RegexTextValidator pattern="[0-9]"/>
>    </validator>
> </TextInput>
>
> WTKX automatically handles setters for primitive types (via
> BeanDictionary), so you don't need to provide String overloads for those.
> However, any non-primitive setters will require a String overload.
>
> We'd very much like to get these updates in before we release version 1.1.
> Do you think you will have time to do it in the very near future?
>
> Thanks,
> Greg
>
>
>

Re: Validation classes

Posted by Greg Brown <gk...@mac.com>.
Hi Noel,

As I mentioned in my reply to Chris's message, I have applied the patch and it works great. Nice work.

FYI, I did tweak the Javadoc a bit, to tie validation less specifically to TextInput. I'm thinking there may be other use cases for it in the future.

Greg


On Thursday, March 26, 2009, at 11:50AM, "Noel Grandin" <no...@gmail.com> wrote:
>Hi
>
>Patch attached.
>
>Regards, Noel.
>
>Greg Brown wrote:
>> Hi Noel,
>>
>> While attempting to add a validator example to the Kitchen Sink demo, I noticed a couple of things that still need a bit more work:
>>
>> 1) The validation classes need more Javadoc. I'll admit that we're pretty lax about method Javadoc a lot of the time, but we do try to make sure that every class has at least a minimal description. Otherwise, the documentation tends to look a bit unprofessional.
>>
>> 2) Each concrete validator should have a no-arg constructor and bean properties for configuring its behavior so it can be used in WTKX. For example, RegexTextValidator should have a "pattern" property:
>>
>> Pattern getPattern()
>> void setPattern(Pattern pattern)
>> void setPattern(String pattern)
>>
>> The string setter allows us to use it from WTKX like this:
>>
>> <TextInput>
>>     <validator>
>>         <validation:RegexTextValidator pattern="[0-9]"/>
>>     </validator>
>> </TextInput>
>>
>> WTKX automatically handles setters for primitive types (via BeanDictionary), so you don't need to provide String overloads for those. However, any non-primitive setters will require a String overload.
>>
>> We'd very much like to get these updates in before we release version 1.1. Do you think you will have time to do it in the very near future?
>>
>> Thanks,
>> Greg
>>
>>
>>   
>
>
>Index: wtk/src/pivot/wtk/text/validation/DecimalValidator.java
>===================================================================
>--- wtk/src/pivot/wtk/text/validation/DecimalValidator.java	(revision 758676)
>+++ wtk/src/pivot/wtk/text/validation/DecimalValidator.java	(working copy)
>@@ -18,10 +18,12 @@
> import java.text.ParseException;
> 
> /**
>- *
>+ * A {@link pivot.wtk.TextInput} {@link Validator} for decimal values.
>+ *  
>  * @author Noel Grandin
>  */
> public abstract class DecimalValidator extends FormattedValidator<NumberFormat> {
>+    
>     protected DecimalValidator(DecimalFormat format) {
>         super(format);
>     }
>Index: wtk/src/pivot/wtk/text/validation/DoubleRangeValidator.java
>===================================================================
>--- wtk/src/pivot/wtk/text/validation/DoubleRangeValidator.java	(revision 758676)
>+++ wtk/src/pivot/wtk/text/validation/DoubleRangeValidator.java	(working copy)
>@@ -14,17 +14,39 @@
> package pivot.wtk.text.validation;
> 
> /**
>- *
>+ * A {@link pivot.wtk.TextInput} {@link Validator} for a double value limited to a range.
>+ * 
>  * @author Noel Grandin
>  */
> public class DoubleRangeValidator extends DoubleValidator {
>-    private final double minValue, maxValue;
>+    private double minValue, maxValue;
> 
>+    public DoubleRangeValidator() {
>+        this.minValue = 0;
>+        this.maxValue = 1;
>+    }
>+    
>     public DoubleRangeValidator(double minValue, double maxValue) {
>         this.minValue = minValue;
>         this.maxValue = maxValue;
>     }
> 
>+    public double getMinimum() {
>+        return minValue;
>+    }
>+
>+    public void setMinimum(double minValue) {
>+        this.minValue = minValue;
>+    }
>+
>+    public double getMaximum() {
>+        return maxValue;
>+    }
>+
>+    public void setMaximum(double maxValue) {
>+        this.maxValue = maxValue;
>+    }
>+
>     @Override
>     public boolean isValid(String text) {
>         boolean valid = false;
>Index: wtk/src/pivot/wtk/text/validation/DoubleValidator.java
>===================================================================
>--- wtk/src/pivot/wtk/text/validation/DoubleValidator.java	(revision 758676)
>+++ wtk/src/pivot/wtk/text/validation/DoubleValidator.java	(working copy)
>@@ -14,6 +14,7 @@
> package pivot.wtk.text.validation;
> 
> /**
>+ * A {@link pivot.wtk.TextInput} {@link Validator} for a double value.
>  *
>  * @author Noel Grandin
>  */
>Index: wtk/src/pivot/wtk/text/validation/FloatRangeValidator.java
>===================================================================
>--- wtk/src/pivot/wtk/text/validation/FloatRangeValidator.java	(revision 758676)
>+++ wtk/src/pivot/wtk/text/validation/FloatRangeValidator.java	(working copy)
>@@ -14,17 +14,39 @@
> package pivot.wtk.text.validation;
> 
> /**
>+ * A {@link pivot.wtk.TextInput} {@link Validator} for a float value limited to a range.
>  *
>  * @author Noel Grandin
>  */
> public class FloatRangeValidator extends FloatValidator {
>-    private final float minValue, maxValue;
>+    private float minValue, maxValue;
> 
>+    public FloatRangeValidator() {
>+        this.minValue = 0;
>+        this.maxValue = 1;
>+    }
>+    
>     public FloatRangeValidator(float minValue, float maxValue) {
>         this.minValue = minValue;
>         this.maxValue = maxValue;
>     }
> 
>+    public float getMinimum() {
>+        return minValue;
>+    }
>+
>+    public void setMinimum(float minValue) {
>+        this.minValue = minValue;
>+    }
>+
>+    public float getMaximum() {
>+        return maxValue;
>+    }
>+
>+    public void setMaximum(float maxValue) {
>+        this.maxValue = maxValue;
>+    }
>+    
>     @Override
>     public boolean isValid(String text) {
>         boolean valid = false;
>Index: wtk/src/pivot/wtk/text/validation/FloatValidator.java
>===================================================================
>--- wtk/src/pivot/wtk/text/validation/FloatValidator.java	(revision 758676)
>+++ wtk/src/pivot/wtk/text/validation/FloatValidator.java	(working copy)
>@@ -14,6 +14,7 @@
> package pivot.wtk.text.validation;
> 
> /**
>+ * A {@link pivot.wtk.TextInput} {@link Validator} for a float value.
>  *
>  * @author Noel Grandin
>  */
>Index: wtk/src/pivot/wtk/text/validation/FormattedValidator.java
>===================================================================
>--- wtk/src/pivot/wtk/text/validation/FormattedValidator.java	(revision 758676)
>+++ wtk/src/pivot/wtk/text/validation/FormattedValidator.java	(working copy)
>@@ -16,12 +16,15 @@
> import java.text.ParsePosition;
> 
> /**
>- *
>+ * A {@link pivot.wtk.TextInput} {@link Validator} for a {@link java.text.Format}'ed value.
>+ * 
>+ * This class is mostly intended to be a base-class for other validators.
>+ * 
>  * @author Noel Grandin
>  */
> public abstract class FormattedValidator<TFormat extends java.text.Format> implements Validator {
>     protected final TFormat format;
>-
>+    
>     protected FormattedValidator(TFormat format) {
>         this.format = format;
>     }
>Index: wtk/src/pivot/wtk/text/validation/IntRangeValidator.java
>===================================================================
>--- wtk/src/pivot/wtk/text/validation/IntRangeValidator.java	(revision 758676)
>+++ wtk/src/pivot/wtk/text/validation/IntRangeValidator.java	(working copy)
>@@ -14,17 +14,39 @@
> package pivot.wtk.text.validation;
> 
> /**
>+ * A {@link pivot.wtk.TextInput} {@link Validator} for an int value limited to a range.
>  *
>  * @author Noel Grandin
>  */
> public class IntRangeValidator extends IntValidator {
>-    private final int minValue, maxValue;
>+    private int minValue, maxValue;
> 
>+    public IntRangeValidator() {
>+        this.minValue = 0;
>+        this.maxValue = 1;
>+    }
>+    
>     public IntRangeValidator(int minValue, int maxValue) {
>         this.minValue = minValue;
>         this.maxValue = maxValue;
>     }
> 
>+    public int getMinimum() {
>+        return minValue;
>+    }
>+
>+    public void setMinimum(int minValue) {
>+        this.minValue = minValue;
>+    }
>+
>+    public int getMaximum() {
>+        return maxValue;
>+    }
>+
>+    public void setMaximum(int maxValue) {
>+        this.maxValue = maxValue;
>+    }
>+    
>     @Override
>     public boolean isValid(String text) {
>         boolean valid = false;
>Index: wtk/src/pivot/wtk/text/validation/IntValidator.java
>===================================================================
>--- wtk/src/pivot/wtk/text/validation/IntValidator.java	(revision 758676)
>+++ wtk/src/pivot/wtk/text/validation/IntValidator.java	(working copy)
>@@ -14,6 +14,7 @@
> package pivot.wtk.text.validation;
> 
> /**
>+ * A {@link pivot.wtk.TextInput} {@link Validator} for an int value.
>  *
>  * @author Noel Grandin
>  */
>Index: wtk/src/pivot/wtk/text/validation/RegexTextValidator.java
>===================================================================
>--- wtk/src/pivot/wtk/text/validation/RegexTextValidator.java	(revision 758676)
>+++ wtk/src/pivot/wtk/text/validation/RegexTextValidator.java	(working copy)
>@@ -14,13 +14,19 @@
> package pivot.wtk.text.validation;
> 
> import java.util.regex.Pattern;
>+import java.util.regex.PatternSyntaxException;
> 
> /**
>- *
>+ * A {@link pivot.wtk.TextInput} {@link Validator} for a regular expression.
>+ * 
>+ * @see Pattern
>  * @author Noel Grandin
>  */
> public class RegexTextValidator implements Validator {
>-    private final Pattern p;
>+    private Pattern p;
>+
>+    public RegexTextValidator() {
>+    }
> 
>     public RegexTextValidator(Pattern p) {
>         this.p = p;
>@@ -30,6 +36,22 @@
>         this.p = Pattern.compile(regexPattern);
>     }
> 
>+    public Pattern getPattern() {
>+        return p;
>+    }
>+
>+    public void setPattern(Pattern pattern) {
>+        this.p = pattern;
>+    }
>+
>+    /**
>+     * @throws PatternSyntaxException
>+     *             If the expression's syntax is invalid
>+     */
>+    public void setPattern(String regexPattern) {
>+        this.p = Pattern.compile(regexPattern);
>+    }
>+
>     public boolean isValid(String text) {
>         return p.matcher(text).matches();
>     }
>Index: wtk/src/pivot/wtk/text/validation/Validator.java
>===================================================================
>--- wtk/src/pivot/wtk/text/validation/Validator.java	(revision 758676)
>+++ wtk/src/pivot/wtk/text/validation/Validator.java	(working copy)
>@@ -14,8 +14,15 @@
> package pivot.wtk.text.validation;
> 
> /**
>- * Validation interface for TextInput widget.
>- *
>+ * Validation interface for {@link pivot.wtk.TextInput} widget.
>+ * 
>+ * Allows the programmer to specify various constraints on the data in the
>+ * TextInput.
>+ * 
>+ * This is indicated visually to the user (a red background would be typical),
>+ * and events are fired by the TextInput if the programmer wishes to take
>+ * further action.
>+ * 
>  * @author Noel Grandin
>  */
> public interface Validator {
>
>

Re: Validation classes

Posted by Noel Grandin <no...@gmail.com>.
Hi

Patch attached.

Regards, Noel.

Greg Brown wrote:
> Hi Noel,
>
> While attempting to add a validator example to the Kitchen Sink demo, I noticed a couple of things that still need a bit more work:
>
> 1) The validation classes need more Javadoc. I'll admit that we're pretty lax about method Javadoc a lot of the time, but we do try to make sure that every class has at least a minimal description. Otherwise, the documentation tends to look a bit unprofessional.
>
> 2) Each concrete validator should have a no-arg constructor and bean properties for configuring its behavior so it can be used in WTKX. For example, RegexTextValidator should have a "pattern" property:
>
> Pattern getPattern()
> void setPattern(Pattern pattern)
> void setPattern(String pattern)
>
> The string setter allows us to use it from WTKX like this:
>
> <TextInput>
>     <validator>
>         <validation:RegexTextValidator pattern="[0-9]"/>
>     </validator>
> </TextInput>
>
> WTKX automatically handles setters for primitive types (via BeanDictionary), so you don't need to provide String overloads for those. However, any non-primitive setters will require a String overload.
>
> We'd very much like to get these updates in before we release version 1.1. Do you think you will have time to do it in the very near future?
>
> Thanks,
> Greg
>
>
>   


Re: Validation classes

Posted by Noel Grandin <no...@gmail.com>.
Hi

No problem, I'll look at it later today, maybe tomorrow at the latest.

Regards, Noel.

Greg Brown wrote:
> Hi Noel,
>
> While attempting to add a validator example to the Kitchen Sink demo, I noticed a couple of things that still need a bit more work:
>
> 1) The validation classes need more Javadoc. I'll admit that we're pretty lax about method Javadoc a lot of the time, but we do try to make sure that every class has at least a minimal description. Otherwise, the documentation tends to look a bit unprofessional.
>
> 2) Each concrete validator should have a no-arg constructor and bean properties for configuring its behavior so it can be used in WTKX. For example, RegexTextValidator should have a "pattern" property:
>
> Pattern getPattern()
> void setPattern(Pattern pattern)
> void setPattern(String pattern)
>
> The string setter allows us to use it from WTKX like this:
>
> <TextInput>
>     <validator>
>         <validation:RegexTextValidator pattern="[0-9]"/>
>     </validator>
> </TextInput>
>
> WTKX automatically handles setters for primitive types (via BeanDictionary), so you don't need to provide String overloads for those. However, any non-primitive setters will require a String overload.
>
> We'd very much like to get these updates in before we release version 1.1. Do you think you will have time to do it in the very near future?
>
> Thanks,
> Greg
>
>
>