ZF2:在提交后传递变量

时间:2014-09-17 20:49:44

标签: php forms zend-framework2

我目前正在研究Zend Framework 2中的一个应用程序。我正在处理一个日历,我在表单中有两个提交按钮,可以转到下个月或上个月。 所以现在我想在提交时在表单中传递两个值(月份和年份),以便我知道我在哪个月。

正如您在下面的课程中看到的那样我尝试使用隐藏字段来完成它,但我不知道如何添加数据或之后提取数据。有人可以告诉我如何做到这一点或给我另一种解决方案吗?是否可以使用形式发送整个CalendarMonth类?

我的设置现在有以下几个类:

表格:CalendarForm.php

class CalendarForm extends Form {

protected $_id = null;

public function __construct()
{
    parent::__construct('calendar');
    $this->setAttribute('method', 'post');
    $this->add(array(
        'name' => 'month',
        'attributes' => array(
            'type'  => 'hidden',
        ),
    ));
    $this->add(array(
        'name' => 'year',
        'attributes' => array(
            'type'  => 'hidden',
        ),
    ));
    $this->add(array(
        'name' => 'previous',
        'attributes' => array(
            'type'  => 'submit',
            'value' => 'Vorige',
        ),
    ));
    $this->add(array(
        'name' => 'next',
        'attributes' => array(
            'type'  => 'submit',
            'value' => 'Volgende',
        ),
    ));
}

public function setId($id) {
    $this->_id = $id;
}
}

控制器:CalendarController.php

class CalendarController extends AbstractActionController
{
    private $calendarEventTable;

public function indexAction()
{
    $calendarMonth = null;
    $form = new CalendarForm();
    $request = $this->getRequest();

    if ($request->isPost()) {
        $form->setData($request->getPost());
        if($this->getRequest()->getPost('next'))
        {
            //Extract the values from the submitted form and recall the getCalenderPage() to recreate the CalendarMonth
            echo "test";
            echo ((isset($_POST['month']))     ? $_POST['month']     : null);

        } else if($this->getRequest()->getPost('previous'))
        {
            //$this->calendarMonth->minusMonth();
        }
    } else {
        $calendarMonth = $this->getCalendarPage(8, 2014);
    }
    $calendarMonth = $this->getCalendarPage(8, 2014);

    //Add the values to the form
    $collection = $form->get('month');
    $collection->setAttribute("month", "test12356t");

    return new ViewModel(array(
        'calendarMonth' => $calendarMonth,
        'form' => $form,
    ));
}

public function detailAction()
{
    return new ViewModel(array(
        'event' => $this->getCalendarTable()->getCalendar($this->getEvent()->getRouteMatch()->getParam('id')),
    ));
}

private function getCalendarTable()
{
    if (!$this->calendarEventTable) {
        $sm = $this->getServiceLocator();
        $this->calendarEventTable = $sm->get('Website\Model\Database\CalendarEventTable');
    }
    return $this->calendarEventTable;
}

public function getCalendarPage($month, $year)
{
    $calendarMonth = new CalendarMonth($month, $year);
    $events = $this->getCalendarTable()->fetchBetween('2014-08-01', '2014-08-31');

    $calendarMonth->initPage($events);

    return $calendarMonth;
}
}

和视图:index.phtml

<?php
$form = $this->form;
$form->setAttribute('action', $this->url('calendar', array('action' => 'index')));
$form->prepare();

echo $this->form()->openTag($form);
echo $this->formHidden($form->get('month'));
echo $this->formHidden($form->get('year'));
echo $this->formSubmit($form->get('previous'));
echo $this->formSubmit($form->get('next'));

echo $this->form()->closeTag();
?>

1 个答案:

答案 0 :(得分:1)

问题出在indexAction,你没有修改月份和年份,你在if后面调用getCalendarPage所以它总是被执行。您可以将indexAction修改为:

public function indexAction()
{
    $calendarMonth = null;
    $form = new CalendarForm();
    $request = $this->getRequest();

    if ($request->isPost()) {
        $form->setData($request->getPost());

        $month = $form->get('month')->getValue();
        $year = $form->get('year')->getValue();
        if($this->getRequest()->getPost('next')) {
            $month += 1;
            if ($month > 12) {
                $month = 1;
                $year += 1;
            }
        } else if($this->getRequest()->getPost('previous')) {
            $month -= 1;
            if ($month < 1) {
                $month = 12;
                $year -= 1;
            }
        }
    } else {
        $month = 8;
        $year = 2014;
    }
    $calendarMonth = $this->getCalendarPage($month, $year);

    $form->get('month')->setValue($month);
    $form->get('year')->setValue($year);

    return new ViewModel(array(
        'calendarMonth' => $calendarMonth,
        'form' => $form,
    ));
}