kevingoos
kevingoos

Reputation: 4303

ZF2: Pass variable in post submit

I am currently working on an app in Zend Framework 2. I am working on a calendar and I have two submit buttons in a form to go to the next or previous month. So now I want to pass two values (month and year) in the form when I submit so that I can know in which month I am.

As you can see in the classes below I tried to accomplish it with hidden fields, but I don't know how to add the data or extract it afterwards. Can somebody tell me how to do this or give me another solution? Is it maybe possible to send the whole CalendarMonth class with the form?

My setup now has the following classes:

Form: 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;
}
}

The controller: 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;
}
}

And the view: 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();
?>

Upvotes: 0

Views: 521

Answers (1)

Mohammad ZeinEddin
Mohammad ZeinEddin

Reputation: 1688

The problem is in the indexAction, you are not modifying the month and year, and you are calling the getCalendarPage after the if else so it is always executed. You can modify the indexAction to be something like this:

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,
    ));
}

Upvotes: 1

Related Questions