Skip to content

Switch

Description

A Switch component in a form is a visual and interactive element that users can toggle between two distinct states, typically labeled on and off.

It offers a clear and efficient way for users to select one of two mutually exclusive options within a form field.

Use Case

  1. Enable/Disable features
  2. Select preferences
  3. Toggle visibility
  4. Confirm binary choices
  5. Control notifications
  6. Marketing consent
  7. Data sharing consent
  8. Public/Private visibility
  9. Subscription status
  10. Content filtering
  11. Automatic form submission
  12. Form validation control
  13. Additional form sections
  14. Error correction confirmation
  15. Password visibility

Interactivity

  1. Multi-state switches
  2. Conditional interactions
  3. Animated transitions

API

Events

Name Description
onChange (element) The onChange event occurs when the value of a field is changed

Methods

Name Description Parameters
this.addClassName Add style class to a form item (fields: String|String[], className: String)
this.removeClassName Remove style class from a form item (fields: String|String[], className: String)
this.getValues Gets the current values of all fields () : Object
this.hide Hides the field (fields: String|String[])
this.show Displays the field (fields: String|String[])
this.disable Disable switch field from user interaction (fields: String|String[])
this.enable Enable switch field from user interaction (fields: String|String[])
this.getValue Get A Value From a component (fieldName: String)
this.getComponent Returns a component whose id has been passed as a parameter (component_ID: String) : Object
this.setData Set the data in the field. The Value object should be of type { componentId: componentValue } (Value: Object)

Info

  1. The show() and hide() methods can also be used to control the visibility of a switch in response to user input.
  2. Fields refers to a component ID or a list of component IDs. You can fetch the ID from the Component Attribute panel in the Page Builder.
  3. Before using this.show(fields), make sure the component is hidden. This can be done using this.hide(fields) or by enabling the Hidden checkbox in the Component Attribute panel.
  4. Before using this.enable(fields), make sure the component is disabled. This can be done using this.disable(fields) or by enabling the Disabled checkbox in the Component Attribute panel.

Steps to use the methods for the Page Builder components

  1. Go to Form Attribute Action Panel Setting (Mounted | refresh | click 'Add action').
  2. Write the method/code as shown in the Example below.
  3. Click on Save.
  4. On the main screen click on Save again.
  5. Click on Preview to see the code in action.

Example

  1. this.addClassName(fields, className)

    • Go to Form Attribute Style Sheets add the class
      .abc{ // abc is the class name
      background-color: red;
      }
      
    • Follow the steps mentioned above, under Steps to use the methods for the Page Builder components
    • this.addClassName('switch_ornfn03c', 'abc')
  2. this.removeClassName(fields, className)

    this.removeClassName('switch_ornfn03c', 'abc')
    

  3. this.getValues()

    var data = this.getValues();
    console.log(data);
    

  4. this.hide(fields)

    var fields= ['switch_ornfn03c']
    this.hide(fields)
    

  5. this.show(fields)

    var fields= ['switch_ornfn03c']
    this.show(fields)
    

  6. this.disable(['fields'])

    this.disable(['switch_ornfn03c'])
    

  7. this.enable(['fields'])

    this.enable(['switch_ornfn03c'])
    

  8. this.getValue('fieldName')

    var switchname = this.getValue('switch_ornfn03c');
    console.log('getValue', switchname);
    

  9. this.getComponent('component_ID')

    var switchname = this.getComponent('switch_ornfn03c');
    console.log('getComponent', switchname);
    

  10. this.setData(Value)

    this.setData(
     {
     "switch_ornfn03c": true,}
    );
    

Config

Name Description Icon
ID The switch component's unique identifier
Name [Optional] The display name of the switch field
Width [Optional] Width of the field
Label Width Width of the label associated with an input field. It determines the horizontal space occupied by the label text
Label Wrap If the label is longer than the allowed width the text will continue on another line
Hide Label Hides the label on the form
Text Prompt A description to aid the user when completing the field
Custom Class An HTML class attribute which allows further customisation See Form Attribute > Style Sheets
Attribute Action Enable Data Binding to connect the data to UI.
Enable Hidden action to hide the field.
Enable Disabled action to make the field unusable.
Action Settings Click on the drop-down to select the pre-defined methods you wish to apply to your component. In this case, you can choose either mounted or refresh for onChange

Validation

Form validation is the process of checking the data entered into a form to ensure that it's valid and complete.

This helps to prevent users from submitting forms with invalid data, which can cause problems for the application that's processing the form.

Form validation can be performed using a variety of methods, including:

Name Description
Required If enabled, then the field value can't be empty, otherwise an error message is emitted
Custom Validation Rules (rule, value, callback) => {
rule: Verification rule, you can view the verification configuration information through this parameter; rule.field can get the field identifier of the current verification
value: Value of the current field
callback: Callback function (must be called) upon completion of validation; callback('Error message')/ callback(new Error('Error message')). These are two ways to return an error message

Info

  1. The callback() function is also called to verify success in the custom validation method.

First time User?

If you are using the Page Builder components on the ConnexCS platform for the first time, we request you to use our guide on steps to use the Components.