myfaces-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Benjamin Mark <be...@bemar.de>
Subject Re: How to solve link enable/disable by validator?
Date Tue, 09 Nov 2010 08:41:04 GMT
Hi,

thx for your comment. I've tried to get the component by the following 
code without success:

private void setLink(FacesContext context)
     {
         UIComponent comp = 
context.getViewRoot().findComponent("company:forgottenLink");
         if (comp != null)
         {
             System.out.println("found");
             try
             {
                 HtmlButton button = (HtmlButton) comp;
                 button.setRendered(false);
             } catch (Exception ex)
             {
                 System.out.println(ex.getMessage());
             }

         } else
         {
             System.out.println("notfound");
         }
     }

The component is found but If I wan't to cast the component as 
HTMLButton (it's definitely a HTMLButton -> Debug Windows -> Variables) 
I get the seam debug page with error "Caused by 
javax.servlet.ServletException with message: "Servlet execution threw an 
exception" .
The exception catch part will be ignored.

If I don't cast and execute the "setRendered" method from the 
UIComponent class nothing happens.
The html button is still on the page. Doesn't matter if I reRender the 
HtmlButton in the page.

Does anyone have an idea?

Thx
Ben


Am 08.11.2010 12:44, schrieb Anton Gavazuk:
> 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
View raw message