Class FormsHelper
- java.lang.Object
-
- com.day.cq.wcm.foundation.forms.FormsHelper
-
public class FormsHelper extends java.lang.Object
Helper class for the forms components.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
REQ_ATTR_ACTION_SUFFIX
static java.lang.String
REQ_ATTR_CLIENT_VALIDATION
static java.lang.String
REQ_ATTR_EDIT_RESOURCES
static java.lang.String
REQ_ATTR_EXPRESSIONS_ENABLED
static java.lang.String
REQ_ATTR_FORM_STRUCTURE_HELPER
static java.lang.String
REQ_ATTR_FORMID
static java.lang.String
REQ_ATTR_FORWARD_OPTIONS
static java.lang.String
REQ_ATTR_FORWARD_PATH
static java.lang.String
REQ_ATTR_GLOBAL_LOAD_MAP
static java.lang.String
REQ_ATTR_GLOBAL_LOAD_RESOURCE
static java.lang.String
REQ_ATTR_IS_INIT
static java.lang.String
REQ_ATTR_PROP_WHITELIST
static java.lang.String
REQ_ATTR_READ_ONLY
static java.lang.String
REQ_ATTR_REDIRECT
static java.lang.String
REQ_ATTR_REDIRECT_TO_REFERRER
static java.lang.String
REQ_ATTR_WRITTEN_JAVASCRIPT
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static boolean
allowExpressions(SlingHttpServletRequest req)
Indicates whether posted form values should be passed through an expression evaluator.static Resource
checkFormStructure(Resource rsrc)
Deprecated.useFormStructureHelper.updateFormStructure(Resource)
instead.static boolean
checkRule(Resource resource, SlingHttpServletRequest req, PageContext pageContext, java.lang.String propName)
Checks the rule specified by the given form property.static java.lang.String
decodeValue(java.lang.String value)
Url decode the value.static boolean
doClientValidation(SlingHttpServletRequest req)
Are we generating client validation?static java.lang.String
encodeValue(java.lang.String value)
Url encode the value.static void
endForm(SlingHttpServletRequest req)
Signal the end of the form.static java.lang.String
getActionSuffix(SlingHttpServletRequest req)
Get the request suffix currently set for the form action's URI.static java.util.Iterator<java.lang.String>
getContentRequestParameterNames(SlingHttpServletRequest req)
Return a list of content fields.static java.lang.String
getCss(ValueMap props, java.lang.String defaultCss)
Return the css classes for the field.static java.lang.String
getDescription(Resource formElement, java.lang.String defaultDescription)
Return the description for the field.static java.lang.String
getFieldId(SlingHttpServletRequest req, Resource rsrc)
Return the id for the fieldstatic java.util.List<Resource>
getFormEditResources(SlingHttpServletRequest req)
Get the list of resources to be handled by the "edit" resources form action.static java.util.Iterator<Resource>
getFormElements(Resource formResource)
Deprecated.useFormStructureHelper.getFormElements(Resource)
instead.static java.lang.String
getFormId(SlingHttpServletRequest req)
Return the formidstatic Resource
getFormLoadResource(SlingHttpServletRequest req)
Get the load resource for the form.static java.lang.String
getFormsPreCheckMethodName(SlingHttpServletRequest req)
Return the name of the check method.static RequestDispatcherOptions
getForwardOptions(ServletRequest req)
Gets the SlingRequestDispatcherOptions
to be used when forwarding togetForwardPath(SlingHttpServletRequest)
.static java.lang.String
getForwardPath(SlingHttpServletRequest req)
Get the forward path for processing the form.static java.lang.String
getForwardRedirect(ServletRequest request)
Returns the redirect to inject as ":redirect" parameter for the Sling POST servlet, when a formforward
is done.static ValueMap
getGlobalFormValues(SlingHttpServletRequest req)
Return the form values to load.static java.util.Locale
getLocale(SlingHttpServletRequest request)
Returns the locale.static java.lang.String
getLocalizedMessage(java.lang.String msg, SlingHttpServletRequest request)
Returns the localized message.static java.util.Map<java.lang.String,java.lang.String>
getOptions(SlingHttpServletRequest request, Resource elementResource)
Return the options for a form elementstatic java.lang.String
getParameterName(Resource rsrc)
Return the parameter name for the fieldstatic java.lang.String
getReferrer(HttpServletRequest request)
Returns the HTTP "referrer" header from the request, and also looks out for the common "referer" misspelling.static Resource
getResource(SlingHttpServletRequest request, Resource elementResource, java.lang.String relPath)
Returns a resource identified by a path relative to theelementResource
.static java.util.Map<java.lang.String,java.lang.String>
getShowHideExpressions(Resource resource)
Return a flattened map of all showHideExpressions that are descendants of a resource.static java.lang.String
getTitle(Resource formElement, java.lang.String defaultTitle)
Return the title for the field.static java.lang.String
getValue(SlingHttpServletRequest request, java.lang.String name, java.lang.String defaultValue)
Returns the value for the given name (property).static java.lang.String
getValue(SlingHttpServletRequest request, Resource elementResource)
Return the value for the element.static java.lang.String
getValue(SlingHttpServletRequest request, Resource elementResource, java.lang.String nameParam)
Return the value for the element.static java.lang.String[]
getValues(SlingHttpServletRequest request, java.lang.String name, java.lang.String[] defaultValues)
Returns the values for the given name (property).static java.lang.String[]
getValues(SlingHttpServletRequest request, Resource elementResource)
Return the values for the element.static java.lang.String[]
getValues(SlingHttpServletRequest request, Resource elementResource, java.lang.String nameParam)
Return the values for the element.static java.util.List<java.lang.String>
getValuesAsList(SlingHttpServletRequest request, Resource elementResource)
Return the values for the element as a list This methodstatic java.lang.String[]
getWhitelistPatterns(SlingHttpServletRequest req)
Get the list of white listed data name patterns.static boolean
hasMultiSelection(Resource formElement)
Is this a field with multi selection?static void
includeResource(SlingHttpServletRequest request, SlingHttpServletResponse response, Resource resource, java.lang.String selectorString)
Include the resource with the given selector with method GETstatic void
inlineValuesAsJson(SlingHttpServletRequest request, java.io.Writer out, java.lang.String path)
Writes the given form load resource as JSON into the given writer.static void
inlineValuesAsJson(SlingHttpServletRequest request, java.io.Writer out, java.lang.String path, int nodeDepth)
Writes the given form load resource as JSON into the given writer.static boolean
isReadOnly(Resource formElement)
Deprecated.To always support the global read-only flag, set for example by the "view" selector of theFormChooserServlet
, useisReadOnly(SlingHttpServletRequest, Resource)
instead.static boolean
isReadOnly(SlingHttpServletRequest request)
Returns true if the entire form has to be displayed in a read-only way.static boolean
isReadOnly(SlingHttpServletRequest request, Resource formElement)
Returns true if either the passed form field is configured as read-only or if the entire form is to be displayed in a read-only way.static boolean
isRedirectToReferrer(ServletRequest request)
Returns whether there should be a redirect to the HTTP referrer after the forward of a form POST request.static boolean
isRequired(Resource formElement)
Is this field required?static void
popFormReadOnly(SlingHttpServletRequest request, java.lang.Object previousState)
Pop a temporary read-only state.static java.lang.Object
pushFormReadOnly(SlingHttpServletRequest request)
Push a temporary read-only state.static void
redirectToReferrer(SlingHttpServletRequest request, SlingHttpServletResponse res)
Redirect to the referrer.static void
redirectToReferrer(SlingHttpServletRequest req, SlingHttpServletResponse res, java.util.Map<java.lang.String,java.lang.String[]> params)
Redirect to the referrer.static void
runAction(java.lang.String actionType, java.lang.String scriptName, Resource formResource, SlingHttpServletRequest request, SlingHttpServletResponse response)
Runs/includes a script of the form action.static void
setActionSuffix(SlingHttpServletRequest req, java.lang.String suffix)
Set a request suffix to be added to the form action's URI.static void
setFormEditResources(SlingHttpServletRequest req, java.util.List<Resource> resources)
Sets the list of resources to be handled by the "edit" resources form action.static void
setFormLoadResource(SlingHttpServletRequest req, Resource rsrc)
Set the load resource for the form.static void
setFormReadOnly(SlingHttpServletRequest request)
Marks the current form rendering to produce an read-only form.static void
setForwardOptions(ServletRequest req, RequestDispatcherOptions options)
Sets the SlingRequestDispatcherOptions
to be used when forwarding togetForwardPath(SlingHttpServletRequest)
.static void
setForwardPath(SlingHttpServletRequest req, java.lang.String path)
Set the forward path for processing the form.static void
setForwardPath(SlingHttpServletRequest req, java.lang.String path, boolean clearFormSelector)
Set the forward path for processing the form and makes sure the "form" selector from the form request is cleared upon forwarding.static void
setForwardRedirect(ServletRequest request, java.lang.String redirect)
Sets the Sling POST serlvet's ":redirect" parameter dynamically during the form POST execution, whensetForwardPath(SlingHttpServletRequest, String)
is used.static void
setRedirectToReferrer(ServletRequest request, boolean redirectToReferrer)
Sets a flag to redirect to the HTTP referrer after the forward of a form POST request.static void
startForm(SlingHttpServletRequest request, SlingHttpServletResponse response)
Signal the start of the form.static void
startForm(SlingHttpServletRequest request, SlingHttpServletResponse response, JspWriter out)
Deprecated.
-
-
-
Field Detail
-
REQ_ATTR_GLOBAL_LOAD_MAP
public static final java.lang.String REQ_ATTR_GLOBAL_LOAD_MAP
- See Also:
- Constant Field Values
-
REQ_ATTR_GLOBAL_LOAD_RESOURCE
public static final java.lang.String REQ_ATTR_GLOBAL_LOAD_RESOURCE
- See Also:
- Constant Field Values
-
REQ_ATTR_EDIT_RESOURCES
public static final java.lang.String REQ_ATTR_EDIT_RESOURCES
- See Also:
- Constant Field Values
-
REQ_ATTR_CLIENT_VALIDATION
public static final java.lang.String REQ_ATTR_CLIENT_VALIDATION
- See Also:
- Constant Field Values
-
REQ_ATTR_FORMID
public static final java.lang.String REQ_ATTR_FORMID
- See Also:
- Constant Field Values
-
REQ_ATTR_WRITTEN_JAVASCRIPT
public static final java.lang.String REQ_ATTR_WRITTEN_JAVASCRIPT
- See Also:
- Constant Field Values
-
REQ_ATTR_ACTION_SUFFIX
public static final java.lang.String REQ_ATTR_ACTION_SUFFIX
- See Also:
- Constant Field Values
-
REQ_ATTR_FORWARD_PATH
public static final java.lang.String REQ_ATTR_FORWARD_PATH
- See Also:
- Constant Field Values
-
REQ_ATTR_FORWARD_OPTIONS
public static final java.lang.String REQ_ATTR_FORWARD_OPTIONS
- See Also:
- Constant Field Values
-
REQ_ATTR_IS_INIT
public static final java.lang.String REQ_ATTR_IS_INIT
- See Also:
- Constant Field Values
-
REQ_ATTR_READ_ONLY
public static final java.lang.String REQ_ATTR_READ_ONLY
- See Also:
- Constant Field Values
-
REQ_ATTR_REDIRECT
public static final java.lang.String REQ_ATTR_REDIRECT
- See Also:
- Constant Field Values
-
REQ_ATTR_REDIRECT_TO_REFERRER
public static final java.lang.String REQ_ATTR_REDIRECT_TO_REFERRER
-
REQ_ATTR_PROP_WHITELIST
public static final java.lang.String REQ_ATTR_PROP_WHITELIST
- See Also:
- Constant Field Values
-
REQ_ATTR_EXPRESSIONS_ENABLED
public static final java.lang.String REQ_ATTR_EXPRESSIONS_ENABLED
- See Also:
- Constant Field Values
-
REQ_ATTR_FORM_STRUCTURE_HELPER
public static final java.lang.String REQ_ATTR_FORM_STRUCTURE_HELPER
- See Also:
- Constant Field Values
-
-
Method Detail
-
startForm
@Deprecated public static void startForm(SlingHttpServletRequest request, SlingHttpServletResponse response, JspWriter out) throws java.io.IOException, ServletException
Deprecated.Signal the start of the form. Prepare the request object, write out the client javascript (if the form is configured accordingly) and write out the start form tag and hidden fields:FormsConstants.REQUEST_PROPERTY_FORMID
with the value of the form id.FormsConstants.REQUEST_PROPERTY_FORM_START
with the relative path to the form start par and_charset_
with the valueUTF-8
- Parameters:
request
- The current request.response
- The current response.out
- The jsp writer.- Throws:
java.io.IOException
- if form generation caused an errorServletException
- if form generation caused an error
-
startForm
public static void startForm(SlingHttpServletRequest request, SlingHttpServletResponse response) throws java.io.IOException, ServletException
Signal the start of the form. Prepare the request object, write out the client javascript (if the form is configured accordingly) and write out the start form tag and hidden fields:FormsConstants.REQUEST_PROPERTY_FORMID
with the value of the form id.FormsConstants.REQUEST_PROPERTY_FORM_START
with the relative path to the form start par and_charset_
with the valueUTF-8
- Parameters:
request
- The current request.response
- The current response.- Throws:
java.io.IOException
- if form generation caused an errorServletException
- if form generation caused an error- Since:
- 5.3
-
runAction
public static void runAction(java.lang.String actionType, java.lang.String scriptName, Resource formResource, SlingHttpServletRequest request, SlingHttpServletResponse response) throws java.io.IOException, ServletException
Runs/includes a script of the form action.- Parameters:
actionType
- action typescriptName
- script nameformResource
- resourcerequest
- requestresponse
- response- Throws:
java.io.IOException
- if inclusion caused an errorServletException
- if inclusion caused an error
-
endForm
public static void endForm(SlingHttpServletRequest req)
Signal the end of the form.- Parameters:
req
- Request
-
setForwardPath
public static void setForwardPath(SlingHttpServletRequest req, java.lang.String path)
Set the forward path for processing the form.- Parameters:
req
- The current request.path
- The forward path.
-
setForwardPath
public static void setForwardPath(SlingHttpServletRequest req, java.lang.String path, boolean clearFormSelector)
Set the forward path for processing the form and makes sure the "form" selector from the form request is cleared upon forwarding. This is usually required if the forward should go to the sling POST servlet, to avoid conflicts with the form chooser.This is not required if the forward path already contains an extension and possible selectors, in which case these overwrite the form request selector anyway.
* This uses
setForwardOptions
withRequestDispatcherOptions.setReplaceSelectors("")
.- Parameters:
req
- The current request.path
- The forward path.clearFormSelector
- if the "form" selector should be cleared- Since:
- 5.5
-
getForwardPath
public static java.lang.String getForwardPath(SlingHttpServletRequest req)
Get the forward path for processing the form.- Parameters:
req
- The current request.- Returns:
- The forward path or null.
-
setForwardOptions
public static void setForwardOptions(ServletRequest req, RequestDispatcherOptions options)
Sets the SlingRequestDispatcherOptions
to be used when forwarding togetForwardPath(SlingHttpServletRequest)
.- Parameters:
req
- The current requestoptions
- options to use with forward path- Since:
- 5.5
-
getForwardOptions
public static RequestDispatcherOptions getForwardOptions(ServletRequest req)
Gets the SlingRequestDispatcherOptions
to be used when forwarding togetForwardPath(SlingHttpServletRequest)
.- Parameters:
req
- The current request- Returns:
- options to use with forward path or
null
- Since:
- 5.5
-
setForwardRedirect
public static void setForwardRedirect(ServletRequest request, java.lang.String redirect)
Sets the Sling POST serlvet's ":redirect" parameter dynamically during the form POST execution, whensetForwardPath(SlingHttpServletRequest, String)
is used.- Parameters:
request
- the current requestredirect
- a redirect path/url- Since:
- 5.5
-
getForwardRedirect
public static java.lang.String getForwardRedirect(ServletRequest request)
Returns the redirect to inject as ":redirect" parameter for the Sling POST servlet, when a formforward
is done.- Parameters:
request
- the current request- Returns:
- redirect path/url
- Since:
- 5.5
-
setActionSuffix
public static void setActionSuffix(SlingHttpServletRequest req, java.lang.String suffix)
Set a request suffix to be added to the form action's URI.- Parameters:
req
- The current request.suffix
- The suffix or null.
-
getActionSuffix
public static java.lang.String getActionSuffix(SlingHttpServletRequest req)
Get the request suffix currently set for the form action's URI.- Parameters:
req
- The current Request.- Returns:
- THe suffix or null.
-
setFormLoadResource
public static void setFormLoadResource(SlingHttpServletRequest req, Resource rsrc)
Set the load resource for the form.- Parameters:
req
- The current request.rsrc
- The load resource
-
getFormLoadResource
public static Resource getFormLoadResource(SlingHttpServletRequest req)
Get the load resource for the form.- Parameters:
req
- current request- Returns:
- load resource or
null
if not set
-
getGlobalFormValues
public static ValueMap getGlobalFormValues(SlingHttpServletRequest req)
Return the form values to load.- Parameters:
req
- The request- Returns:
- The values or null
-
setFormEditResources
public static void setFormEditResources(SlingHttpServletRequest req, java.util.List<Resource> resources)
Sets the list of resources to be handled by the "edit" resources form action.- Parameters:
req
- current requestresources
- the list of resources
-
getFormEditResources
public static java.util.List<Resource> getFormEditResources(SlingHttpServletRequest req)
Get the list of resources to be handled by the "edit" resources form action.- Parameters:
req
- current request- Returns:
- the list of resources (or
null
if not set)
-
getFormsPreCheckMethodName
public static java.lang.String getFormsPreCheckMethodName(SlingHttpServletRequest req)
Return the name of the check method. Method name is based on from id with any hyphens converted to underscores. If the form can't be found, it will default to defaultForm.- Parameters:
req
- The current request.- Returns:
- The method name
-
includeResource
public static void includeResource(SlingHttpServletRequest request, SlingHttpServletResponse response, Resource resource, java.lang.String selectorString) throws java.io.IOException, ServletException
Include the resource with the given selector with method GET- Parameters:
request
- The current request.response
- The current response.resource
- The resource to include.selectorString
- The selector string to use for inclusion.- Throws:
java.io.IOException
- if requested resource is not accessibleServletException
- if requested resource is not accessible
-
doClientValidation
public static boolean doClientValidation(SlingHttpServletRequest req)
Are we generating client validation?- Parameters:
req
- Request- Returns:
- true or false
-
getFormId
public static java.lang.String getFormId(SlingHttpServletRequest req)
Return the formid- Parameters:
req
- Request- Returns:
- The form id or null
-
getParameterName
public static java.lang.String getParameterName(Resource rsrc)
Return the parameter name for the field- Parameters:
rsrc
- The resource- Returns:
- The parameter name.
-
getFieldId
public static java.lang.String getFieldId(SlingHttpServletRequest req, Resource rsrc)
Return the id for the field- Parameters:
req
- The current request.rsrc
- The resource.- Returns:
- The id.
-
getFormElements
@Deprecated public static java.util.Iterator<Resource> getFormElements(Resource formResource)
Deprecated.useFormStructureHelper.getFormElements(Resource)
instead.Return all form elements for this form.- Parameters:
formResource
- The form resource-- Returns:
- An iterator for all form elements.
-
getContentRequestParameterNames
public static java.util.Iterator<java.lang.String> getContentRequestParameterNames(SlingHttpServletRequest req)
Return a list of content fields. This method returns all field names (= request parameter names) that contain actually content by filtering out special request parameters like those starting with a ":", "_charset_" etc.- Returns:
- Iterator for the field names.
-
getResource
public static Resource getResource(SlingHttpServletRequest request, Resource elementResource, java.lang.String relPath)
Returns a resource identified by a path relative to theelementResource
. This method also considers form edit resources.- Parameters:
request
- the current request.elementResource
- the element resource.relPath
- a path relative toelementResource
.- Returns:
- the resource or
null
if none exists at the givenrelPath
.
-
getValue
public static java.lang.String getValue(SlingHttpServletRequest request, Resource elementResource)
Return the value for the element. This method invokesgetValues(SlingHttpServletRequest, Resource)
and returns if available the first value from the array.- Parameters:
request
- The current request.elementResource
- The element resource.- Returns:
- The value for the form element or null.
-
getValue
public static java.lang.String getValue(SlingHttpServletRequest request, Resource elementResource, java.lang.String nameParam)
Return the value for the element. This method invokesgetValues(SlingHttpServletRequest, Resource)
and returns if available the first value from the array.- Parameters:
request
- The current request.elementResource
- The element resource.nameParam
- The name of the name parameter (defaults to "name")- Returns:
- The value for the form element or null.
-
getValues
public static java.lang.String[] getValues(SlingHttpServletRequest request, Resource elementResource)
Return the values for the element. This method- Parameters:
request
- The current request.elementResource
- The element resource.- Returns:
- The values for the form element or null.
-
getValues
public static java.lang.String[] getValues(SlingHttpServletRequest request, Resource elementResource, java.lang.String nameParam)
Return the values for the element. This method- Parameters:
request
- The current request.elementResource
- The element resource.nameParam
- The name of the name parameter (defaults to "name")- Returns:
- The values for the form element or null.
-
getValuesAsList
public static java.util.List<java.lang.String> getValuesAsList(SlingHttpServletRequest request, Resource elementResource)
Return the values for the element as a list This method- Parameters:
request
- The current request.elementResource
- The element resource.- Returns:
- The values for the form element or an empty list.
-
getValue
public static java.lang.String getValue(SlingHttpServletRequest request, java.lang.String name, java.lang.String defaultValue)
Returns the value for the given name (property).- Parameters:
request
- The current requestname
- name of the property (or property path)defaultValue
- default value to return if property is not present- Returns:
- The value for the given property
-
getValues
public static java.lang.String[] getValues(SlingHttpServletRequest request, java.lang.String name, java.lang.String[] defaultValues)
Returns the values for the given name (property).- Parameters:
request
- The current requestname
- name of the property (or property path)defaultValues
- default values to return if property is not present- Returns:
- The values for the given property
-
inlineValuesAsJson
public static void inlineValuesAsJson(SlingHttpServletRequest request, java.io.Writer out, java.lang.String path) throws java.io.IOException, RepositoryException, JSONException
Writes the given form load resource as JSON into the given writer. This will dump the full tree; useinlineValuesAsJson(SlingHttpServletRequest, Writer, String, int)
for controlling the node depth.Can be used in JSPs to inline JSON for javascript code, for example:
var data = <% FormsHelper.inlineValuesAsJson(slingRequest, out, "."); %>;
which might result in:var data = { "jcr:primaryType": "nt:unstructured", "property" : "value" };
If the path cannot be found, an empty object "
{}
" will be written. Any exception will be passed to the caller.The underlying form load resource must be based on a JCR Node. The path is relative and allows to specify subnodes. It cannot point to JCR properties, please use
getValue(SlingHttpServletRequest, String, String)
orgetValues(SlingHttpServletRequest, String, String[])
for them.- Parameters:
request
- the current requestout
- a writer, such as aJspWriter
, to write the JSON into. Will automatically be flushed before and after.path
- an absolute node path or a node path relativ to the current form load resource. Use "." for the resource node itself.- Throws:
RepositoryException
- if some jcr error happenedJSONException
- if writing the json failedjava.io.IOException
- if there was a problem with the writer
-
inlineValuesAsJson
public static void inlineValuesAsJson(SlingHttpServletRequest request, java.io.Writer out, java.lang.String path, int nodeDepth) throws java.io.IOException, RepositoryException, JSONException
Writes the given form load resource as JSON into the given writer. Can be used in JSPs to inline JSON for javascript code, for example:var data = <% FormsHelper.inlineValuesAsJson(slingRequest, out, "."); %>;
which might result in:var data = { "jcr:primaryType": "nt:unstructured", "property" : "value" };
If the path cannot be found, an empty object "
{}
" will be written. Any exception will be passed to the caller.The underlying form load resource must be based on a JCR Node. The path is relative and allows to specify subnodes. It cannot point to JCR properties, please use
getValue(SlingHttpServletRequest, String, String)
orgetValues(SlingHttpServletRequest, String, String[])
for them.- Parameters:
request
- the current requestout
- a writer, such as aJspWriter
, to write the JSON into. Will automatically be flushed before and after.path
- an absolute node path or a node path relativ to the current form load resource. Use "." for the resource node itself.nodeDepth
- until which depth the tree should be written; 0 means the current node and its properties only; -1 means the whole tree.- Throws:
RepositoryException
- if some jcr error happenedJSONException
- if writing the json failedjava.io.IOException
- if there was a problem with the writer
-
getShowHideExpressions
public static java.util.Map<java.lang.String,java.lang.String> getShowHideExpressions(Resource resource) throws RepositoryException
Return a flattened map of all showHideExpressions that are descendants of a resource.- Parameters:
resource
- the node to check to see if there's any showHideExpressions.- Returns:
- A map of showHideExpressions keyed by element name.
- Throws:
RepositoryException
- if repository access caused an error
-
getOptions
public static java.util.Map<java.lang.String,java.lang.String> getOptions(SlingHttpServletRequest request, Resource elementResource)
Return the options for a form element- Parameters:
request
- requestelementResource
- element- Returns:
- A map of options (key-value) or null.
-
isRequired
public static boolean isRequired(Resource formElement)
Is this field required?- Parameters:
formElement
- The form element.- Returns:
- true if the field is required, false otherwise.
-
setFormReadOnly
public static void setFormReadOnly(SlingHttpServletRequest request)
Marks the current form rendering to produce an read-only form. Form field renderings must useisReadOnly(SlingHttpServletRequest, Resource)
to decide between edit or read-only representations.- Parameters:
request
- the current request
-
pushFormReadOnly
public static java.lang.Object pushFormReadOnly(SlingHttpServletRequest request)
Push a temporary read-only state. (SeesetFormReadOnly(SlingHttpServletRequest)
for further details of read-only states.)- Parameters:
request
- the current request- Returns:
- a token for use by
popFormReadOnly(SlingHttpServletRequest, Object)
-
popFormReadOnly
public static void popFormReadOnly(SlingHttpServletRequest request, java.lang.Object previousState)
Pop a temporary read-only state.- Parameters:
request
- the current requestpreviousState
- The token returned bypushFormReadOnly(SlingHttpServletRequest)
-
isReadOnly
public static boolean isReadOnly(SlingHttpServletRequest request, Resource formElement)
Returns true if either the passed form field is configured as read-only or if the entire form is to be displayed in a read-only way. The latter is the case whensetFormReadOnly(SlingHttpServletRequest)
was called, for example if the "view" selector of theFormChooserServlet
is used.- Parameters:
request
- the current requestformElement
- the form field resource- Returns:
true
if this field is to be rendered as read-only,false
otherwise.
-
isReadOnly
public static boolean isReadOnly(SlingHttpServletRequest request)
Returns true if the entire form has to be displayed in a read-only way.- Parameters:
request
- the current request- Returns:
true
if a form element has to be rendered as read-only,false
otherwise.- Since:
- 5.5
-
isReadOnly
@Deprecated public static boolean isReadOnly(Resource formElement)
Deprecated.To always support the global read-only flag, set for example by the "view" selector of theFormChooserServlet
, useisReadOnly(SlingHttpServletRequest, Resource)
instead.Returns true if the passed form field is configured as read-only. This is determined solely by looking at the field's "readOnly" property.- Parameters:
formElement
- the form field resource- Returns:
true
if this field is read-only,false
otherwise.
-
checkRule
public static boolean checkRule(Resource resource, SlingHttpServletRequest req, PageContext pageContext, java.lang.String propName)
Checks the rule specified by the given form property.
Rules may be used to enable/disable form elements according to some serverside conditions.
Currently, only rules depending on access rights are available.
- Parameters:
resource
- resourcereq
- requestpropName
- property name- Returns:
- true if check was successful
-
getTitle
public static java.lang.String getTitle(Resource formElement, java.lang.String defaultTitle)
Return the title for the field.- Parameters:
formElement
- The form element.defaultTitle
- The default title.- Returns:
- The title to display.
-
getDescription
public static java.lang.String getDescription(Resource formElement, java.lang.String defaultDescription)
Return the description for the field.- Parameters:
formElement
- The form element.defaultDescription
- The default description.- Returns:
- The description to display.
-
hasMultiSelection
public static boolean hasMultiSelection(Resource formElement)
Is this a field with multi selection?- Parameters:
formElement
- The form element- Returns:
- true if field has multi selection
- Since:
- 5.5
-
redirectToReferrer
public static void redirectToReferrer(SlingHttpServletRequest req, SlingHttpServletResponse res, java.util.Map<java.lang.String,java.lang.String[]> params) throws java.io.IOException
Redirect to the referrer. This method redirects to the referrer and adds optional request parameters.- Parameters:
req
- The current requestres
- The current response- Throws:
java.io.IOException
- if redirection caused an error
-
redirectToReferrer
public static void redirectToReferrer(SlingHttpServletRequest request, SlingHttpServletResponse res) throws java.io.IOException
Redirect to the referrer. This method redirects to the referrerand copies the request parameters.- Parameters:
request
- The current requestres
- The current response- Throws:
java.io.IOException
- if redirection caused an error- Since:
- 5.2
-
checkFormStructure
@Deprecated public static Resource checkFormStructure(Resource rsrc)
Deprecated.useFormStructureHelper.updateFormStructure(Resource)
instead.Fix the form. When a form start is added, add automatically a forms end (if missing) When only a form end is on the page remove it- Since:
- 5.2
-
encodeValue
public static java.lang.String encodeValue(java.lang.String value)
Url encode the value. The value is encoded with character set UTF-8.- Parameters:
value
- The value- Returns:
- The encoded value.
- Since:
- 5.2
-
decodeValue
public static java.lang.String decodeValue(java.lang.String value)
Url decode the value. The value is decoded with character set UTF-8.- Parameters:
value
- The value- Returns:
- The decoded value.
- Since:
- 5.2
-
getCss
public static java.lang.String getCss(ValueMap props, java.lang.String defaultCss)
Return the css classes for the field. If the field has a propertyFormsConstants.ELEMENT_PROPERTY_CSS
the value of this property is appended to the default css. If not, only the default css is returned.- Parameters:
props
- The field properties.defaultCss
- The default css for this field.- Returns:
- The css classes
- Since:
- 5.4
-
getReferrer
public static java.lang.String getReferrer(HttpServletRequest request)
Returns the HTTP "referrer" header from the request, and also looks out for the common "referer" misspelling.- Parameters:
request
- current request- Returns:
- value of the referrer header or
null
if not present - Since:
- 5.5
-
setRedirectToReferrer
public static void setRedirectToReferrer(ServletRequest request, boolean redirectToReferrer)
Sets a flag to redirect to the HTTP referrer after the forward of a form POST request. This will usually only be used if no explicit redirect is already given in the ":redirect" parameter used by the Sling POST servlet.- Parameters:
request
- current requestredirectToReferrer
-true
to enable the redirect to the referrer- Since:
- 5.5
-
isRedirectToReferrer
public static boolean isRedirectToReferrer(ServletRequest request)
Returns whether there should be a redirect to the HTTP referrer after the forward of a form POST request.- Parameters:
request
- current request- Returns:
true
if there should be a redirect to the referrer- Since:
- 5.5
-
getLocale
public static java.util.Locale getLocale(SlingHttpServletRequest request)
Returns the locale. If the request originates from a jsp, it returns the locale as defined by theLanguageManager
, otherwise it returnsnull
- Parameters:
request
- The current request- Returns:
- the locale or
null
if not determinable
-
getLocalizedMessage
public static java.lang.String getLocalizedMessage(java.lang.String msg, SlingHttpServletRequest request)
Returns the localized message. If the request originates from a jsp, it returns the localized message based on theResourceBundle
of the request and the locale defined by theLanguageManager
. Otherwise it returns the original message.- Parameters:
msg
- The message to be localizedrequest
- The current request- Returns:
- the localized message or the original message if it cannot be localized
-
getWhitelistPatterns
public static java.lang.String[] getWhitelistPatterns(SlingHttpServletRequest req)
Get the list of white listed data name patterns.- Parameters:
req
- The current request.- Returns:
- The array of white listed data name patterns
-
allowExpressions
public static boolean allowExpressions(SlingHttpServletRequest req)
Indicates whether posted form values should be passed through an expression evaluator. It will be up to each component to decide how it react to this attribute.- Parameters:
req
- The current request.- Returns:
true
if expressions should be evaluated on posted values
-
-