【问题标题】:Adding Jquery Datepicker in a Zend form在 Zend 表单中添加 Jquery Datepicker
【发布时间】:2011-02-02 01:13:01
【问题描述】:

我是 Zend 框架的新手,我想知道如何将日期选择器 jquery 小部件添加到我在 Google 上广泛搜索但找不到任何准确信息的 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;




    }

【问题讨论】:

    标签: zend-framework zend-form


    【解决方案1】:

    一种方法是使用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 在我的待办事项清单上是要向ZendX_jQuery 发送邮件。我总是按照@Marcin 描述的手动方式进行操作。
    • zendcasts.com/working-with-zendx_jquery/2010/07中很容易解释Jquery集成的最佳和简单的解决方案
    • 看起来 ZendX_jQuery 链接已损坏。这还能用吗?再也找不到了。
    【解决方案2】:

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

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

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

    pluginPaths.ZendX_Application_Resource = "ZendX/Application/Resource"
    

    要使用 JQuery UI,您可以通过将此行添加到 application.ini 来从 google 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 表单类中添加一个 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);
    

    【讨论】:

    • 我尝试了 ZendX_jquery 选项(上一个)。选择器没有出现。将我的装饰器添加到元素[->setDecorators($this->elementDecorators)] 导致此错误。警告:表单捕获的异常:如果没有至少一个装饰器实现“ZendX_JQuery_Form_Decorator_UiWidgetElementMarker”接口,则无法呈现 jQuery 表单元素。这个标记接口的默认装饰器是'ZendX_JQuery_Form_Decorator_UiWidgetElement'。提示:ViewHelper 装饰器无法正确呈现 jQuery 元素。
    • Ehmm 抱歉,您必须在视图/布局中回显 Jquery 内容:“echo $this->jQuery()->enable();”但我不确定你是否可以使用自己的装饰器。
    • 我尝试了您的建议,但日期选择器没有出现。我在视图脚本中添加的命令“echo $this->xxx”
    【解决方案3】:
    $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 );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-20
      • 2015-04-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多