Timo -
Thanks for your response.
I did use a PatternValidator alone in my first approach, but while I
could verify that '123-45-6789' was a legitimate SSN, what I wanted to
additionally do was fill in my model object's string with '123456789'.
Now, if the setSsn() in the model object needs to incorporate this
behavior, I can live with that, but I thought it would be cleaner to
convert to/from the model using a custom converter.
Here is the full text (sans imports) of the SsnTextField:
public class SsnTextField<T> extends TextField<T>
{
static Pattern pattern = Pattern.compile("(\\d{3})[ \\-]*(\\d{2})[
\\-]*(\\d{4})");
public SsnTextField(String id)
{
this(id, null);
}
public SsnTextField(String id, IModel<T> object)
{
super(id, object);
add(new PatternValidator(pattern));
}
@Override
public final IConverter<String> getConverter(Class type)
{
return new IConverter<String>()
{
public String convertToObject(String value, Locale
locale)
{
String result = "";
result = value.substring(0, 3) + "-" +
value.substring(3, 5) + "-"
+ value.substring(5, 8);
return result;
}
public String convertToString(String value, Locale
locale)
{
String result = "";
Matcher fit = pattern.matcher(value);
if(fit.find())
for(int index = 1; index <= fit.groupCount(); index++)
result += fit.group(index);
return result;
}
};
}
}
On 10/7/08, Timo Rantalaiho <[EMAIL PROTECTED]> wrote:
> On Tue, 07 Oct 2008, Steve Thompson wrote:
>> I'm attempting to create a custom validator for social security number
>> strings. I've got the following code for this:
> ...
>> public final IConverter<String> getConverter(Class type)
>
> Are you overriding getConverter?
>
> For custom validation, your better off using validators
> rather than converter for that. Check out the form
> validation examples.
>
> Basically your approach should work as well, it's just
> weird design to do validation in conversion, especially as
> your IConverter returns Strings and not ready-made
> SocialSecurityNumbers. But we'd need to see more code to be
> able to help debugging it. Make sure that your form
> component gets submitted.
>
> Best wishes,
> Timo
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]