org.apache.click.extras.control
Class CreditCardField

java.lang.Object
  extended by org.apache.click.control.AbstractControl
      extended by org.apache.click.control.Field
          extended by org.apache.click.control.TextField
              extended by org.apache.click.extras.control.CreditCardField
All Implemented Interfaces:
Serializable, Control, Stateful

public class CreditCardField
extends TextField

Provides a Credit Card control:   <input type='text'><select>.

Credit Card
CreditCardField will validate the card number against the selected card type when the control is processed.

Supported card include VISA, MASTER, AMEX, DINERS and DISCOVER.

CSS and JavaScript resources

The CreditCardField control makes use of the following resources (which Click automatically deploys to the application directory, /click): The CreditCardField uses a JavaScript onkeypress() integerFilter() method to prevent users from entering invalid characters. To enable number key filtering reference the variables $headElements and $jsElements in the page template.

CreditCardField Example

A CreditCardField example is provided below:
 <html>
 <head>
 $headElements
 </head>
 <body>

 $form

 $jsElements
 </body>
 </html> 
An example page using CreditCardField is provided below:
 public class PaymentPage extends Page {

     private Form form = new Form("form");
     private CreditCardField creditCardField  = new CreditCardField("creditCard", true);
     private IntegerField expiryField  = new IntegerField("expiryDate", true);

     public PaymentPage() {
         form.add(creditCardField);

         expiryField.setMinLength(4);
         expiryField.setMaxLength(4);
         expiryField.setSize(4);
         form.add(expiryField);

         form.add(new Submit("ok", "  OK  "));
         form.add(new Submit("cancel", this, "onCancelClick"));

         addControl(form);
     }

     public boolean onCancelClick() {
         setRedirect("index.htm");
         return false;
     }

     public void onPost() {
         if (form.isValid()) {
             String cardType = creditCardField.getCardType();
             Long cardNumber = creditCardField.getCardNumber();
             String expiryDate = expiryField.getInteger();

             // Make payment
             ..
         }
     }
 } 
See also W3C HTML reference INPUT

See Also:
Serialized Form

Field Summary
static String AMEX
          The American Express type credit card: "AMEX".
protected static List<Option> CARD_OPTIONS
          The statically initialized card type options list.
protected  String cardType
          The type of credit card: ["VISA" | "MASTER" | "AMEX" | "DINERS" | "DISCOVER"].
protected  Select cardTypeSelect
          The card type Select.
static String DINERS
          The Diners Club type credit card: "DINERS".
static String DISCOVER
          The Discovery type credit card: "DISCOVER".
static String MASTER
          The MasterCard type credit card: "MASTER".
protected static String SELECT_NAME
          The card type Select name.
protected static String VALIDATE_CREDITCARD_FUNCTION
          The field validation JavaScript function template.
static String VISA
          The Visa type credit card: "VISA".
 
Fields inherited from class org.apache.click.control.TextField
maxLength, minLength, size, VALIDATE_TEXTFIELD_FUNCTION
 
Fields inherited from class org.apache.click.control.Field
disabled, error, focus, form, help, label, labelStyle, labelStyleClass, parentStyleClassHint, parentStyleHint, readonly, required, tabindex, title, trim, validate, value
 
Fields inherited from class org.apache.click.control.AbstractControl
actionListener, attributes, behaviors, headElements, listener, listenerMethod, messages, name, parent, styles
 
Fields inherited from interface org.apache.click.Control
CONTROL_MESSAGES
 
Constructor Summary
CreditCardField()
          Create a credit card field with no name defined.
CreditCardField(String name)
          Construct the credit card field with the given name.
CreditCardField(String name, boolean required)
          Construct the credit card field with the given name and required status.
CreditCardField(String name, String label)
          Construct the credit card field with the given name and label.
CreditCardField(String name, String label, boolean required)
          Construct the credit card field with the given name, label and required status.
CreditCardField(String name, String label, int size)
          Construct a CreditCardField with the given name, label and size.
CreditCardField(String name, String label, int size, boolean required)
          Construct the CreditCardField with the given name, label, size and required status.
 
Method Summary
 void bindRequestValue()
          Bind the request submission, setting the value property and cardType property if defined in the request.
 Long getCardNumber()
          Return the Credit Card number.
 String getCardType()
          Return the selected Credit Card type:   [ "VISA" | "MASTER" | "AMEX" | "DINERS" | "DISCOVER" ].
 int getControlSizeEst()
           
 List<Element> getHeadElements()
          Returns the CreditCardField HTML head imports statements for the click/extras-control.js resource.
 String getValidationJavaScript()
          Return the field JavaScript client side validation function.
 void render(HtmlStringBuffer buffer)
          Render the HTML representation of the CreditCardField.
 void validate()
          Validate the CreditCardField request submission, using the card type to validate the card number.
 
Methods inherited from class org.apache.click.control.TextField
getMaxLength, getMinLength, getSize, getTag, getType, setMaxLength, setMinLength, setSize
 
Methods inherited from class org.apache.click.control.Field
getError, getErrorLabel, getFocus, getFocusJavaScript, getForm, getHelp, getId, getLabel, getLabelStyle, getLabelStyleClass, getParentStyleClassHint, getParentStyleHint, getRequestValue, getState, getTabIndex, getTextAlign, getTitle, getValidate, getValue, getValueObject, getWidth, isDisabled, isHidden, isReadonly, isRequired, isTrim, isValid, onProcess, removeState, renderTagBegin, restoreState, saveState, setDisabled, setError, setErrorMessage, setErrorMessage, setFocus, setForm, setHelp, setLabel, setLabelStyle, setLabelStyleClass, setListener, setParent, setParentStyleClassHint, setParentStyleHint, setReadonly, setRequired, setState, setTabIndex, setTextAlign, setTitle, setTrim, setValidate, setValue, setValueObject, setWidth
 
