我正在寻找关于在JSF 2.0中添加字段错误突出显示的最佳方法的一些指导/意见 . 到目前为止,我已经使用Cagatay's example成功实现了对逻辑的一些小调整 .
String styleClass = ( String ) uiInput.getAttributes().get("styleClass");
//Check the valid flag
if ( !uiInput.isValid() )
{
//Component already has a styleclass
if ( styleClass != null )
{
//check if it's already highlighted
if ( !styleClass.contains("ui-input-invalid") )
{
//if not add the error class to it
styleClass = styleClass + " ui-input-invalid";
//and put the new styleclass back on the component
uiInput.getAttributes().put("styleClass", styleClass);
}
} else
{
//no current style class so just add the error class
uiInput.getAttributes().put("styleClass", "ui-input-invalid");
}
} else //component is valid so we might need to remove a highlight
{
//component has a styleclass
if ( styleClass != null )
{
//check if it is already highlighted
if ( styleClass.contains("ui-input-invalid") )
{
//remove error class from the string
styleClass = styleClass.replace("ui-input-invalid", "");
//and put the new styleclass back on the component
uiInput.getAttributes().put("styleClass", styleClass);
}
}
}
我还使用了为每个组件的样式添加el的建议 - styleClass="#{component.valid ? '' : 'ui-input-invalid'}"
.
当与Bean Validation JSR303结合使用时,这两种方法都像魅力一样 . 但是我还有2个额外的验证阶段 . 1表示整个表单,即字段的正确组合,1表示字段和表单验证成功后验证我们的一般业务规则 . 为了使这些阶段也添加突出显示我需要做一些手动工作 . 对于两种突出显示方法,我必须手动将组件有效标志设置为false . 为了能够访问组件,我将它以vo的形式绑定到它自己的对象 . 现在为Cagatay 's example I need to add all of the components to a List and then pass this list to the highlighting method. If I use the styleclass approach I don' t不得不担心组件列表并传递给突出显示方法 . 这对我来说似乎是更好的方法,但我有点担心它是在JSF页面添加逻辑,这是我想要远离的东西 .
您认为最好的方法是什么,还是我不知道的另一种方式?另外我假设没有其他方法设置有效标志而不绑定组件?目前我必须绑定每个组件,以便我可以设置其有效标志 .
1 回答
自从我问这个问题已经有一段时间了,同时我们实际上已经将JSF部分实现到了我们的企业应用程序中 . 突出显示的字段的解决方案非常复杂,为了使其工作,可重用和可定制,它实际上必须适合包含JSF /输入值对象,相关输入字段对象和Bean验证的框架 . 因此,有人在这里磕磕绊绊可能无法得到他们正在寻找的答案 . 但是,我将包含一部分描述机制的内部设计文档 . 希望这将有助于其他任何人: