Text Field

The text field is used to represent text within a form.

Properties

Title Text
Description Renders object items into a table row
Field Type text
Base Field Type control

Schema

Property Type Default Description
allowOptionalEmpty Allows this non-required field to validate when the value is empty
autocomplete string Allows you to specify the autocomplete attribute for the underlying input control whether or not field should have autocomplete enabled.
data object Allows you to specify a key/value map of data attributes that will be added as DOM attribuets for the underlying input control. The data attributes will be added as data-{name}='{value}'.
disallowEmptySpaces boolean Whether to disallow the entry of empty spaces in the text
disallowOnlyEmptySpaces boolean Whether to disallow the entry of only empty spaces in the text
enum array List of specific values for this property
inputType string Allows for the override of the underlying HTML5 input type. If not specified, an assumed value is provided based on the kind of input control (i.e. 'text', 'date', 'email' and so forth)
maskString string Expression for the field mask. Field masking will be enabled if not empty.
placeholder string Field placeholder.
size number 40 Field size.
trim boolean Remove whitespace from the beginning and end of string
typeahead Provides configuration for the $.typeahead plugin if it is available. For full configuration options, see: https://github.com/twitter/typeahead.js

Options

Property Type Default Description
name string Field Name.
sort function Defines an f(a,b) sort function for the array of enumerated values [{text, value}]. This is used to sort enum and optionLabels as well as results that come back from any data sources (for select and radio controls). By default the items are sorted alphabetically. Don't apply any sorting if false.

Example 1

A simple example of using Alpaca with nothing more than a string of text. Alpaca looks at your data and determines that it is a string. It then looks for a suitable candidate for representing a string and it decides to use the text field.

{
    "data": "I Love Alpaca Ice Cream!"
}

Example 2

A more developed example that specifies not only the data but also the schema and options. In this example, we intentionally set the data to something that is invalid. The schema specifies that the maximum length of the allowed value is 8 characters. Our value exceeds that and so we receive a message straight away indicating this problem.

{
    "data": "Mint Chocolate",
    "schema": {
        "minLength": 3,
        "maxLength": 8
    },
    "options": {
        "label": "Ice Cream",
        "helper": "Please tell us the kind of ice cream you love most!",
        "size": 30,
        "placeholder": "Enter an ice cream flavor"
    }
}

Example 3

Text field with data, schema, options and view parameters. The view parameter is for injecting additional styles to make the field label float to the left of the text field.

{
    "data": "Mint",
    "schema": {
        "minLength": 3,
        "maxLength": 8
    },
    "options": {
        "label": "Ice Cream",
        "helper": "Your favorite ice cream?",
        "size": 30
    },
    "view": {
        "parent": "bootstrap-edit",
        "styles": {
            ".alpaca-controlfield-label": {
                "float": "left",
                "padding": "6px 0.3em 0 0"
            }
        }
    }
}

Example 4

Text field with a mask. This feature is based on Josh Bush's Masked Input Plugin. It allows a user to more easily enter fixed width input where you would like them to enter the data in a certain format (dates,phone numbers, etc). The maskString parameter supports following predefined characters:

  • a - Represents an alpha character (A-Z,a-z)
  • 9 - Represents a numeric character (0-9)
      • Represents an alphanumeric character (A-Z,a-z,0-9)
{
    "data": "123-45-6789",
    "options": {
        "label": "Social Security Number",
        "helper": "Please enter your social security number.",
        "size": 30,
        "maskString": "999-99-9999"
    }
}

Example 5

Text field with an event listener option that listens to keypress event and then prints out your input in an outside div in reverse order.

{
    "options": {
        "label": "Echo Your Input",
        "helper": "Type whatever you want to type.",
        "onFieldKeyup": function(e) {
            $('#output').html(this.getValue().split("").reverse().join(""));
        }
    }
}

Example 6

Displays a text field using a display-only view. The text field simply prints out and is not editable.

{
    "data": "Mickey Mantle",
    "schema": {
        "type": "string"
    },
    "options": {
        "label": "Name"
    },
    "view": "bootstrap-display"
}

Example 7

This example uses $.typeahead auto-completion with a function to provide lookup values. The config block defines the first argument into the typeahead plugin. The datasets block defines the second argument into the typeahead plugin.

