Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RequiredIf validation needs a custom snippet to work #32

Open
hugopeek opened this issue Sep 3, 2016 · 0 comments
Open

RequiredIf validation needs a custom snippet to work #32

hugopeek opened this issue Sep 3, 2016 · 0 comments

Comments

@hugopeek
Copy link
Owner

hugopeek commented Sep 3, 2016

RequiredIf validation

The trick will be to add a field, or 2 fields, to the JSON output of each field, telling the fbValidateProcessJSON snippet which fields are only required if a show/hide checkbox OR radio is selected.

Right now, it’s hardcoded like this:

{"chunk_properties":,"field":26,"settings":{"field_name":"Gebruikersnaam","field_required":"1","field_layout":"form-horizontal","field_type":"text","field_placeholder":"","field_parent":"accountgegevens"}},

This is because the field_parent cannot get the contents of the [[+legend]] layout setting from it’s parent layout. By adding that value to a field inside a collapsible fieldset, and only when it is set as collapsible, then the fbValidateProcessJSON snippet can be rewritten to check that first, before generating the normal validation row.

We need to write a snippet to accomplish this, similar to the existing cbGetFieldContent snippet, to go over the layouts and check if it is a collapsible layout, and if so: get the value of the [[+legend]] field of the fieldset.

Can we do that? Or can Mark do that?

Processing the JSON output

Once finished, we need to hook up certain things in the JSON output. field_parent values in the input fields need to be matched with collapse_target of the radio button or checkbox that toggles the fieldset show/hide.

Based on the following JSON output:

[  
   {  
      "chunk_properties":,
      "field":26,
      "settings":{  
         "field_name":"Gebruikersnaam",
         "field_required":"1",
         "field_layout":"form-horizontal",
         "field_type":"text",
         "field_placeholder":"",
         "field_parent":"accountgegevens"
      }
   },
   {  
      "chunk_properties":,
      "field":26,
      "settings":{  
         "field_name":"Wachtwoord",
         "field_required":"1",
         "field_layout":"form-horizontal",
         "field_type":"text",
         "field_placeholder":"",
         "field_parent":"accountgegevens"
      }
   },
   {  
      "chunk_properties":,
      "field":26,
      "settings":{  
         "field_name":"Naam",
         "field_required":"1",
         "field_layout":"form-horizontal",
         "field_type":"text",
         "field_placeholder":"",
         "field_parent":"account-aanmaken"
      }
   },
   {  
      "chunk_properties":,
      "field":26,
      "settings":{  
         "field_name":"Adres",
         "field_required":"1",
         "field_layout":"form-horizontal",
         "field_type":"text",
         "field_placeholder":"",
         "field_parent":"account-aanmaken"
      }
   },
   {  
      "chunk_properties":,
      "field":26,
      "settings":{  
         "field_name":"Telefoon",
         "field_required":"1",
         "field_layout":"form-horizontal",
         "field_type":"text",
         "field_placeholder":"",
         "field_parent":"account-aanmaken"
      }
   },
   {  
      "chunk_properties":,
      "field":26,
      "settings":{  
         "field_name":"Email",
         "field_required":"1",
         "field_layout":"form-horizontal",
         "field_type":"text",
         "field_placeholder":"",
         "field_parent":"account-aanmaken"
      }
   }
][  
   {  
      "chunk_properties":,
      "field":27,
      "settings":{  
         "field_name":"Nog wat te miepen?",
         "field_required":"1"
      }
   }
][  
   {  
      "rows":[  
         {  
            "field_option":{  
               "value":"Ja",
               "fieldId":"contentblocks-field-2"
            },
            "collapse_target":{  
               "value":"accountgegevens",
               "fieldId":"contentblocks-field-3"
            }
         },
         {  
            "field_option":{  
               "value":"Nee",
               "fieldId":"contentblocks-field-4"
            },
            "collapse_target":{  
               "value":"account-aanmaken",
               "fieldId":"contentblocks-field-5"
            }
         }
      ],
      "field":40,
      "settings":{  
         "field_name":"Heb je al een account?",
         "field_type":"radio",
         "field_helptext":"",
         "field_required":"1",
         "field_layout":"form-horizontal",
         "option_layout":"inline"
      }
   }
]

Can we do something like this?

foreach field = 40 {
    set variable: collapse_parent_field_name = (settings > field_name)
    set variable: collapse_parent_target = (rows > collapse_target > value)

    collapse_parent_trigger {
        if {
            row contains collapse_parent_target
        } then {
            select from same row: (field_option > value)
        }
    }

    look through all JSON fields for: collapse_parent_target == (settings > field_parent)

    foreach match {
        row = field_name . :requiredIf=^ . collapse_parent_field_name . == . collapse_parent_trigger . ^,
    }
}

Desired output

In the end, the output should be like this:

fb19-gebruikersnaam:requiredIf=^fb19-=="Ja"^,

[[prefix]]-[[field_name]]:requiredIf=^[[prefix]]-[[fieldname_of_showhide_radio]]==“[[value_of_showhide_radio]]”^,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant