Skip to content

Visible parameter #149

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

Closed
reardestani opened this issue Mar 10, 2017 · 2 comments
Closed

Visible parameter #149

reardestani opened this issue Mar 10, 2017 · 2 comments
Labels

Comments

@reardestani
Copy link

Hi,

I am building the schema in a PHP array then convert it to JSON for the front-end. The problem is that the function will be wrapped in quote so it won't work.

PHP array

array(
    'label' => __('Comments on Pages', 'mk_framework') ,
    'desc' => __('Using this option you can enable comments for pages.', 'mk_framework') ,
   'model' => 'pages_comments',
   'default' => 'false',
   'type' => 'mk-toggle',
   'styleClasses' => 'col-md-6',
   'visible' => "function(model) {return model && model.smoothscroll == true;}"
),

Generated JSON

default:"false"
desc:"Using this option you can enable comments for pages."
label:"Comments on Pages"
model:"pages_comments"
styleClasses:"col-md-6"
type:"mk-toggle"
visible:"function(model) {return model && model.smoothscroll == true;}"

1- Is it possible I can simply pass a array for visibility and it detects the visibility.

visible: array(
   model.id => true,
   model.color => 'red'
)

2- I assume first option is not possible but is there any way for passing function as I explained above?

@lionel-bijaoui
Copy link
Member

lionel-bijaoui commented Mar 10, 2017

I had the same issue.
You can add a function just before passing the schema. What is not documented but the function you associate to visible has 3 parameters:

  • model which is the current model
  • field which correspond to the current input parameters
  • vfg which is the current instance of vue-form-generator
    And armed with this knowledge I added a parameter to the schema (lets call it condition) and created a function that use this option. I'm not going to tell you how I structured my conditions, but based on your proposal, you can do that:
var conditionValue = function(model, field, oldVal) {
	/*
	example of format for condition
	[
		{"model":"id", "value":true},
		{"model":"color", "value":"red"}
	]
	*/
	var conditions = field.condition;
	/*
	score will help to decide if all condition are true
	for every condition, if true, you get 1 point
	if total score if equal the number of conditions, the conditions are validated
	here it will be score/condition.length, so 2/2 for a validation
	This is only an example, there is better ways to do this logic
	 */
	var score = 0;
	for (var i = 0; i < condition.length; i++) {
		if (model[condition[i].model] === condition[i].value) {
			score++;
		}
	}
	if (score === condition.length) {
		// all condition validated, field is visible
		return true
	} else {
		// not a perfect score, fields is not visible
		return false
	}
}

In you function that prepare the schema, you then just need to associate visible to conditionValue before sending it to VFG.

All of this is just an example, you can much more with the logic and the value you get. Good luck !

@reardestani
Copy link
Author

Thanks, nice solution.

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

No branches or pull requests

3 participants