+ Reply to Thread
Results 1 to 4 of 4

Thread: Custom Form Element tutorial wanted

  1. #1
    cabofixe is offline Junior Member
    Join Date
    Nov 2008
    Posts
    1

    Default Custom Form Element tutorial wanted

    Hey everyone.

    I create form classes that extend Zend_Form for my models. When I want to create a form element I type
    Code:
    $company_name = new Zend_Form_Element_Text('company_name');
    What I want to do is create a custom form element for like a phone number so I would type
    Code:
    $phone_number = new Kwista_Zend_Form_Element_Phone_Number('phone_number');
    and when I displayed the form for that element it would show [HTML](<input type="text" size="2" name="phone_number[]">) <input type="text" size="2" name="phone_number[]"> - <input type="text" size="3" name="phone_number[]"> ext. <input type="text" size="3" name="phone_number[]">[/HTML] Are there any tutorials out there that would show me how to do this? Also I want to place it in a nice folder in my application so I can reuse it in any of my other applications.

  2. #2
    chisflorinel is offline Junior Member
    Join Date
    Dec 2008
    Location
    Romania
    Posts
    7

    Default Zend Form reference


  3. #3
    gernberg is offline Junior Member
    Join Date
    Jul 2009
    Posts
    2

    Default Did you solve the problem?

    @cabofixe:
    I'm just wondering if you solved the problem (and if; how :-D)

  4. #4
    Laura Dean is offline Member
    Join Date
    Jun 2009
    Posts
    80

    Default

    I think you would need two files, My_Form_Element_Phonenumber and My_View_Helper_FormPhonenumber.

    Code:
    <?php
    
    class My_Form_Element_Phonenumber extends Zend_Form_Element 
    {
        public $helper = 'formPhonenumber';
    
        public function setValue($value)
        {  
            if(is_array($value))
            {
                @list($first, $second, $third, $ext) = $value;	
                 
                // saving comma-delimited for simplicity, save however you want
                // this is NOT the display
                $value = sprintf('%s,%s,%s,%s', $first, $second, $third, $ext);
            }
    
            return parent::setValue($value);
        }
    
    }
    Code:
    <?
    
    require_once 'Zend/View/Helper/FormElement.php';
    
    class My_View_Helper_Phonenumber extends Zend_View_Helper_FormElement {
        
        public function formPhonenumber($name, $value = null, $attribs = null) {
            $info = $this->_getInfo($name, $value, $attribs);
            extract($info); // name, value, attribs, options, listsep, disable
    
            $value=$this->view->escape($value);
    
            // retrieve from comma-delimited list
            list($first, second, $third, $ext) = split(',', $value);
                
            $html = '(<input type="text" size=2'
                    . ' name="' . $this->view->escape($name) . '[]"'
                    . ' value="' . $first . '">)'
     
            $html = ' <input type="text" size=2'
                    . ' name="' . $this->view->escape($name) . '[]"'
                    . ' value="' . $second . '">'
    
            $html = ' - <input type="text" size=3'
                    . ' name="' . $this->view->escape($name) . '[]"'
                    . ' value="' . $third . '">'
    
            $html = ' ext. <input type="text" size=3'
                    . ' name="' . $this->view->escape($name) . '[]"'
                    . ' value="' . $ext . '">'               
    
            return $html;
        }
    }
    This is untested, loosely based on some of my working code.

+ Reply to Thread

Similar Threads

  1. Tutorial - Form with Zend_Form
    By danieldeveloper in forum Model-View-Controller (MVC)
    Replies: 0
    Last Post: 02-03-2010, 08:32 PM
  2. quickstart tutorial - Newbie no.2 needs Help with Create A Model and Db Tutorial
    By apprentice in forum Installation & Configuration
    Replies: 1
    Last Post: 11-26-2009, 03:49 AM
  3. Form + Custom Element of form
    By yogi in forum Model-View-Controller (MVC)
    Replies: 0
    Last Post: 10-23-2009, 11:24 AM
  4. Custom Form Element with custom ViewHelper
    By ablock in forum Model-View-Controller (MVC)
    Replies: 9
    Last Post: 05-25-2009, 05:04 PM
  5. View helper not used by custom element
    By Laurent in forum General Q&A on Zend Framework
    Replies: 1
    Last Post: 04-09-2009, 03:52 PM

Posting Permissions

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