SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: Quick Form help

  1. #1
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Quick Form help

    Does anyone know if it is physically phossible to establish an element in a quick form

    ie:
    PHP Code:
    $form->addElement('text''newUsername','new Username'); 
    but have a predetermined value in the text box when it is displayed.

    For instance, I would like for the the current value of a user's username (or whatever the appropriate field) be taken from a database, and place as the initial value of the element.

    anyone have any ideas? I have looked through the addElement class, and the text class, and niether one allow for a fourth variable (default value).

    what do you think?

  2. #2
    SitePoint Evangelist
    Join Date
    Oct 2001
    Location
    Texas
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Without knowing anything about the class I would say probably not since it isnt developed that way. It wouldnt be too hard to add a fourth element say $vaule='' to the constructor and have the form dispaly include that. Then when you call on the add element it would be
    PHP Code:
     $form->addElement('text''newUsername','new Username'$databaseArray['username']); 

  3. #3
    Ceci n'est pas Zoef Zoef's Avatar
    Join Date
    Nov 2002
    Location
    Malta
    Posts
    1,111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I remember properly you could do something like this:
    PHP Code:
    $form->setDefaults(array('newUsername' => 'Mr. Username')); 
    Rik
    English tea - Italian coffee - Maltese wine - Belgian beer - French Cognac

  4. #4
    SitePoint Evangelist
    Join Date
    Oct 2001
    Location
    Texas
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is this class available somewhere?

  5. #5
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is the text field class


    PHP Code:
    require_once("HTML/QuickForm/input.php");

    /**
     * HTML class for a text field
     * 
     * @author       Adam Daniel <adaniel1@eesus.jnj.com>
     * @author       Bertrand Mansion <bmansion@mamasam.com>
     * @version      1.0
     * @since        PHP4.04pl1
     * @access       public
     */
    class HTML_QuickForm_text extends HTML_QuickForm_input
    {
                    
        
    // {{{ constructor

        /**
         * Class constructor
         * 
         * @param     string    $elementName    (optional)Input field name attribute
         * @param     string    $elementLabel   (optional)Input field label
         * @param     mixed     $attributes     (optional)Either a typical HTML attribute string 
         *                                      or an associative array
         * @since     1.0
         * @access    public
         * @return    void
         */
        
    function HTML_QuickForm_text($elementName=null$elementLabel=null$attributes=null)
        {
            
    HTML_QuickForm_input::HTML_QuickForm_input($elementName$elementLabel$attributes);
            
    $this->_persistantFreeze true;
            
    $this->setType('text');
        } 
    //end constructor
            
        // }}}
        // {{{ setSize()

        /**
         * Sets size of text field
         * 
         * @param     string    $size  Size of text field
         * @since     1.3
         * @access    public
         * @return    void
         */
        
    function setSize($size)
        {
            
    $this->updateAttributes(array('size'=>$size));
        } 
    //end func setSize

        // }}}
        // {{{ setMaxlength()

        /**
         * Sets maxlength of text field
         * 
         * @param     string    $maxlength  Maximum length of text field
         * @since     1.3
         * @access    public
         * @return    void
         */
        
    function setMaxlength($maxlength)
        {
            
    $this->updateAttributes(array('maxlength'=>$maxlength));
        } 
    //end func setMaxlength

        // }}}
        
    //end class HTML_QuickForm_text
    ?> 
    And this is the addElement class

    PHP Code:
    // {{{ addElement()

        /**
         * Adds an element into the form
         * 
         * If $element is a string representing element type, then this 
         * method accepts variable number of parameters, their meaning 
         * and count depending on $element
         *
         * @param    mixed      $element        element object or type of element to add (text, textarea, file...)
         * @since    1.0
         * @return   object     reference to element
         * @access   public
         * @throws   HTML_QuickForm_Error
         */
        
    function &addElement($element)
        {
            if (
    is_object($element) && is_subclass_of($element'html_quickform_element')) {
               
    $elementObject = &$element;
               
    $elementObject->onQuickFormEvent('updateValue'null$this);
            } else {
                
    $args func_get_args();
                
    $elementObject =& $this->_loadElement('addElement'$elementarray_slice($args1));
                if (
    PEAR::isError($elementObject)) {
                    return 
    $elementObject;
                }
            }
            
    $elementName $elementObject->getName();

            
    // Add the element if it is not an incompatible duplicate
            
    if (!empty($elementName) && isset($this->_elementIndex[$elementName])) {
                if (
    $this->_elements[$this->_elementIndex[$elementName]]->getType() ==
                    
    $elementObject->getType()) {
                    
    $this->_elements[] =& $elementObject;
                    
    $this->_duplicateIndex[$elementName][] = end(array_keys($this->_elements));
                } else {
                    return 
    PEAR::raiseError(nullQUICKFORM_INVALID_ELEMENT_NAMEnullE_USER_WARNING"Element '$elementName' already exists in HTML_QuickForm::addElement()"'HTML_QuickForm_Error'true);
                }
            } else {
                
    $this->_elements[] =& $elementObject;
                
    $this->_elementIndex[$elementName] = end(array_keys($this->_elements));
            }

            return 
    $elementObject;
        } 
    // end func addElement
        
        // }}}
        // {{{ addGroup() 
    hope this helps for you that don't know about the Pear::quickform classes.

    Zoef I am confused as to where i would put that and how i would incorporate it withthe addElement class.

    Thanks Guys

  6. #6
    SitePoint Evangelist
    Join Date
    Oct 2001
    Location
    Texas
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    IT appears you copied just the addElement function of the class. There is probably another one he is reffering to in that class, the setDefault function. It its there his code will most likely work.

    Quote Originally Posted by Raskolnikov
    Zoef I am confused as to where i would put that and how i would incorporate it withthe addElement class.

  7. #7
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys...when i was looking through the QuickForm class, i completely missed the setDefaults function.

    thanks for pointing it out. it works great!

  8. #8
    Ceci n'est pas Zoef Zoef's Avatar
    Join Date
    Nov 2002
    Location
    Malta
    Posts
    1,111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, QuickForms is a real timesaver, glad it worked for you.

    Rik
    English tea - Italian coffee - Maltese wine - Belgian beer - French Cognac


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •