gpt4 book ai didi

validation - PrimeFaces DataTable InCell 编辑 - 非平凡验证

转载 作者:行者123 更新时间:2023-12-01 10:57:37 25 4
gpt4 key购买 nike

我想在 DataTable 中同时使用 InCell 编辑和验证。我知道简单的验证可以用 f:validator 解决,但是用非平凡的名字呢?

比方说,我必须确保“名称”属性在表中是唯一的。因此,在接受编辑之前,我应该检查名称是否已更改以及是否被另一个元素使用。如果是这样,则必须拒绝修改。

如何实现?据我所知,eventListener 只会收到编辑已被接受的通知,因此理论上我可以使用react并还原它,但我更愿意在用户单击“接受”图标时拒绝编辑。

最佳答案

正如 Daniel 所说,您可以为此使用 JSF 验证器。一个简短的例子:

假设我们有一个人:

public class Person
{
// Just imagine getters and setters ;-)
private String firstName, lastName;
}

还有一个非常简单的辅助 bean:

@ManagedBean
@ViewScoped
public class PersonBean
{
private List<Person> persons = new ArrayList<Person>();

@PostConstruct
private void init()
{
persons.add(new Person("John", "Doe"));
}
}

例如,我们要确保名字以大写字母开头。我们不关心姓氏是否不以大写字母开头(因为与 IE 或遗留数据库的兼容性,你知道,通常的怪异)。

@FacesValidator("firstNameValidator")
public class FirstNameValidator implements javax.faces.validator.Validator
{
@Override
public void validate(FacesContext context, UIComponent component,
Object value) throws ValidatorException
{
if (!Character.isUpperCase(String.valueOf(value).charAt(0)))
{
FacesMessage msg = new FacesMessage("First name should start with a capital.");
throw new ValidatorException(msg);
}
}

}

现在要很好地显示所有内容:

<p:growl id="growl" />
<h:form>
<p:dataTable value="#{bean.persons}" var="person" editable="true">
<p:ajax event="rowEdit" update=":growl"/>
<p:column headerText="first name">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{person.firstName}" />
</f:facet>
<f:facet name="input">
<p:inputText validator="firstNameValidator"
value="#{person.firstName}" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="last name">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{person.lastName}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{person.lastName}" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column>
<p:rowEditor />
</p:column>
</p:dataTable>
</h:form>

如果您有兴趣,可以使用 bean 验证 (JSR-303) 在域级别配置验证。我强烈推荐它,因为它不依赖于 JSF,并且与 JPA 集成。


使用 bean 验证按照 promise 更新:

首先是验证器:

public class FirstNameValidator implements ConstraintValidator<FirstUpper, String>
{
@Override
public void initialize(FirstUpper constraintAnnotation) { }

@Override
public boolean isValid(String value, ConstraintValidatorContext context)
{
return Character.isUpperCase(value.charAt(0));
}

}

我们将要使用的注解:

@Constraint(validatedBy = FirstNameValidator.class)
@Target({ ElementType.METHOD, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
public @interface FirstUpper
{
String message() default "{FirstUpper.message}";

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};
}

请注意,我们声明的消息 "{FirstUpper.message}" 将被解析为资源包。该包必须位于类路径的根目录中,名为 ValidationMessages.properties。要进行本地化,您可以添加语言环境代码:ValidationMessages_en.properties

在该文件中声明消息:

FirstUpper.message=First name should start with a capital.

人物类:

public class Person
{
@FirstUpper
private String firstName;
private String lastName;
// Imagine the getters/setters again ;-)
}

现在,在您的 UI 中您不必引用验证器,JSF 足够智能,可以使用 JSR-303 进行验证。所以不是这个:

<p:inputText validator="firstNameValidator" value="#{person.firstName}" />

只需使用这个:

<p:inputText value="#{person.firstName}" />

简单吧? ;-)

关于validation - PrimeFaces DataTable InCell 编辑 - 非平凡验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14214262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com