{
    "schema": {
        "type": "string"
    },
    "options": {
        "type": "text",
        "label": "Company Name",
        "helper": "Select the name of a cloud computing company",
        "typeahead": {
            "config": {
                "autoselect": true,
                "highlight": true,
                "hint": true,
                "minLength": 1
            },
            "datasets": {
                "type": "local",
                "source": function(query)
                {
                    var companies = [
                        "Cloud CMS",
                        "Amazon",
                        "HubSpot"
                    ];

                    var results = [];
                    for (var i = 0; i < companies.length; i++)
                    {
                        var add = true;

                        if (query)
                        {
                            add = (companies[i].indexOf(query) === 0);
                        }

                        if (add)
                        {
                            results.push({
                                "value": companies[i]
                            });
                        }
                    }

                    return results;
                }
            }
        }
    }
}

Example 8

Simple configuration for $.typeahead auto-completion of the field value based on locally provided values.

By convention, the source setting is a function that provides the dataset for a given query (see the typeahead documentation). To make things easier, we also accept an object with two fields - type and source.

If type is local, then an array can be passed in via source. The array should be either simple strings or an array of objects with the structure {'value': ''}.

If type is remote, then the source is a remote URL. This should hand back an array of objects with the structure {'value': ''}.

If type is prefetch, the source is a prefetch URL. This should hand back an array of objects with the structure {'value': ''}.

Here, for fun, we register a change event handler that pretties up a little box so we can see what the CSS color looks like.

var colorNames = ["AliceBlue","AntiqueWhite","Aqua","Aquamarine","Azure","Beige","Bisque","Black","BlanchedAlmond","Blue","BlueViolet","Brown","BurlyWood","CadetBlue","Chartreuse","Chocolate","Coral","CornflowerBlue","Cornsilk","Crimson","Cyan","DarkBlue","DarkCyan","DarkGoldenRod","DarkGray","DarkGrey","DarkGreen","DarkKhaki","DarkMagenta","DarkOliveGreen","Darkorange","DarkOrchid","DarkRed","DarkSalmon","DarkSeaGreen","DarkSlateBlue","DarkSlateGray","DarkSlateGrey","DarkTurquoise","DarkViolet","DeepPink","DeepSkyBlue","DimGray","DimGrey","DodgerBlue","FireBrick","FloralWhite","ForestGreen","Fuchsia","Gainsboro","GhostWhite","Gold","GoldenRod","Gray","Grey","Green","GreenYellow","HoneyDew","HotPink","IndianRed","Indigo","Ivory","Khaki","Lavender","LavenderBlush","LawnGreen","LemonChiffon","LightBlue","LightCoral","LightCyan","LightGoldenRodYellow","LightGray","LightGrey","LightGreen","LightPink","LightSalmon","LightSeaGreen","LightSkyBlue","LightSlateGray","LightSlateGrey","LightSteelBlue","LightYellow","Lime","LimeGreen","Linen","Magenta","Maroon","MediumAquaMarine","MediumBlue","MediumOrchid","MediumPurple","MediumSeaGreen","MediumSlateBlue","MediumSpringGreen","MediumTurquoise","MediumVioletRed","MidnightBlue","MintCream","MistyRose","Moccasin","NavajoWhite","Navy","OldLace","Olive","OliveDrab","Orange","OrangeRed","Orchid","PaleGoldenRod","PaleGreen","PaleTurquoise","PaleVioletRed","PapayaWhip","PeachPuff","Peru","Pink","Plum","PowderBlue","Purple","RebeccaPurple", "Red","RosyBrown","RoyalBlue","SaddleBrown","Salmon","SandyBrown","SeaGreen","SeaShell","Sienna","Silver","SkyBlue","SlateBlue","SlateGray","SlateGrey","Snow","SpringGreen","SteelBlue","Tan","Teal","Thistle","Tomato","Turquoise","Violet","Wheat","White","WhiteSmoke","Yellow","YellowGreen"];
$("#field8").alpaca({
    "schema": {
        "type": "string"
    },
    "options": {
        "type": "text",
        "label": "CSS Color",
        "helper": "Provide the name of a CSS color you would like to use",
        "typeahead": {
            "datasets": {
                "type": "local",
                "source": colorNames
            }
        },
        "events": {
            "change": function() {
                $("#field8box").css("background-color", this.getValue().toLowerCase());
            }
        }
    },
    "postRender": function(control) {
        control.getFieldEl().append("<div id='field8box' style='width: 200px; height: 100px; border: 1px gray solid; margin: 20px; border-radius: 20px;'></div>");
    }
});

