zeebo404/play-html5-validation — comment on commit f855eef1ceb9852f02b0042e7c7b24c55eb11037
File:  line 0
By zeebo404:

I'm talking about nested classes, e.g. *#{input
for:'rootClass.nestedClass.fieldName', ... /}*

Its a convenience rather than having to have another variable to store
nestedClass in order to have *#{input for:'nestedClassVar.fieldName', ... /}
*, especially if you only need to access the nested class once.

As for refactoring out into another method, it would definitely clean up
the code, like make a method for '*Field getField(String[] components)*' to
return the field of the last component. However, to fix the bug with the
value being broken, rather than return the Field, we should return the
value of the second to last component, so that we can access the field
value. So rather than a getField method, we need a *Object
getComponent(String[] components)* that returns components[length - 2], so
change:

for(int x = 0; x < components.length - 1; x++)
{
clazz = clazz.getField(components[x]).getType();
}
// Prevent questionable NPE
if (clazz == null) {
return;
}
// Find field
final Field field = clazz.getField(components[components.length - 1]);
// Print the name of the field
HTML5ValidationTags.printAttribute("name", fieldname, out);
// Print the value of the field
final Object object = RenderArgs.current().get(components[0], clazz);

to:

*final Object object = getComponent(components);*
where getComponent:

private Object getComponent(String[] components)
{
Class clazz = null;
for (final Class current : Play.classloader.getAllClasses()) {
if (current.getSimpleName().equalsIgnoreCase(components[0])) {
clazz = current;
}
}
Object instance = RenderArgs.current().get(components[0], clazz);

for (int x = 0; x < components.length - 1; x++) {
Field field = clazz.getField(components[x]);
instance = field.get(instance);
clazz = field.getType();
}

return instance;
}

and then we can do *object.getClass().getField(components[components.length
- 1]);* to get the field, then use the same try/catch for accessing the
getter or field as needed.

Thanks for the reply, and let me know if this works. I don't have access to
the code at this time, so I can't test it.

Thanks,
Eric Siebeneich

On Sat, Jan 21, 2012 at 6:49 AM, Sebastian Ho <
reply@reply.github.com
> wrote:

> Hey, could you give an example why traversing is needed? Are you talking
> about *input* tags inside *input* tags? Are nested classes inside another
> class?
> I'd like to extend the commit with an explanation/comment in the code why
> we are doing this. Maybe we even should refactor all lines concerning
> finding the right class to its own method?
>
> ---
> Reply to this email directly or view it on GitHub:
>
> https://github.com/zeebo404/play-html5-validation/commit/f855eef1ceb9852f02b0042e7c7b24c55eb11037#commitcomment-888339
>