Actually, I was able to get this done.
I had to contributeValidationMessagesSource to get the messages.
And to get to the current page component from within the Validator, I
was able to do this:
public static void contributeFieldValidatorSource
(MappedConfiguration<String, Validator> configuration,
@InjectService
("ComponentSource")ComponentSource componentSource,
@InjectService
("Request")Request request
) {
configuration.add("confirm", new Confirm(componentSource,
request));
}
and then:
String[] pathComponents = request.getPath().split("/");
String pageComponent = pathComponents[pathComponents.length
- 1];
String[] pageComponents = pageComponent.split("\\.");
Component page = componentSource.getPage(pageComponents[0]);
but this seems a little fragile. Is there a better way?
I do see what you mean though - there is nowhere to attach a piece of
javascript to accompany this validator, so it just goes into my own
global library. But I'm pretty happy overall with this solution.
J
On 25-Mar-08, at 2:13 PM, Julian Wood wrote:
Ok no worries.
On a related note, where do you put the messages file for a custom
validator? In the example below, which looks for a key called
'confirm', I have tried Confirm.properties (in same package as
validator called Confirm.java), Signup.properties (the page using
the validator) and app.properties. The latter two of course have
verifiably working messages. I always get a [[missing key:
confirm]] though.
Back to my validator question, I couldn't find the JIRA issue you
were referring to.
BTW, the client side of this type of a validator works quite nicely.
In .java:
@Validate("required")
public String getPassword() {
return password;
}
@Validate("required,confirm=password")
public String getConfirmPassword() {
return confirmPassword;
}
On the .tml:
<script>
Object.extend(Tapestry.Validator, {
confirm: function(field, message, constraint) {
Tapestry.addValidator(field, true, function(value, event)
{
var confirmationSourceField = $(constraint);
if (confirmationSourceField == null) {
console.error("No such field: " + constraint);
return;
} else if (value != confirmationSourceField.value) {
event.recordError(message);
}
});
}
})
</script>
And in Confirm.java (the validator):
public void render(Field field, String sourceFieldId,
MessageFormatter formatter, MarkupWriter markupWriter, FormSupport
formSupport) {
formSupport.addValidation(field, "confirm", buildMessage
(formatter, field), sourceFieldId);
}
J
On Mar 25, 2008, at 12:27 PM, Howard Lewis Ship wrote:
Sorry, those APIs are not in place yet. I have JIRA issue add
optional validators, which is a loose, general term for what you are
aiming for ("password is required if userName is given").
I think some relationships may not be expressable using the @Validate
annotation, i.e., the string representation; I'm thinking there will
be a kind of JavaScript template object that will be used to generate
some kinds of JavaScript.
Things have slowed down on 5.0 for a moment, while I work on an
urgent
client project (in Tapestry 5).
On Tue, Mar 25, 2008 at 11:08 AM, Julian Wood <[EMAIL PROTECTED]>
wrote:
Not possible?
Sometimes a validator needs to look at input from another field
to see
if it is valid (ie password/confirmPassword). I know it's easy to do
in onValidateForm, but I'd like to see how to do it in a nice
modular
Validator. I'm looking for something like:
public void validate(Field field, String sourceFieldId,
MessageFormatter formatter, Object value) throws
ValidationException {
Component page = ((AbstractField)
field)._resources.getPage();
try {
Method getter =
page.getClass().getDeclaredMethod(getMethodName(sourceFieldId));
String sourceValue = (String) getter.invoke(page);
if (value == null || !value.toString().equals
(sourceValue))
throw new ValidationException(buildMessage
(formatter,
field));
} catch (Exception e) {
log.error("No public getter field in page for: " +
sourceFieldId);
}
}
but can't do that (the only problem being the private
_resources). How
can that be done in a Validator? I can't inject anything. I don't
actually need to make my own field which exposes the
ComponentResources, do I?
Thanks.
J
On Mar 24, 2008, at 5:17 PM, Julian Wood wrote:
How can you get access to an arbitrary Field from inside a
Validator, given its id? In t4 it was relatively easy to grab the
form and from there grab a field.
Thanks,
J
-------------------------------------------------------------------
--
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]
--
Howard M. Lewis Ship
Creator Apache Tapestry and Apache HiveMind
---------------------------------------------------------------------
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]