mongorilla icon indicating copy to clipboard operation
mongorilla copied to clipboard

Conditional fields

Open michael-may opened this issue 10 years ago • 1 comments

Dependent Fields

Dependent fields allow you to show or hide form elements based on a set of rules you define. This can be because it allows you to surface or remove certain inputs as they are, or are not, needed for completion of your form.

Currently, simple (on/off) dependent fields are supported.

To use this feature you need to apply some attributes to the dependent field's schema.
The object is formatted like this:

"yourDependentField" : {  
    ...  
    "depends" : {  
        "nameOfParentField" : {  
            "value" : "action"  
        }  
    }  
}  

The "value" key should match the value you want the parent to have in order to apply the "action" to the child form. "*" is supported as a wildcard to match any non-empty value.

The "action" value currently supports "show" and "hide".
Forms with the "show" action start off hidden.
Forms with the "hide" action start off visible.

michael-may avatar Sep 19 '14 21:09 michael-may

Hi @michael-may,

Regarding this new feature, I think it's not as generic as I would want. I mean, this should cover your specific problematic, but even with more re-work, it's almost impossible to make it flexible in order to cover a good range of possibilities, for example:

  • Fields that depends on more than one other field
  • Conditional validation
  • Behavior on the dependent fields (on the model) when an action is performed - hidden field should be null?, should keep the value?, etc.

I prefer to keep this outside the trunk... the reason is - It's pretty complex for the reduced functionality, and not so flexible.

In my opinion, the most convenient way to implement this, is to use the non-well documented, but cool way of customizing forms, models, and collections that this project support: Consists on putting a static version of the backend-generated JS file (an interceptor), and make it extend from that one but appending the .base extension. It's maybe hard to explain, but this example should tell you all: https://github.com/gerardobort/mongorilla/commit/54db22678ab54ebff9cdc4ddcc45cf17c6150689

That method is being used to tune the forms for editing built-in users and roles forms.

Thanks a lot for your collaboration on the project :)

gerardobort avatar Sep 20 '14 04:09 gerardobort