sfContains

Checks whether the search value is present in the provided collection, and returns true if the value is present in the collection otherwise returns false.

Supported from version 8.0.0 onward.

Syntax

sfContains (collection, searchValue, index)

Parameters

Name

Type

Mandatory

Description

Name

Type

Mandatory

Description

collection

comma-separated string or an array

yes

Collection to search for the provided value in. The collection can be one of the following:

  • A comma separated string of values

  • A JavaScript array

searchValue

string

yes

Value to be searched in the provided collection.

index

number

no

Index of the form control. Applicable only if the control is present inside a group control.

The index is a zero-based index.

Usage examples

Example 1

Let us assume a form contains a multiple choice control Products that displays the list of products involved in a cargo operation: Oil, Gas and Chemical. The code of the choices are O, G and C respectively.

It can be verified if the user has selected Oil as a choice using,

if(sfContains(sfGetValue('control_products'),'O')) { // do something }

Here, sfGetValue returns the list of product codes selected by the user and sfContains returns true if Oil is one of the products selected, false otherwise.

Example 2

Consider a variation in the example above where the multiple choice control Products is inside a group control CargoOperations. To check if the user has selected Oil in at least one of the rows in the group,

for(var i = 0; i < sfField('div_cargooperations').recordCount(true); i++) { if(sfContains(sfGetValue('control_products'),'O',i)) { // do something } }

Here, the recordCount method returns the number of rows present in the group control (the parameter true includes unsaved records as well).

Example 3

To check if the current logged in user has a specific role named Planner,

if(sfContains(sfGetCurrentUser("Roles"),"Planner") { // do something }

Here, sfGetCurrentUser(“Roles”) returns a comma separated list of the roles that the currently logged in user has.

Example 4

The function can search for a value in a JavaScript array as well as a comma separated string of values.