ZF2表格 - 提交或按钮

时间:2014-04-29 15:18:00

标签: zend-framework2

我的提交按钮类型存在问题,我无法解决。

我创建了一个名为RegisterForm.php的表单类,其中包含一些元素,如电子邮件,密码,confirm_password和用于提交操作的按钮。我按照这种结构编码了所有元素。

$this->add(array( //Email
            'type'=>'Zend\Form\Element\Email',
            'name'=>'email',
            'options'=> array(
                'label'=>'Email',
            ),
            'attributes'=>array(
                'required'=>'required'
            ),
            'filters'=>array(
                array('name' => 'StringTrim'),
            ),
            'validators' => array(
                array(
                    'name' => 'EmailAddress',
                    'options' => array(
                        'messages' => array(
                        \Zend\Validator\EmailAddress::INVALID_FORMAT =>'Formato de dirección email incorrecto'
                        )
                    )
                )
            )
        ));

我的问题在于按钮'提交'。这是代码:

$this->add(array( //Boton envio
            'type'=>'Zend\Form\Element\Button',
            'name'=>'submit',
            'attributes'=> array(
                'value'=>'Enviar',
            ),
            'options'=> array(
                'label'=>'Enviar',
            ),
        ));

当我点击提交按钮时,没有任何反应!我尝试过改变Zend \ Form \ Element \ Button'对于Zend \ Form \ Element \ Submit'但错误又来了......

任何人都可以帮助我吗? =)

编辑:

我要发布我的RegisterFilter.php代码

<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Users\Form;
use Zend\InputFilter\InputFilter;

/**
 * Description of RegisterFilter
 *
 * @author mlorente
 */
class RegisterFilter extends InputFilter{
    public function __construct() {
        //Validador para Email;
        $this->add(array(
            'name' => 'email',
            'required' => true,
            'validators' => array(
                array(
                    'name' => 'EmailAddress',
                    'options' => array(
                        'domain' => true,
                    ),
                ),
            ),
        ));

        //Validador para Nombre. Limite entre 2 y 140 caracteres.
        $this->add(array(
            'name' => 'name',
            'required' => true,
            'filters' => array(
                array(
                    'name' => 'StripTags',
                ),
                'validators' => array(
                    array(
                        'name' => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min' => 2,
                            'max' => 140,
                        ),
                    ),
                ),
            ),
        ));

        $this->add(array(
            'name' => 'password',
            'required' => true,
        ));

        $this->add(array(
            'name' => 'confirm_password',
            'required' => true,
        ));
    }
}

然后,出现了一个新的错误&#34;无效的过滤器规范;不包括&#39; name&#39;密钥&#34;

全部谢谢4 ^^

1 个答案:

答案 0 :(得分:0)

好吧,我在代码中做了一些修改。我所有人都订购了我的文件。我有一个Register.php(模型),一个RegisterController.php(Controller)和一个RegisterForm(主窗体)。

以下是代码:

Register.php(模型)

<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Users\Model;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;

/**
 * Description of Register
 *
 * @author mlorente
 */
class Register implements InputFilterAwareInterface {

    public $name;
    public $email;
    public $password;
    public $confirm_password;

    protected $inputFilter;

    public function exchangeArray($data){
        $this->name = (isset($data['name'])) ? $data['name'] : NULL;
        $this->email = (isset($data['email'])) ? $data['email'] : NULL;
        $this->password = (isset($data['password'])) ? $data['password'] : NULL;
        $this->confirm_password = (isset($data['confirm_password'])) ? $data['confirm_password'] : NULL;
    }

    public function getInputFilter() {

        if(!$this->inputFilter){
            $inputFilter = new InputFilter();

            //Filtro para 'name'
            $inputFilter->add(array(
                'name' => 'name',
                'required' => true,
                'filters' => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name' => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min' => 4,
                            'max' => 70,
                        ),
                    ),
                ),
            ));

            //Filtro para 'email'
            $inputFilter->add(array(
                'name' => 'email',
                'required' => true,
                'filters' => array(
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name' => 'EmailAdress',
                        'options' => array(
                            'domain' => true,
                            'messages' => array(
                            \Zend\Validator\EmailAddress::INVALID_FORMAT =>
                    'Direccion de email incorrecta'
                            ),
                        ),
                    ),
                ),
            ));

            //Filtro para 'password'
            $inputFilter->add(array(
                'name' => 'password',
                'required' => true,
            ));

            //Filtro para 'confirm_password'
            $inputFilter->add(array(
                'name' => 'confirm_password',
                'required' => true,
            ));

            $this->inputFilter = $inputFilter;
        }
        return $this->inputFilter;
    }

    public function setInputFilter(InputFilterInterface $inputFilter) {
        throw new \Exception("Not used.");
    }

}

RegisterController.php(Controller)

<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Users\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Users\Model\Register;
use Users\Form\RegisterForm;


/**
 * Description of RegisterController
 *
 * @author mlorente
 */
class RegisterController extends AbstractActionController {

    public function indexAction() {
        $form = new RegisterForm();
        $viewModel = new ViewModel(array('form' => $form));
        return $viewModel;
    }

    public function confirmAction(){
        $viewModel = new ViewModel();
        return $viewModel;
    }

    public function processAction(){
        $form = new RegisterForm();
        $form->get('submit')->setValue('Enviar');

        $request = $this->getRequest();
        if($request->isPost()){
            $registro = new Register();
            $form->setInputFilter($registro->getInputFilter());
            $form->setData($request->getPost());

            if($form->isValid()){
                $registro->exchangeArray($form->getData());
                return $this->redirect()->toRoute(NULL, array('controller' => 'register','action' => 'confirm'));
            }
        }
        return array('form' => $form);
    }
}

RegisterForm.php(主要表格)

<?php

/**
 * Description of RegisterForm
 *
 * @author mlorente
 */
namespace Users\Form;
use Zend\Form\Form;

class RegisterForm extends Form{

    public function __construct($name = null) {
        parent::__construct('Register');

        /*TIPO DE FORMULARIO*/
        $this->setAttribute('method', 'post');
        $this->setAttribute('enctype', 'multipart/form-data');

        /*CAMPOS DEL FORMULARIO*/

        //Nombre
        $this->add(array(
            'name' => 'name',
            'type' => 'Text',
            'options' => array(
                'label' => 'Nombre completo: ',
            ),
        ));

        //Email
        $this->add(array(
            'name' => 'email',
            'type' => 'Email',
            'options' => array(
                'label' => 'Direccion de email: ',
            ),
        ));

        //Password
        $this->add(array( //Password
            'name'=>'password',
            'type' => 'Password',
            'options'=> array(
                'label'=>'Contraseña: ',
            ),
        ));

        //Repite password
        $this->add(array( //Password
            'name'=>'confirm_password',
            'type' => 'Password',
            'options'=> array(
                'label'=>'Confirma contraseña: ',
            ),
        ));

        $this->add(array( //Boton envio
            'name'=>'submit',
            'type' => 'Submit',
            'attributes'=> array(
                'value'=>'Enviar',
                'id' => 'subBtn',
            ),
        ));

    }
}

如果我测试实现主窗体的index.phtml页面,我会遇到一个新错误。

  

Zend \ Validator \ ValidatorPluginManager :: get无法获取或   为EmailAdress创建一个实例

我需要一些关于什么样的问题的想法...

谢谢!

编辑:我的错误=(我有EmailAdress而不是EmailAddress ..