Methods inherited from class org.apache.click.control.AbstractControl
addBehavior, addStyleClass, appendAttributes, dispatchActionEvent, getActionListener, getAttribute, getAttributes, getBehaviors, getContext, getHtmlImports, getMessage, getMessage, getMessages, getName, getPage, getParent, getStyle, getStyles, hasAttribute, hasAttributes, hasBehaviors, hasStyles, isAjaxTarget, onDeploy, onDestroy, onInit, onRender, removeBehavior, removeStyleClass, renderTagEnd, setActionListener, setAttribute, setId, setName, setStyle, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

VISA

public static final String VISA
The Visa type credit card: "VISA".

See Also:
Constant Field Values

MASTER

public static final String MASTER
The MasterCard type credit card: "MASTER".

See Also:
Constant Field Values

AMEX

public static final String AMEX
The American Express type credit card: "AMEX".

See Also:
Constant Field Values

DINERS

public static final String DINERS
The Diners Club type credit card: "DINERS".

See Also:
Constant Field Values

DISCOVER

public static final String DISCOVER
The Discovery type credit card: "DISCOVER".

See Also:
Constant Field Values

CARD_OPTIONS

protected static final List<Option> CARD_OPTIONS
The statically initialized card type options list.


SELECT_NAME

protected static final String SELECT_NAME
The card type Select name.

See Also:
Constant Field Values

VALIDATE_CREDITCARD_FUNCTION

protected static final String VALIDATE_CREDITCARD_FUNCTION
The field validation JavaScript function template. The function template arguments are:

See Also:
Constant Field Values

cardType

protected String cardType
The type of credit card: ["VISA" | "MASTER" | "AMEX" | "DINERS" | "DISCOVER"]. The default value is "VISA"


cardTypeSelect

protected Select cardTypeSelect
The card type Select.

Constructor Detail

CreditCardField

public CreditCardField(String name)
Construct the credit card field with the given name.

Parameters:
name - the name of the field

CreditCardField

public CreditCardField(String name,
                       String label)
Construct the credit card field with the given name and label.

Parameters:
name - the name of the field
label - the label of the field

CreditCardField

public CreditCardField(String name,
                       boolean required)
Construct the credit card field with the given name and required status.

Parameters:
name - the name of the field
required - the field required status

CreditCardField

public CreditCardField(String name,
                       String label,
                       boolean required)
Construct the credit card field with the given name, label and required status.

Parameters:
name - the name of the field
label - the label of the field
required - the field required status

CreditCardField

public CreditCardField(String name,
                       String label,
                       int size)
Construct a CreditCardField with the given name, label and size.

Parameters:
name - the name of the field
label - the label of the field
size - the size of the field

CreditCardField

public CreditCardField(String name,
                       String label,
                       int size,
                       boolean required)
Construct the CreditCardField with the given name, label, size and required status.

Parameters:
name - the name of the field
label - the label of the field
size - the size of the field
required - the field required status

CreditCardField

public CreditCardField()
Create a credit card field with no name defined.

Please note the control's name must be defined before it is valid.

Method Detail

getCardType

public String getCardType()
Return the selected Credit Card type:   [ "VISA" | "MASTER" | "AMEX" | "DINERS" | "DISCOVER" ].

Returns:
the selected Credit Card type

getCardNumber

public Long getCardNumber()
Return the Credit Card number.

Returns:
the Credit Card number

getHeadElements

public List<Element> getHeadElements()
Returns the CreditCardField HTML head imports statements for the click/extras-control.js resource.

Specified by:
getHeadElements in interface Control
Overrides:
getHeadElements in class AbstractControl
Returns:
the HTML head import statements for the control
See Also:
Control.getHeadElements()

bindRequestValue

public void bindRequestValue()
Bind the request submission, setting the value property and cardType property if defined in the request.

Overrides:
bindRequestValue in class Field

getControlSizeEst

public int getControlSizeEst()
Overrides:
getControlSizeEst in class TextField
Returns:
the estimated rendered control size in characters
See Also:
AbstractControl.getControlSizeEst()

render

public void render(HtmlStringBuffer buffer)
Render the HTML representation of the CreditCardField.

Specified by:
render in interface Control
Overrides:
render in class TextField
Parameters:
buffer - the specified buffer to render the control's output to
See Also:
AbstractControl.toString()

getValidationJavaScript

public String getValidationJavaScript()
Return the field JavaScript client side validation function.

The function name must follow the format validate_[id], where the id is the DOM element id of the fields focusable HTML element, to ensure the function has a unique name.

Overrides:
getValidationJavaScript in class TextField
Returns:
the field JavaScript client side validation function

validate

public void validate()
Validate the CreditCardField request submission, using the card type to validate the card number.

A field error message is displayed if a validation error occurs. These messages are defined in the resource bundle:

  • /click-control.properties
    • field-maxlength-error
    • field-minlength-error
    • field-required-error
  • /org/apache/click/extras/control/CreditCardField.properties
    • creditcard-number-error

Overrides:
validate in class TextField