Back

hiddenField()

The hiddenField() function generates a hidden <input type="hidden"> tag for a given model object and property. It’s commonly used to store identifiers or other values that need to persist across form submissions without being visible to the user. Note: Pass any additional arguments like class, rel, and id, and the generated tag will also include those values as HTML attributes.

Name Type Required Default Description
objectName any Yes The variable name of the object to build the form control for.
property string Yes The name of the property to use in the form control.
association string No The name of the association that the property is located on. Used for building nested forms that work with nested properties. If you are building a form with deep nesting, simply pass in a list to the nested object, and Wheels will figure it out.
position string No The position used when referencing a hasMany relationship in the association argument. Used for building nested forms that work with nested properties. If you are building a form with deep nestings, simply pass in a list of positions, and Wheels will figure it out.
encode boolean No true Encode URL parameters using EncodeForURL(). Please note that this does not make the string safe for placement in HTML attributes, for that you need to wrap the result in EncodeForHtmlAttribute() or use linkTo(), startFormTag() etc instead.
1. Basic usage with object and property
<!--- Hidden field for user.id --->
#hiddenField(objectName="user", property="id")#

// Generates something like:
// <input id="user-id" name="user.id" type="hidden" value="123">

2. Adding extra HTML attributes
#hiddenField(
    objectName="user",
    property="sessionToken",
    id="custom-token",
    class="hidden-tracker"
)#

// <input id="custom-token" name="user.sessionToken" type="hidden" value="abc123" class="hidden-tracker">

3. Nested association (hasOne or belongsTo)
#hiddenField(
    objectName="order",
    property="id",
    association="customer"
)#

// If an order has a customer, this binds the hidden field to order.customer.id.

4. Nested hasMany with position
#hiddenField(
    objectName="order",
    property="id",
    association="items",
    position="1"
)#

// Binds to the id of the second item in the order’s items collection.
Copied!