簡體   English   中英

ZF2表單-提交或按鈕

[英]ZF2 forms - Submit or Button

我無法解決提交按鈕類型的問題。

我創建了一個名為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 \\ Submit'更改為'Zend \\ Form \\ Element \\ Button',但錯誤再次出現...

誰能幫我? =)

編輯:

我要發布我的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,
        ));
    }
}

然后,出現新錯誤“提供了無效的過濾器規范;不包括'name'鍵”

感謝4全部^^

好吧,我對代碼進行了som更改。 我首先訂購了文件。 我有一個Register.php(模型),一個RegisterController.php(控制器)和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(控制器)

<?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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM