以Zend形式添加Jquery Datepicker

时间:2011-02-02 01:13:01

标签: zend-framework zend-form

我是Zend框架的新手,我想知道如何将日期选择器jquery小部件添加到zend_form我广泛搜索但找不到任何精确的

请帮助我。提前谢谢!

以下是我的Zend_form代码

表单代码

<?php
class Application_Form_Matriregistrationform extends Zend_Form
{
    public $elementDecorators = array(
        'ViewHelper',
        'Errors',
        array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')),
        array('Label', array('tag' => 'td')),
        array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
    );

    public $buttonDecorators = array(
        'ViewHelper',
        array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')),
        array(array('label' => 'HtmlTag'), array('tag' => 'td', 'placement' => 'prepend')),
        array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
    );


    public function init()
    {

    $this->setAction('/matri/public/matri/matri')
        ->setMethod('post');


        $id = $this->addElement('hidden', 'id', array(
          'decorators' => $this->elementDecorators,

        ));


    $email = new Zend_Form_Element_Text('username');
    $email->setLabel('Username')
          ->addFilter('StringToLower')
              ->setRequired(true)
              ->addValidator('NotEmpty', true)
              ->addValidator('EmailAddress')
          ->setDecorators($this->elementDecorators);
    $this->addElement($email);



    $password = new Zend_Form_Element_Password('password');
    $password->setLabel('Password:')
        ->setRequired(true)
        ->setDecorators($this->elementDecorators);
    $this->addElement($password);



    $confpassword = new Zend_Form_Element_Password('confpassword');
    $confpassword->setLabel('Confirm Password:')
        ->setRequired(true)
        ->setDecorators($this->elementDecorators)
        ->addValidator(new Zend_Validate_Identical($_POST['password']));
    $this->addElement($confpassword);

        $name = $this->addElement('text', 'firstname', array(
            'decorators' => $this->elementDecorators,
            'label'       => 'Name:',
        ));
    $this->addElement('datePicker','movie_release_date', array(
            'label' => 'Release Date:',
            'required'=> false
            )
    );




    $gender2 = new Zend_Form_Element_Radio('gender');

        $gender2->setSeparator('')
        ->setLabel('Gender:')
        ->setRequired(true)
                ->addMultiOption('m', 'Male')
                ->addMultiOption('f', 'Female')
        ->setDecorators($this->elementDecorators);

    $this->addElement($gender2);

        $DOB = $this->addElement('text', 'DOB', array(
            'decorators' => $this->elementDecorators,
            'label'       =>'Date of Birth:',
        ));

        $religion = $this->addElement('text', 'religion', array(
            'decorators' => $this->elementDecorators,
            'label'       =>'Religion:',
        ));

        $mothertongue = $this->addElement('text', 'mothertongue', array(
            'decorators' => $this->elementDecorators,
            'label'       =>'Mother Tongue:',
        ));

        $country = $this->addElement('text', 'country', array(
            'decorators' => $this->elementDecorators,
            'label'       =>'Country:',
        ));

        $maritalstatus = $this->addElement('text', 'maritalstatus', array(
            'decorators' => $this->elementDecorators,
            'label'       =>'Marital Status:',
        ));

        $height = $this->addElement('text', 'height', array(
            'decorators' => $this->elementDecorators,
            'label'       =>'Height:',
        ));

        $caste = $this->addElement('text', 'caste', array(
            'decorators' => $this->elementDecorators,
            'label'       =>'Caste:',
        ));




        $smoke = $this->addElement('text', 'smoke', array(
            'decorators' => $this->elementDecorators,
            'label'       =>'Smoke:',
        ));
    $smoke = new Zend_Form_Element_Radio('smoke');
    $smoke->setSeparator('')
        ->setLabel('Smoke:')
        ->setRequired(true)
                ->addMultiOption('yes', 'Yes')
                ->addMultiOption('no', 'No')
        ->setDecorators($this->elementDecorators);
    $this->addElement($smoke);


    $drink = new Zend_Form_Element_Radio('drink');
    $drink->setSeparator('')
        ->setLabel('Drink:')
        ->setRequired(true)
                ->addMultiOption('yes', 'Yes')
                ->addMultiOption('no', 'No')
        ->setDecorators($this->elementDecorators);
    $this->addElement($drink);


    $diet = new Zend_Form_Element_Radio('diet');
    $diet->setSeparator('')
        ->setLabel('diet:')
        ->setRequired(true)
                ->addMultiOption('yes', 'Yes')
                ->addMultiOption('no', 'No')
        ->setDecorators($this->elementDecorators);
        $this->addElement($diet);

        $country = $this->addElement('text', 'country', array(
            'decorators' => $this->elementDecorators,
            'label'       =>'Country:',
        ));

        $state = $this->addElement('text', 'state', array(
            'decorators' => $this->elementDecorators,
            'label'       =>'State of Residence:',
        ));

        $city = $this->addElement('text', 'city', array(
            'decorators' => $this->elementDecorators,
            'label'       =>'City of Residence:',
        ));


        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setAttrib('id', 'submitbutton')
        ->setDecorators($this->buttonDecorators);
    $this->addElement($submit);


        //$this->addElements(array($id, $username, $firstname, $lastname, $submit));




    }

