API/Form

From aMember Pro Documentation
Revision as of 06:24, 6 June 2016 by Caesar (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

aMember Pro utilizes [HTML_QuickForm2] library to handle forms displaying and validation. Please check usage example of HTML_QF2 to get an understanding of how it works:

[QF2 Tutorial] [DataSources] [Rules and Validation]

aMember Pro defines a class Am_Form derived from HTML_QuickForm2 and adds the following:

  • function isSubmitted() // @return bool
  • function setAction($action) // to set "action" form attribute
  • function removeElementByName($name) // to remove element from form by its name
  • adds jquery.validate support for basic HTML_QF2 rules

New elements and rules

  • 'callback2' validation rule: it runs a callback and validation fails if function returned string value, validation succeed if null returned
  • 'advradio' element - configured like the 'select', but displayed as group of radiobuttons
  • 'upload' element - handles uploads with history and ability to select from already uploaded files for this tag
  • 'date' element - to provide JQuery UI datepicker support
  • 'htmleditor' element - to provide CKeditor suppor
  • 'magicselect' element - to provide new original UI element for multi-select boxes


Sample Usage

Put this code into file named MyPageController.php and copy to am/application/default/controllers/ , then try access http://example.com/am/my-page

<?php
class MyPageController extends Am_Mvc_Controller 
{
    function indexAction()
    {
        $form = new Am_Form;
        $t1 = $form->addText('t1', array('size'=>40))->setLabel('Text 1');
        $t1->addRule('required', 'This field is required');
        $s = $form->addMagicSelect('s1');
        $s->loadOptions(array('o1' => 'Option 1', 'o2' => 'Option 2'));
 
        if ($form->isSubmitted() && $form->validate())
        {
            // form ok - do some actions here!
            $this->view->content = print_r($form->getValue(), true);
        } else {
            // not submitted or validation fails
            $this->view->content = (string)$form;
        }
        $this->view->title = "My Page";
        $this->view->display('layout.phtml');
    }
}
 
?>