[migxdb] Call to on config attribute of form field snippet

Hi All,

Similarly to the “inputOptionValues” or “restrictive_condition”, I can call snippets to allow dynamic results.
I am trying to do the same for the “config”.

Here is what I am trying to do:
snippet called “nowdate”:

<?php
/** 
 * 
 * this will return json structures string like: 
 * {"allowBlank":"false","disabledDates":"0000-00-00","disabledDays":"","minDateValue":","minTimeValue":"","maxDateValue":"2019-04-27", "maxTimeValue":"","startDay":"","timeIncrement":"", "hideTime":true}
 * 
*/

$object = array(
   'allowBlank' => 'false',
   'disabledDates' => '0000-00-00',
   'disabledDays' => '',
   'minDateValue' => '',
   'minTimeValue' => '',
   'maxDateValue' => date("Y-m-d"),
   'maxTimeValue' => '',
   'startDay' => '',
   'timeIncrement' => '',
   'hideTime' => 'true',
);

$json = $modx->toJSON($object);

return $json;

now my config file where I am trying to apply this (objective is to have maxDateValue as today date):

{
  "MIGX_id":6,
  "field":"id_validity",
  "caption":"ID validity date",
  "description":"ID Card validity date (Format: AAAA-MM-DD)",
  "description_is_code":"0",
  "inputTV":"",
  "inputTVtype":"date",
  "validation":"required",
  "configs":"[[!nowdate]]",
  "restrictive_condition":"",
  "display":"",
  "sourceFrom":"config",
  "sources":"",
  "inputOptionValues":"",
  "default":"",
  "useDefaultIfEmpty":"0",
  "pos":6
}

This does not seem to be working this way. but when I call the snippet on a chunk or on the “default” attribute of a text field, the json will sent properly (copied, pasted by hand on the config, and Migx reads it, so it is well formed).

Do you know if config attribute supports snippet call for this kind of approach?

EDIT:
Or maybe use bindings inside each option? I tried but didn’t work (Not parsed):

"configs":{
	"allowBlank":true,
	"disabledDates":"0000-00-00",
	"disabledDays":"",
	"minDateValue":"",
	"minTimeValue":"",
	"maxDateValue":"@SELECT DATE_FORMAT(NOW(),'%Y-%m-%d');",
	"maxTimeValue":"",
	"startDay":"",
	"timeIncrement":"",
	"hideTime":true
},

Thanks in advance.
JNogueira

Hi All,

Answering my own question:
What i am trying to do is not related with migx itself but with the options of the Input TV type date that is part of modx core.
But it is possible to do with a very small change that I will explain below:

On the modx folder /manager/templates/default/element/tv/renders/input/ you will find a file called date.tpl that contains all the backend template code that will render the input type date TV.

By opening the file, this is what we have:

<input id="tv{$tv->id}" type="hidden" class="datefield"
    value="{$tv->value}" name="tv{$tv->id}"
    onblur="MODx.fireResourceFormChange();"/>

<script type="text/javascript">
// <![CDATA[
{literal}
Ext.onReady(function() {
    var fld = MODx.load({
    {/literal}
        xtype: 'xdatetime'
        ,applyTo: 'tv{$tv->id}'
        ,name: 'tv{$tv->id}'
        ,dateFormat: MODx.config.manager_date_format
        ,timeFormat: MODx.config.manager_time_format
        {if $params.disabledDays|default},disabledDays: {$params.disabledDays|default}{/if}
        {if $params.minDateValue|default},minDateValue: '{$params.minDateValue|default}'{/if}
        {if $params.maxDateValue|default},maxDateValue: '{$params.maxDateValue|default}'{/if}
        {if $params.startDay|default},startDay: {$params.startDay|default}{/if}

        {if $params.minTimeValue|default},minTimeValue: '{$params.minTimeValue|default}'{/if}
        {if $params.maxTimeValue|default},maxTimeValue: '{$params.maxTimeValue|default}'{/if}
        {if $params.timeIncrement|default},timeIncrement: {$params.timeIncrement|default}{/if}
        {if $params.hideTime|default},hideTime: {$params.hideTime|default}{/if}

        ,dateWidth: 198
        ,timeWidth: 198
        ,allowBlank: {if $params.allowBlank == 1 || $params.allowBlank == 'true'}true{else}false{/if}
        ,value: '{$tv->value}'
        ,msgTarget: 'under'
    {literal}
        ,listeners: { 'change': { fn:MODx.fireResourceFormChange, scope:this}}
    });
    Ext.getCmp('modx-panel-resource').getForm().add(fld);
});
{/literal}
// ]]>
</script>

It is possible to see that all the attributes for the inputTVtype configuration are here and this is the TPL that is called by the rendering.
Everything is converted to javascript for the backend form generator like this:

<script type="text/javascript">
// <![CDATA[

