View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.trinidad.validator;
20  
21  import java.util.Collection;
22  
23  import javax.faces.component.UIComponent;
24  import javax.faces.context.FacesContext;
25  
26  /**
27   * <p>
28   * Interface implemented by Objects that wish to perform client-side
29   * validation in addition to server-side validation.  Client side validation
30   * should always be the same or or more lenient than the server-side
31   * validation.
32   * </p>
33   *
34   * <P>
35   *     One of the benefits of Apache Trinidad is that it supports client-side versions of converters and validators. This means that errors can be caught on the client and a round trip avoided. This interface can be used to add client-side validation to a validator.
36   *     </P>
37   *     <p>
38   *     The basic idea of Apache Trinidad's client-side validation is that it works on the client in a very similar way to how it works on the server, except the language on the client is javascript instead of java. Apache Trinidad supports javascript Validator objects that support the method validate(). A Validator can throw a ValidatorException.
39   *     </p>      Let's say you've written a javax.faces.validator.Validator implementation and now you want to add client-side validation. The first thing to do is write a version of the validator in javascript. Here is the javascript code for the validator "interface".
40   *     </p>
41   *       <p>
42   *  <pre><code>
43   * /**
44   *  * Validator "interface" similar to javax.faces.validator.Validator,
45   *  * except that all relevant information must be passed to the constructor
46   *  * as the context and component are not passed to the validate method
47   *  * /
48   * function Validator(){}
49   *
50   * /**
51   * * Perform the correctness checks implemented by this Validator.
52   * * If any violations are found, a ValidatorException will be thrown.
53   * * /
54   * Validator.prototype.validate = function(value, label, converter){}
55   *
56   * </code></pre>
57   * Validators can throw a ValidatorException, here is the signature:
58   * <ul>
59   * <li>ValidatorException(detail)
60   *  <ul>
61   *    <li>detail - Localized detail message text </li>
62   *   </ul>
63   * </li>
64   * </ul>
65   * The method
66   * <ul>
67   *   <li><code>getClientLibrarySource()</code> is expected to return a library
68   * that has an implementation of the javascript Validator object.</li>
69   *   <li><code>getClientValidation()</code> is expected to return a
70   * javascript constructor which will be used to instantiate an instance of the validator.</li>
71   *   <li><code>getClientScript()</code> can be used to write out inline js.</li>
72   *   <li><code>getClientImportNames()</code> is used to import the built-in scripts provided by Apache Trinidad.</li>
73   *  </ul>
74   * <p>
75   * @see javax.faces.validator.Validator
76   */
77  public interface ClientValidator
78  {
79  
80    /**
81     * Gets the URI specifying the location of the js lib resource.
82     * Only the first reference to a library will result in its being imported.
83     */
84    public String getClientLibrarySource(
85     FacesContext context);
86  
87    /**
88     * Supports importing the built-in scripts provided by Apache Trinidad.
89     * It can be used to ensure that a Javascript function is available
90     * before using it in a Javascript handler.
91     * Only the first reference to a script will result in its being imported.
92     * <p>If this function returns null "Validator()" will be used.
93     * @return a collection of function names
94     */
95    public Collection<String> getClientImportNames();
96  
97  
98   /**
99     * Opportunity for the ClientValidator to return script content.
100    * For HTML, this will be javascript that will be embedded in a
101    * script tag. For HTML this method is expected to return an
102    * implementation of the javascript Validator object.
103    * <p>Do not rely on this content being ppr updated.
104    * <p>This method will be called once per validator instance.
105    * Content that should only be written once per request
106    * should only be returned once.
107    */
108   public String getClientScript(
109    FacesContext context,
110    UIComponent component);
111 
112   /**
113    * Called to retrieve the appropriate client
114    * validation code for the node and context.
115    * For HTML, this will be javascript that will be embedded in a
116    * script tag. For HTML this method is expected to return a
117    * constructor of the javascript Validator object
118    * returned by getClientScript().
119    */
120   public String getClientValidation(
121    FacesContext context,
122    UIComponent component);
123 
124 
125 }