我有一个这样的表格:
<h:form>
Phone number:
<h:inputText id="phone_number"
value="#{dbUserManager.phoneNumber}"
validatorMessage="The phone number is invalid."
required="#{auth.value eq 'MOBILE'}"
requiredMessage="Phone number is required." />
</h:inputText>
Authentication type:
<h:selectOneMenu value="#{dbUserManager.authentication}" converter="#{authenticationTypeConverter}" binding="#{auth}">
<f:selectItems value="#{dbUserManager.authTypeList}" />
</h:selectOneMenu>
</h:form>
AuthenticationType是一个与此类似的实体:
@Entity
public class AuthenticationType implements Serializable
{
@Id
@Basic(optional = false)
private Short id;
@Basic(optional = false)
private String description;
@Override
//...
public String toString()
{
return description;
}
最后是AuthenticationTypeConverter:
@Stateless
@ManagedBean
@FacesConverter(value="authenticationTypesConverter")
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class AuthenticationTypeConverter implements Converter
{
@PersistenceContext(unitName="pu")
private EntityManager em = null;
@Override
public Object getAsObject(FacesContext fc, UIComponent uic, String string)
{
TypedQuery<AuthenticationType> q = em.createNamedQuery(
AuthenticationType.FIND_BY_DESCRIPTION,
AuthenticationType.class);
q.setParameter("description", string);
return q.getSingleResult();
}
@Override
public String getAsString(FacesContext fc, UIComponent uic, Object o)
{
if (o == null)
{
return "";
}
return o.toString();
}
}
我尝试做的是在身份验证类型设置为“MOBILE”时将电话号码的要求设置为true。但它没有像我期望的那样表现。
我认为问题在于我需要将FacesConverter用于AuthenticationType。但我不知道如何解决它。
任何人都可以提示吗?
由于
答案 0 :(得分:1)
用户从h:inputText
中选择后,您必须重新呈现h:selectOneMenu
字段。为此,请将此选项更改为
<h:selectOneMenu value="#{dbUserManager.authentication}"
converter="#{authenticationTypeConverter}" binding="#{auth}">
<f:selectItems value="#{dbUserManager.authTypeList}" />
<f:ajax event="change" render="phone_number" />
</h:selectOneMenu>
同样在inputTexts required
- 属性中,我希望更像
{dbUserManager.authentication.description}
试一试......
更新2014/01/22:
如果您想在phone_number
字段中保留已输入的信息,您可以告诉<f:ajax>
标记不仅要执行selectOneMenu
,还要在输入新字段之前执行输入字段。在这种情况下,用
<f:ajax />
解决方案
...
<f:ajax event="change" execute="@this phone_number" render="phone_number" />
...