We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34078
    • 76 Posts
    Hi folks,

    I've got trouble to use MODx.com.boolean component in CMP. I followed the Developing an Extra in MODX Revolution tutorial (http://rtfm.modx.com/display/revolution20/Developing+an+Extra+in+MODX+Revolution) and it works fine but now I want add a combo-boolean to my creating form, values aren't store in database anymore.

    Appears that values from combo-boolean are sent as string and not boolean, I think.

    Here is the JS part :

    ...
    
    CustomUrls.window.CreateCustomUrl = function(config) {
        config = config || {};
        Ext.applyIf(config,{
            title: _('customurls.customurl_create')
            ,url: CustomUrls.config.connectorUrl
            ,baseParams: {
                action: 'mgr/customurl/create'
            }
            ,fields: [{
                xtype: 'textfield'
                ,fieldLabel: _('customurls.pattern')
                ,name: 'pattern'
                ,anchor: '100%'
            },{
                xtype: 'textfield'
                ,fieldLabel: _('customurls.criteria_key')
                ,name: 'criteria_key'
                ,anchor: '100%'
            },{
                xtype: 'textfield'
                ,fieldLabel: _('customurls.criteria_value')
                ,name: 'criteria_value'
                ,anchor: '100%'
            },{
                xtype: 'modx-combo-usergroup'
                ,fieldLabel: _('customurls.usergroup')
                ,name: 'usergroup'
                ,anchor: '50%'
            },{
                xtype: 'combo-boolean'
                ,renderer: 'boolean' 
                ,fieldLabel: _('customurls.uri')
                ,name: 'uri'
                ,anchor: '50%'
            },{
                xtype: 'combo-boolean'
                ,renderer: 'boolean' 
                ,fieldLabel: _('customurls.override')
                ,name: 'override'
                ,anchor: '50%'
            },{
                xtype: 'combo-boolean'
                ,renderer: 'boolean' 
                ,fieldLabel: _('customurls.active')
                ,name: 'active'
                ,anchor: '50%'
            }]
        });
        CustomUrls.window.CreateCustomUrl.superclass.constructor.call(this,config);
    };
    Ext.extend(CustomUrls.window.CreateCustomUrl,MODx.Window);
    Ext.reg('customurls-window-customurl-create',CustomUrls.window.CreateCustomUrl);
    
    ...
    


    Here the processor part :
    <?php
    class CustomUrlCreateProcessor extends modObjectCreateProcessor {
        public $classKey = 'CustomUrl';
        public $languageTopics = array('customurls:default');
        public $objectType = 'customurls.customurl';
    }
    return 'CustomUrlCreateProcessor';


    And here the xpdo schema :
    <?xml version="1.0" encoding="UTF-8"?>
    <model package="customurls" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" phpdoc-package="customurls" phpdoc-subpackage="model">
    	<object class="CustomUrl" table="customurls" extends="xPDOSimpleObject">
    		<field key="id" dbtype="int" precision="11" phptype="integer" null="false" index="pk"  generated="native" />
    		<field key="pattern" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
    		<field key="criteria_key" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
    		<field key="criteria_value" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
    		<field key="usergroup" dbtype="int" precision="11" attributes="unsigned" phptype="integer" null="false" default="0" />
    		<field key="uri" dbtype="tinyint" precision="1" attributes="unsigned" phptype="boolean" null="false" default="1" />
    		<field key="override" dbtype="tinyint" precision="1" attributes="unsigned" phptype="boolean" null="false" default="1" />
    		<field key="active" dbtype="tinyint" precision="1" attributes="unsigned" phptype="boolean" null="false" default="1" />
    	</object>
    </model>


    Have I to test and convert manually returned values to booleans before save ? Or is there another way to do ?

    Thanks
    • I was more than pleased when using MIGxDB to create a CMP; it did almost all the "dirty" work and made things so much easier.

      http://rtfm.modx.com/display/ADDON/MIGXdb.Create+doodles+manager+with+help+of+MIGXdb
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 33968
        • 863 Posts
        You can try the 'hiddenName' attribute:
        {
                    xtype: 'combo-boolean'
                    ,renderer: 'boolean'
                    ,fieldLabel: _('customurls.uri')
                    ,name: 'uri'
                    ,hiddenName: 'uri'
                    ,anchor: '50%'
                },{
                    xtype: 'combo-boolean'
                    ,renderer: 'boolean'
                    ,fieldLabel: _('customurls.override')
                    ,name: 'override'
                    ,hiddenName: 'override'
                    ,anchor: '50%'
                },{
                    xtype: 'combo-boolean'
                    ,renderer: 'boolean'
                    ,fieldLabel: _('customurls.active')
                    ,name: 'active'
                    ,hiddenName: 'active'
                    ,anchor: '50%'
                }
        
          • 34078
          • 76 Posts
          @sottwell
          I'll take a look at MIGXdb, thanks.

          @Lucas
          I already try the 'hiddenName' attribute but it doesn't work either, returned value are still string.

          Here is the form data from Chrome web inspector :
          pattern:pattern
          criteria_key:
          criteria_value:
          group:
          uri:true
          override:
          active:true
            • 33968
            • 863 Posts
            true is not a string, that's the expected result and xpdo should insert it as 1/0 into your database.

            You can always manipulate this in your processor if it still doesn't work:
            <?php
            class CustomUrlCreateProcessor extends modObjectCreateProcessor {
                public $classKey = 'CustomUrl';
                public $languageTopics = array('customurls:default');
                public $objectType = 'customurls.customurl';
            
                public function beforeSave() {
                    $uri = $this->getProperty('uri') == true ? 1 : 0; 
                    $this->object->set('uri', $uri);
                    return !$this->hasErrors();
                }
            }
            return 'CustomUrlCreateProcessor';
            
              • 34078
              • 76 Posts
              In fact, I did a var_dump on my uri property in the beforeSave method and it's a string.
              But when I update records from the MODExt grid, the uri property is a boolean. Probably because datas are sent in JSON and not POST.

              So for now, I test and convert my properties to boolean in beforeSave method :
              $uri = $this->getProperty('uri',false) == 'true' ? true : false;
              


              And you're right, I had to add hiddenName attribute to work.;) Thanks
                • 33968
                • 863 Posts
                I had to investigate this a bit further smiley

                The code for the 'system/settings/create' processor (which uses combo-boolean for some settings) contains the following:
                    public function beforeSave() {
                        /* type parsing */
                        $xtype = $this->getProperty('xtype','textfield');
                        $value = $this->getProperty('value');
                        if ($xtype == 'combo-boolean' && !is_numeric($value)) {
                            if ($value == 'yes' || $value == 'Yes' || $value == $this->modx->lexicon('yes')) {
                                $this->object->set('value',1);
                            } else {
                                $this->object->set('value',0);
                            }
                        }
                

                So that's how you would get it to work without 'hiddenName'.

                Surely it would have been better to set the combo up to return a straight '1' or '0'? Anyway...
                  • 34078
                  • 76 Posts
                  Ah thanks Lucas ! So there is no magic way to achieve that ! smiley
                  Really thanks you, I didn't think to take a look to MODx core.
                  • You could use the $this->setCheckbox('fieldname',true); in the beforeSet function of your processor, which basically checks the value and converts it to a proper boolean.
                      Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                      Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
                      • 34078
                      • 76 Posts
                      After all, there is a magic way ! wink It works like a charm.
                      Thanks Mark.