We can certainly contribute to the standard Translators, although I
don't believe HiveMind will pick up contributions from a page
specification (I could be wrong). The following will work:
Your hivemodule.xml (you can put hivemodule.xml in WEB-INF if you don't
already have one):
<?xml version="1.0"?>
<module id="lis" version="1.8.0"
package="com.example.services">
<contribution
configuration-id="tapestry.form.translator.Translators">
Add an integer-only translator with strict enforcement. Any
non-integer input will throw a validation exception.
<bean name="integer" class="IntegerTranslator" />
</contribution>
</module>
The IntegerTranslator class:
package com.example.services;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.hivemind.util.PropertyUtils;
import org.apache.tapestry.form.IFormComponent;
import org.apache.tapestry.form.ValidationMessages;
import org.apache.tapestry.form.translator.NumberTranslator;
import org.apache.tapestry.valid.ValidatorException;
/**
* A strict version of Tapestry's NumberTranslator that throws a validation
* error on any non-numeric input.
*
*/
public class IntegerTranslator extends NumberTranslator {
// Tapestry already contains an appropriate message
// for invalid integers, so we use its key here.
private static final String INVALID_FORMAT_KEY = "invalid-int-format";
private static final Pattern NUMERIC_PATTERN = Pattern.compile(
"-?[0-9]*" );
public IntegerTranslator(String initializer) {
PropertyUtils.configureProperties( this, initializer );
}
@Override
protected Object parseText(IFormComponent field, ValidationMessages
messages,
String text) throws ValidatorException {
// Validate against pattern
Matcher matcher = NUMERIC_PATTERN.matcher( text );
if ( !matcher.matches() ) {
throw new ValidatorException( buildMessage( messages, field,
getMessageKey() ), getConstraint() );
}
return super.parseText( field, messages, text );
}
@Override
protected String getMessageKey() {
return INVALID_FORMAT_KEY;
}
}
And reference it as normal:
<component id="integerField" type="TextField">
<binding name="translator" value="translator:specialTranslator" />
</component>
-Ryan
Leonardo Quijano Vincenzi wrote:
If that's true, then we can actually contribute to the standard
Translators, right?
<contribution configuration-id="Translators">
<bean name="date" class="SpecialTranslator"/>
</contribution>
<bean name="specialTranslator" class="xxx.xxx.SpecialTranslator">
<set name="something" value="someval" />
</bean>
And use it like this:
<component id="dateField" type="DatePicker">
<binding name="translator" value="translator:specialTranslator" />
</component>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]