myfaces-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Benjamin Mark" <be...@bemar.de>
Subject How to solve link enable/disable by validator?
Date Mon, 08 Nov 2010 11:36:21 GMT
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
View raw message