    public function loadDefaultDecorators()
    {
        $this->setDecorators(array(
            'FormElements',
            array('HtmlTag', array('tag' => 'table')),
            'Form',
        ));
    }
}

表单操作代码

public function matriAction()
    {   

//      $this->_helper->layout->disableLayout();
        $form = new Application_Form_Matriregistrationform();
            $form->submit->setLabel('Profile Registration');

         if ($this->_request->isPost()) {
                $formData = $this->_request->getPost();
                if ($form->isValid($formData)) {
                    echo 'Form Successfully sumbitted!';
                    exit;
                } else {
                    $form->populate($formData);
                }
            }



            $this->view->form = $form;




    }

3 个答案:

答案 0 :(得分:6)

一种方法是使用ZendX_jQuery。另一种方法是手动完成,就像对任何其他形式一样。

对于手动方式,您需要在HTML的head标签中包含jquery和jquery-ui,例如

 <?php echo $this->headLink()->appendStylesheet($this->baseUrl('/css/smoothness/jquery-ui-1.8.7.custom.css')); ?>
 <?php echo $this->headScript()->appendFile($this->baseUrl('/js/jquery-1.4.4.min.js')); ?>
 <?php echo $this->headScript()->appendFile($this->baseUrl('/js/jquery-ui-1.8.7.custom.min.js')); ?>

然后你可以将以下JavaScript添加到你的html:

$(document).ready(function () {
    /* assuming that text input datePicker would have id='datePicker' */
    $( "#datePicker" ).datepicker({ dateFormat: 'dd/mm/yy' });

});

但是,首先,我建议您查看ZendX_jQuery。我提供手动方式示例的原因是我还没有尝试使用ZendX_jQuery。

答案 1 :(得分:4)

如果您想使用ZendX,请按照以下简单步骤操作:

下载Full Zend Framework库,并将Zendx从“extras”文件夹复制到您的库文件夹。

将此添加到您的application.ini以使用ZendX:

pluginPaths.ZendX_Application_Resource = "ZendX/Application/Resource"

要使用JQuery UI,您可以通过将此行添加到您的application.ini

从谷歌cdn中获取它
resources.jquery.version = 1.4.1
resources.jquery.ui_enable = true
resources.jquery.ui_version = 1.8.4
resources.jquery.stylesheet = "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/smoothness/jquery-ui.css"

现在只需在Zend Form Class中添加一个Datepicker:

$birthdate = new ZendX_JQuery_Form_Element_DatePicker('birthdate');
$birthdate->setLabel('Geburtsdatum:')
->setJQueryParam('dateFormat', 'dd.mm.yy')
->setJQueryParam('changeYear', 'true')
->setJqueryParam('changeMonth', 'true')
->setJqueryParam('regional', 'de')
->setJqueryParam('yearRange', "1980:2000")
->setDescription('dd.mm.yyyy')
->addValidator(new Zend_Validate_Date(
array(
'format' => 'dd.mm.yyyy',
)))
->setRequired(true);

$this->addElement($birthdate);

答案 2 :(得分:1)

$this->addElement('datePicker','movie_release_date', array(
            'label' => 'Release Date:',
            'required'=> false,
            'class' => 'datepicker'
            )
    );

您需要在datePicker字段中添加一个类,以便jquery可以连接它。我不确定我上面的例子是否正确,因为我通常使用以下setAttrib方法,如下所示。

$datePicker = new Zend_Form_Element_Text('datePicker');
$datePicker->setAttrib('class', 'datePicker')
           ->setRequired( true );