ZF2 Doctrine2填充带有日期字段的表单

时间:2013-11-27 17:18:03

标签: doctrine-orm zend-framework2 twitter-bootstrap-3

当我尝试将日期字段传递给表单时出错。错误是:提供给Escape助手的对象,但是标志不允许递归这是由两个在mysql中具有日期类型的字段引起的。

我写了一部分剧本,如果需要进一步的信息,请告诉我...... 在我的控制器中,我有:

public function editAction() {

    $id = (int) $this->params()->fromRoute('id', NULL);
    if (!isset($id))
        return $this->redirect()->toRoute('group', array('action' => 'add'));

    $this->layout('layout/modal');

    $objectManager = $this->_getEntityManager();

    $group = $this->_getEntityManager()->find('Hotbed\Entity\Group', $id);
    $form = new \Hotbed\Form\Group\EditGroup($objectManager);
    $form->bind($group);

    $prg = $this->prg();
    if ($prg instanceof \Zend\Http\PhpEnvironment\Response) {
        // returned a response to redirect us
        return $prg;
    } elseif ($prg === false) {
        // this wasn't a POST request, but there were no params in the flash messenger
        // probably this is the first time the form was loaded

        return array(
            'id' => $id,
            'form' => $form,
        );
    }

    $form->setData($prg);


    if ($form->isValid()) {.......

在我的Fieldset中我有

class GroupFieldset extends Fieldset implements InputFilterProviderInterface {

protected $_om = '';

public function __construct(ObjectManager $objectManager) {

    parent::__construct('group');

    $this->_om = $objectManager;

    $this->setHydrator(new DoctrineHydrator($this->_om, 'Hotbed\Entity\Group'))
            ->setObject(new Group());

    // Id
    $this->add(array(
        'name' => 'id',
        'attributes' => array(
            'type' => 'hidden',
        ),
    ));

    // Title
    $this->add(array(
        'name' => 'title',
        'type' => 'text',
        'options' => array(
            'label' => 'Title',
        ),
        'attributes' => array(
            'class' => 'form-control input-large',
            'placeholder' => "Enter name of group",
            'required' => '*'
        ),
    ));

    //Root_Parents
    $this->add(array(
        'name' => 'parent',
        'type' => 'select',
        'options' => array(
            'label' => 'Parent group',
            'empty_option' => 'To define sub-groups, select parent',
            'value_options' => $this->_getMainGroups()
        ),
        'attributes' => array(
            'class' => 'form-control input-large',
        ),
    ));

    //State
    $this->add(array(
        'name' => 'state',
        'type' => 'checkbox',
        'options' => array(
            'label' => 'Is Group actif?',
            'use_hidden_element' => true,
            'checked_value' => 1,
            'unchecked_value' => 0
        ),
        'attributes' => array(
            'class' => 'form-control'
        ),
    ));

    //valid from
    $this->add(array(
        'name' => 'valid_from',
        'type' => 'text',
        'options' => array(
            'label' => 'Valid from:',
            'format' => 'd-m-Y'
        ),
        'attributes' => array(
            'placeholder' => 'dd-mm-yyyy',
            'class' => 'form-control input-large date date-picker',
            'readonly' => TRUE,
        ),
    ));



    //valid to
    $this->add(array(
        'name' => 'valid_to',
        'type' => 'text',
        'options' => array(
            'label' => 'Valid to:',
            'format' => 'd-m-Y'
        ),
        'attributes' => array(
            'placeholder' => 'dd-mm-yyyy',
            'class' => 'form-control input-large date date-picker',
            'readonly' => TRUE,
        ),
    )); ......

将valid_from和valid_to的类型设置为date而不是text,可以使用,但booststrap3 datepicker将替换为html5 date,表单不再有效。 表格如下:

<div class="form-group">
                    <label class="col-md-4 control-label"><?php echo $this->formLabel($group->get('valid_from')); ?></label>
                    <div class="col-md-8"><?php echo $this->formInput($group->get('valid_from')); ?></div>
                </div>
                <div class="form-group">
                    <label class="col-md-4 control-label"><?php echo $this->formLabel($group->get('valid_to')); ?></label>
                    <div class="col-md-8"><?php echo $this->formInput($group->get('valid_to')); ?></div>
                </div>

我真的不知道如何解决这个问题...感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

找到了答案: 在将组绑定到表单之前的控制器中,我做了日期的格式,因此只传递字符串而不再传递对象。