myfaces-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Anton Gavazuk <antongava...@gmail.com>
Subject Re: How to solve link enable/disable by validator?
Date Mon, 08 Nov 2010 11:44:14 GMT
Hi,

rendered option is your friend:

2 possibilities inside your validator
1) you are looking for the link component in your view and change rendered
property of discovered UIComponent
2) you create a binding method for the rendered attribute of the link and
binding method will verify the condition
drawback of this method is that you will have to introduce session Variable
(Bean) or maintain your own 'custom' scope variable.

So prefer to use case #1

Cheers,
Anton

2010/11/8 Benjamin Mark <bemar@bemar.de>

> Hello,
>
> I have the following problem:
>
> If some user is entering a already used email adress or username I wan't to
> let appear a link (s:link/button) to the "forgott password" page in a form?
>
> I'm not sure how to solve it with my custom email validator?
>
> Has someone an idea how I could solve that?
>
> Thx for any help
>
> Best regards
>
> Ben
>
>
> Validator
> ######################################################################
>
> public class EmailValidator implements Validator, Serializable {
>
>
>    public void validate(FacesContext context, UIComponent component, Object
> value) throws ValidatorException
>    {
>
>        context.getViewRoot().getChildren()
>        if (value instanceof String)
>        {
>            if (!isValid((String) value))
>            {
>                throw new ValidatorException(new
> FacesMessage(Messages.instance().get("text_validemail")));
>            } else if (!isAvailable((String) value))
>            {
>                throw new ValidatorException(new
> FacesMessage(Messages.instance().get("test_emailalreadyused")));
>            }
>        } else
>        {
>            throw new ValidatorException(new FacesMessage("The object ["
> + value
>                    + "] is no instance of String which is needed for email
> validation"));
>        }
>
>    }
>
>    private boolean isValid(String eMail)
>    {
>        String expression = ".+@.+\\.[a-z]+";
>        // Make the comparison case-insensitive.
>        Pattern pattern = Pattern.compile(expression,
> Pattern.CASE_INSENSITIVE);
>        Matcher matcher = pattern.matcher(eMail);
>        if (matcher.matches())
>        {
>            return true;
>        }
>        return false;
>    }
>
>    private boolean isAvailable(String eMail)
>    {
>        EntityManager entityManager = (EntityManager)
> Component.getInstance("entityManager");
>
>        entityManager.joinTransaction();
>
>        Query q = entityManager.createQuery("from Person p where p.email =
> :email");
>        q.setParameter("email", eMail);
>        if (q.getResultList().size() > 0)
>        {
>            return false;
>        }
>        return true;
>    }
> }
>
> Email in Form
> ##############################################################
>
> <s:decorate id="emailField" template="layout/edit.xhtml">
>                    <ui:define
> name="label">#{messages.text_email}</ui:define>
>                    <h:inputText id="email" required="true" size="45"
> maxlength="45"
>                        label="#{messages.text_email}"
>                        value="#{personHome.instance.email}">
>                        <f:validator validatorId="emailValidator" />
>                        <a:support event="onblur" reRender="emailField"
>                            bypassUpdates="true" ajaxSingle="true" />
>                    </h:inputText>
>                </s:decorate>
>
>
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message