Example 9

This example uses $.typeahead auto-completion with a remote data source. Auto-completion is provided for names of cloud computing companies. It also uses Hogan.js to assist with template driven rendering of the drop-down list.

The remote values are retrieved from a PHP script that accepts the input text as a query parameter. It uses this to perform a simple comparison. In your own script, you'll likely query a database or connect to a web service to produce matches.

{
    "schema": {
        "type": "string"
    },
    "options": {
        "type": "text",
        "label": "Company Name",
        "helper": "Select the name of a cloud computing company",
        "typeahead": {
            "datasets": {
                "type": "remote",
                "source": "http://www.alpacajs.org/endpoints/typeahead-sample.php?q=%QUERY",
                "templates": {
                    "empty": "Nothing found...",
                    "header": "

List of companies



", "footer": "

Powered by Alpaca

", "suggestion": "

" } } } } }

Example 10

This example uses the placeholder option to set up the placeholder text for a text field.

{
    "schema": {
        "type": "string"
    },
    "options": {
        "type": "text",
        "label": "Speak thy name and enter...",
        "placeholder": "What is your name?"
    }
}

Example 11

This example constrains the entered text value, forcing it to be at minimum 3 and at most 25. This not only runs validation checks but also enforces some UI behavior.

This also shows how many characters are left for maxLength as you type.

{
    "schema": {
        "type": "string",
        "minLength": 3,
        "maxLength": 25
    },
    "options": {
        "type": "text",
        "label": "What is your name?",
        "constrainMaxLength": true,
        "constrainMinLength": true,
        "showMaxLengthIndicator": true
    },
    "data": "Jackie Robinson"
}

Example 12

A text field with disallowed values.

{
    "data": "Mickey Mantle",
    "schema": {
        "type": "string",
        "disallow": ["Mickey Mantle", "Mickey"]
    },
    "options": {
        "label": "Name"
    }
}

Example 13

A text field with autocomplete.

{
    "data": "Mickey Mantle",
    "schema": {
        "type": "string"
    },
    "options": {
        "label": "Name",
        "autocomplete": true
    }
}

Example 14

A text with field with disallowEmptySpaces set to true. This prevents the entry of spaces. This is useful for things like username entry fields, as configured below.

{
    "schema": {
        "type": "string"
    },
    "options": {
        "type": "lowercase",
        "label": "User Name",
        "disallowEmptySpaces": true
    }
}

Example 15

A simple example of using Alpaca with nothing more than a string of text. Alpaca looks at your data and determines that it is a string. It then looks for a suitable candidate for representing a string and it decides to use the text field.

{
    "schema": {
        "type": "object",
        "properties": {
            "username": {
                "type": "string"
            },
            "password": {
                "type": "string"
            }
        }
    },
    "options": {
        "fields": {
            "username": {
                "type": "text",
                "label": "Username",
                "buttons": {
                    "check": {
                        "value": "Check Availability",
                        "click": function() {

                            // do something here...!
                            alert("This is available!");
                        }
                    }
                }
            },
            "password": {
                "type": "password",
                "label": "Password"
            }
        }
    }
}

Example 16

Here we set the format to uppercase to have text content automatically store in upper case.
Click on the View button to see the results.

{
    "schema": {
        "type": "string",
        "format": "uppercase"
    },
    "options": {
        "label": "Title:",
        "type": "text",
        "form": {
            "buttons": {
                "view": {
                    "label": "View",
                    "click": function() {
                        alert(this.getValue());
                    }
                }
            }
        }
    }
}

Example 17

Here we set the format to lowercase to have text content automatically store in lower case. Click on the View button to see the results.

{
    "schema": {
        "type": "string",
        "format": "lowercase"
    },
    "options": {
        "label": "Title:",
        "type": "text",
        "form": {
            "buttons": {
                "view": {
                    "label": "View",
                    "click": function() {
                        alert(this.getValue());
                    }
                }
            }
        }
    }    
}

Example 18

Here we use the trim option to ensure that the value is trimmed upon retrieval. Click on the View button to see the results.

{
    "schema": {
        "type": "string"
    },
    "options": {
        "label": "Title:",
        "type": "text",
        "trim": true,
        "form": {
            "buttons": {
                "view": {
                    "label": "View",
                    "click": function() {
                        alert("value: " + this.getValue() + ", length: " + this.getValue().length);
                    }
                }
            }
        }
    }    
}