Ext.onReady(function() {
    var fld = MODx.load({
    
        xtype: 'xdatetime'
        ,applyTo: 'tvinp_195_1_5'
        ,name: 'tvinp_195_1_5'
        ,dateFormat: MODx.config.manager_date_format
        ,timeFormat: MODx.config.manager_time_format
        ,maxDateValue: '2019-04-27'        
        ,hideTime: 1
        ,dateWidth: 198
        ,timeWidth: 198
        ,allowBlank: true
        ,value: ''
        ,msgTarget: 'under'
        ,listeners: { 'change': { fn:MODx.fireResourceFormChange, scope:this}}
    });
    Ext.getCmp('modx-panel-resource').getForm().add(fld);
});

// ]]>
</script>

If you change the date.tpl to this, you will be able to use smarty on the config settings of your migxdb config json file:

<input id="tv{$tv->id}" type="hidden" class="datefield"
    value="{$tv->value}" name="tv{$tv->id}"
    onblur="MODx.fireResourceFormChange();"/>

<script type="text/javascript">
// <![CDATA[
{literal}
Ext.onReady(function() {
    var fld = MODx.load({
    {/literal}
        xtype: 'xdatetime'
        ,applyTo: 'tv{$tv->id}'
        ,name: 'tv{$tv->id}'
        ,dateFormat: MODx.config.manager_date_format
        ,timeFormat: MODx.config.manager_time_format
        {if $params.disabledDays|default},disabledDays: {$params.disabledDays|default}{/if}
        {if $params.minDateValue|default},minDateValue: '{eval var=$params.minDateValue|default}'{/if}
        {if $params.maxDateValue|default},maxDateValue: '{eval var=$params.maxDateValue|default}'{/if}
        {if $params.startDay|default},startDay: {$params.startDay|default}{/if}

        {if $params.minTimeValue|default},minTimeValue: '{eval var=$params.minTimeValue|default}'{/if}
        {if $params.maxTimeValue|default},maxTimeValue: '{eval var=$params.maxTimeValue|default}'{/if}
        {if $params.timeIncrement|default},timeIncrement: {$params.timeIncrement|default}{/if}
        {if $params.hideTime|default},hideTime: {$params.hideTime|default}{/if}

        ,dateWidth: 198
        ,timeWidth: 198
        ,allowBlank: {if $params.allowBlank == 1 || $params.allowBlank == 'true'}true{else}false{/if}
        ,value: '{$tv->value}'
        ,msgTarget: 'under'
    {literal}
        ,listeners: { 'change': { fn:MODx.fireResourceFormChange, scope:this}}
    });
    Ext.getCmp('modx-panel-resource').getForm().add(fld);
});
{/literal}
// ]]>
</script>

Mind the fields minDateValue, maxDateValue, minTimeValue and maxTimeValue that now have a preffix:
eval var=

This is the way to allow smarty to read a variable content and parse it again if it is valid smarty code (smarty recursive parsing).

With this you will be able to use smarty in migxdb config attributes for inputTVtype date:

{
  "MIGX_id":5,
  "field":"id_issue_date",
  "caption":"ID Issued On",
  "description":"ID Issue date (format: 2000-01-01). You can only pick dates on the past ",
  "description_is_code":"0",
  "inputTV":"",
  "inputTVtype":"date",
  "validation":"",
  "configs":{
	"allowBlank":true,
	"disabledDates":"0000-00-00",
	"disabledDays":"",
	"minDateValue":"",
	"minTimeValue":"",
	"maxDateValue":"{$smarty.now|date_format:'%Y-%m-%d'}",
	"maxTimeValue":"",
	"startDay":"",
	"timeIncrement":"",
	"hideTime":true
  },
  "restrictive_condition":"",
  "display":"",
  "sourceFrom":"config",
  "sources":"",
  "inputOptionValues":"",
  "default":"",
  "useDefaultIfEmpty":"0",
  "pos":5
}

This is the result:
Untitled

Now it will change the max date according to the present date.

Hope it helps other people seeking for the same solution.

Cheers all.
J.Nogueira

Opened a change request to propose this change:

Hi @bruno17,

One question from my side concerning this topic as it is now l inked to migxdb directly:
I created a custom manager template where I preserved the same folder structure like the default theme including only the date.tpl file on the respective folder location.
Apparently migxdb is not using the manager_theme files on form load with inputTVtype defined for various fields (not only date type).

I did a small test:
I went to the default directory and changed the name of the date.tpl. Then assigned a date TV to a template and I was able to edit it (on debug, it was possible to see that manager theme was getting the correct file from custom theme defined on the manager_theme setting).
Migx started to throw an error of not found file (smarty template not found). As soon as I renamed back to date.tpl on the default theme directory, all went just fine.

I know that you made a lot of adjustments on previous releases of migx.

Am I missing any configuration on migx side that can define the base theme that migx should use independently from the manager_theme (like an override on the site settings on migx namespace)?

Many thanks in advance

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.