You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Steve Thompson <th...@gmail.com> on 2008/10/07 19:19:02 UTC

Custom Text Field...

Hi,

I'm attempting to create a custom validator for social security number
strings.  I've got the following code for this:

	static Pattern pattern = Pattern.compile("(\\d{3})[ \\-]*(\\d{2})[
\\-]*(\\d{4})");

	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;
			}

		};
	}

When I use this with a text field, I can intercept the calls to
convertToString.  However, convertToObject never seems to be getting
called.  Is there something that I am doing wrong/not understanding?

Thanks and best regards,


Steve

--

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Custom Text Field...

Posted by Steve Thompson <th...@gmail.com>.
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 <Ti...@ri.fi> 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: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Custom Text Field...

Posted by Timo Rantalaiho <Ti...@ri.fi>.
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: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Custom Text Field...

Posted by Igor Vaynberg <ig...@gmail.com>.
converters are not used for string<->string conversion because the
input is already a string.

-igor

On Tue, Oct 7, 2008 at 10:19 AM, Steve Thompson <th...@gmail.com> wrote:
> Hi,
>
> I'm attempting to create a custom validator for social security number
> strings.  I've got the following code for this:
>
>        static Pattern pattern = Pattern.compile("(\\d{3})[ \\-]*(\\d{2})[
> \\-]*(\\d{4})");
>
>        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;
>                        }
>
>                };
>        }
>
> When I use this with a text field, I can intercept the calls to
> convertToString.  However, convertToObject never seems to be getting
> called.  Is there something that I am doing wrong/not understanding?
>
> Thanks and best regards,
>
>
> Steve
>
> --
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org