We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27376
    • 576 Posts
    Would it be possible to get a super quick example of how to use the MakeForm class to display an xPDOObject? I read in the code that it actually requires MODx to work. Is this true?
    • Yes, the MakeForm class was originally a tool I created for MODx, and the version in xPDO requires MODx because it uses chunks to represent form element templates. It will be moved out of the xPDO project soon, and into the MODx 0.9.7 model where it belongs, along with the MakeTable class.

      The simplest usage of it with xPDO could go something like this (though there are a lot of ways to use it, and this is just one approach)1:

      <?php
      $object= $xpdo->getObject('myClass', $_REQUEST['uid']);
      if (!$object) $object= $xpdo->newObject('myClass');
      
      $xpdo->loadClass('form.MakeForm', XPDO_CORE_PATH, true, true);
      $form= new MakeForm($xpdo);
      
      $excludeFields= array ();
      
      if ($_POST) {
          if (isset ($_POST['save'])) {
              $form->setFormObject($object, $excludeFields);
              $saved= $form->processForm($object, $_POST);
          }
      }
      
      $form->setFormObject($object, $excludeFields);
      $form->formElements['save']= array (
          'type' => 'submit',
          'name' => 'save',
          'value' => 'Save',
      );
      
      return $form->buildForm();
      ?>

      You can also build the form elements an an array definition (this example automatically builds the elements based on the object meta data) manually, so you have more control over it. Likewise, you can process the form however you want; the processForm() method simply automates that for simple cases. There’s probably a lot more I can tell you about it, so please don’t hesitate to ask more questions...

      1 Note that this requires the chunks to be added to MODx, or no elements will be rendered. You can get a set of sample chunks from the original MakeForm distribution here.
        • 27376
        • 576 Posts
        I was hoping to use it for another application I’m working on outside of MODx. From the looks of the code I don’t think it would be too hard to derive (extend) a class and then just populate [tt]$formTemplateCache[/tt] with all my templates for generation. Would that work?

        Thanks for the example! Much appreciated.
        • Quote from: sirlancelot at May 11, 2007, 03:59 PM

          I was hoping to use it for another application I’m working on outside of MODx. From the looks of the code I don’t think it would be too hard to derive (extend) a class and then just populate [tt]$formTemplateCache[/tt] with all my templates for generation. Would that work?

          Thanks for the example! Much appreciated.
          Sure, you would just need to replace the calls to $modx->parseChunk(), getChunk(), etc. with your own functions to get/merge the template content. I’d love to find a way to abstract that, so this could be used with or without MODx interchangeably...then I could keep it in xPDO with the model...