Author Topic: Zend Form building help  (Read 754 times)

0 Members and 1 Guest are viewing this topic.

Offline phpdeveloper82Topic starter

  • Irregular
  • Posts: 45
    • View Profile
Zend Form building help
« on: February 27, 2010, 05:00:57 AM »
Hello,

I need help in building Form elements for the Birthday Input (Label: Birthday and Three select menus - Day, Month & Year).

I don't get how can I build three select menus in the same line, as I am new to Zend Framework.. I need Birthday field must work same as of Facebook Sign Up page (http://facebook.com

Thanks in advance for your help, guidelines & article links.


Offline ignace

  • Guru
  • Freak!
  • *
  • Posts: 5,093
  • Gender: Male
    • View Profile
Re: Zend Form building help
« Reply #1 on: February 27, 2010, 08:57:53 AM »
class MyForm extends Zend_Form {
    const 
ELEMENT_DAY 'day';
    const 
ELEMENT_MONTH 'month';
    const 
ELEMENT_YEAR 'year';
    
    public function 
getDay() { return $this->getValue(self::ELEMENT_DAY); }
    public function 
getMonth() { return $this->getValue(self::ELEMENT_MONTH); }
    public function 
getYear() { return $this->getValue(self::ELEMENT_YEAR); }
    
    public function 
isValid($data) {
        
$isValid true;
        
$day $this->getDay(); $month $this->getMonth(); $year $this->getYear();
        if (!
checkdate($day$month$year)) {
            
$isValid false;
            
$this->getElement(self::ELEMENT_DAY)->addError('Invalid date specified');
        }
        return 
parent::isValid($data) && $isValid;
    }
    
    public function 
init() {
        
$e $this->createElement('Select'self::ELEMENT_DAY);
        
$e->setMultiOptions(range(131));
        
$this->addElement($e);
        
        
$e $this->createElement('Select'self::ELEMENT_MONTH);
        
$e->setMultiOptions(array_combine(range(112), range('January''December')));
        
$this->addElement($e);

        
$e $this->createElement('Select'self::ELEMENT_YEAR);
        
$e->setMultiOptions(range(idate('Y') - 100idate('Y')));
        
$this->addElement($e);
    }
}
Developer from Belgium, Vlaams-Brabant

Offline phpdeveloper82Topic starter

  • Irregular
  • Posts: 45
    • View Profile
Re: Zend Form building help
« Reply #2 on: March 01, 2010, 04:41:23 AM »
Thank you for your help.

Your code helps me to generate the form elements. I need to display all three "select" menus on the same row. Currently it returns like the format:
Code: [Select]
<dt id="day-label">
<label for="day" class="required">Birthday:</label>
</dt>

<dd id="day-element">
<select name="day" id="day">
    <option value="" label="Day:">Day:</option>
    <option value="0" label="1">1</option>
</select>
</dd>

<dt id="month-label">&nbsp;</dt>
<dd id="month-element">
<select name="month" id="month">
    <option value="" label="Month:">Month:</option>

    <option value="0" label="January">January</option>
  <option value="11" label="December">December</option>

</select>
</dd>
<dt id="year-label">&nbsp;</dt>
<dd id="year-element">
<select name="year" id="year">
    <option value="" label="Year:">Year:</option>
    <option value="0" label="1910">1910</option>
</select>
</dd>





Please help me to change to the format:

Code: [Select]

<dt id="day-label">
<label for="day" class="required">Birthday:</label>
</dt>

<dd id="day-element">

<select name="day" id="day">
    <option value="" label="Day:">Day:</option>
    <option value="0" label="1">1</option>
</select>

<select name="month" id="month">
    <option value="" label="Month:">Month:</option>

    <option value="0" label="January">January</option>
  <option value="11" label="December">December</option>

</select>

<select name="year" id="year">
    <option value="" label="Year:">Year:</option>
    <option value="0" label="1910">1910</option>
</select>
</dd>






Offline ignace

  • Guru
  • Freak!
  • *
  • Posts: 5,093
  • Gender: Male
    • View Profile
Re: Zend Form building help
« Reply #3 on: March 01, 2010, 08:20:18 AM »
To do that you first need to disable any and all decorators on the form elements:

public function init() {
    
$this->setDisableLoadDefaultDecorators(true);
}


Then for each element add the appropriate decorators like shown here: http://framework.zend.com/manual/en/zend.form.quickstart.html
Developer from Belgium, Vlaams-Brabant

Offline phpdeveloper82Topic starter

  • Irregular
  • Posts: 45
    • View Profile
Re: Zend Form building help
« Reply #4 on: March 01, 2010, 09:38:57 AM »
Thank you for your reply. I have searched on net the whole day and couldn't find any tutorial which helps me to do as I need.

Also gone through the Decorator section in the ZF site also.