1
bumciach
Form in form
  • 2009/8/14 8:13

  • bumciach

  • Not too shy to talk

  • Posts: 153

  • Since: 2007/6/25


Let's say we have two class to define form elements (for example, the module mytabs):
class MytabspageForm extends XoopsThemeForm {
    function 
createElements($target) {
        
$this->addElement(new XoopsFormText(_AM_MYTABS_TITLE'pagetitle'35255$target->getVar('pagetitle''e')));
       [...]
    }
}

class MytabsTabForm extends XoopsThemeForm {
    function 
createElements($target) {
        
$this->addElement(new XoopsFormText(_AM_MYTABS_TITLE'tabtitle'35255$target->getVar('tabtitle''e')));

        
$this->addElement(new XoopsFormDateTime(_AM_MYTABS_PUBLISHDATE'tabfromdate'15$target->getVar('tabfromdate''e')));

        [...]
    }
}

Is it possible to incorporate elements of the form in one class to another?
For example...
$form = new MytabsTabForm;
$form->createElements();
echo 
$form->render();

could show TabForm with elements from PageForm (between 'tabtitle' and 'tabfromdate').
You know... If you want to change the items in PageForm you need to modify this one class rather than two.

It does not concern me about this particular module, but the overall concept. How to solve for classes?

2
ghia
Re: Form in form
  • 2009/8/14 9:23

  • ghia

  • Community Support Member

  • Posts: 4953

  • Since: 2008/7/3 1


XoopsForm is a rather simple class for making HTML forms. It needs that the formelements (elements that make the form, like inputs and texts) are added in the order of displaying to the form.
I do not fully understand your question, (because the references to your example code is not always clear or consequent) but I assume you would like to have some regular used group of formelements as form blocks, which are then to be used to build the form.
There are several ways to work this out:
- You extend the form class to eg BlockThemeForm with a function that can add its elements to another form eg AddToForm .
From that BlockThemeForm you can derive a new form eg MyFormTabs, that adds automatic some elements in its construct (similar to your examples).
Then you do:
$mainform = new XoopsThemeForm;
$formtabs = new MyFormTabs;
$formtabs->AddToForm($mainform); 
echo 
$mainform->render();

Other way is to extend FormElements to have more items at once. There is already an element that does this in a grouped manner eg XoopsFormElementTray An extension or similar class, could also allow to add some grouped elements to a form.
Have also a look at the cross reference documentation at XooFoo.

3
trabis
Re: Form in form
  • 2009/8/14 10:18

  • trabis

  • Core Developer

  • Posts: 2269

  • Since: 2006/9/1 1


/**
     * Get the form for adding or editing tabs
     *
     * @return MytabsTabForm
     */
    
function getForm() {
        include_once(
XOOPS_ROOT_PATH."/modules/mytabs/class/tabform.php");
        
$form = new MytabsTabForm('Tab''tabform''tab.php');
        
$form->createElements($this);
        return 
$form;
    }


This code is inside MytabsTab class and calls that form by passing itself into it ($this).

The Form class is dependent on this Object class. Actually, there was no need to have a form class. I could have build the form inside the object class.

Anyway, after you create elements you can use getElements()

$myelements[] = $form1->getElements();
$myelements[] = $form2->getElements();

each element can be rendered with no need to be inside a form.
you can do
echo "foreach ($myelements as $ele) {echo $ele->render();}
echo "";

Or you can build a themeform and use the label element containing the rendered elements.


4
bumciach
Re: Form in form
  • 2009/8/14 12:35

  • bumciach

  • Not too shy to talk

  • Posts: 153

  • Since: 2007/6/25


Quote:
I do not fully understand your question, (because the references to your example code is not always clear or consequent)

Maybe I show what I did before without OOP (in simplifying):
function form_to_include(&$formObj

  
$formObj->addElement(new XoopsFormText(_PF_COST'cost'2010$row_fromDB['cost']), false);
  return 
$formObj;
}

function 
mainform(&$formObj

  
$formObj->addElement(new XoopsFormText(_PF_TITLE'title'35255$row_fromDB['title']));
  
  
$tray = new XoopsFormElementTray'COST''' );    
  
$tray form_to_include$tray );
  
$formObj->addElement$trayfalse );

  
$formObj->addElement(new XoopsFormText(_PF_DETAILS'details'35255$row_fromDB['details']));

  return 
$formObj;
}

$cform = new XoopsThemeForm('MYFORM'"frmStage"'test.php');
$cform->setExtra('enctype="multipart/form-data"');
     
$mainform mainform($cform);
   
$cform-> addElement($mainformfalse);
   
$cform->addElement(new XoopsFormButton("""submit"'OK'"submit"));
     
$cform->display();

It is working what I expected.

Quote:

trabis wrote:
The Form class is dependent on this Object class. Actually, there was no need to have a form class. I could have build the form inside the object class.

I know that. But I like yours code and the way you have it organized. :)
I want to achieve this the definition of each form is included in a separate class. Simplicity and easy to maintain for me.

I think that this example with AddToForm method is the nearest what I want to and most flexible.

Thanks guys!

Login

Who's Online

376 user(s) are online (299 user(s) are browsing Support Forums)


Members: 0


Guests: 376


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits