5

我正在创建注册表单:

这是我的行动:

public function registrationAction() {
    $form = new RegistrationForm();

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

        var_dump($form->isValid());
        exit;

        if ($form->isValid()) {
            $users->exchangeArray($form->getData());
            $this->getUsersTable()->addUser($users);
            $message = 'Регистрирахте се успешно! Можете да влезете в профила си чрез формата за вход на началната страница.';
        }
    }

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

这是我的表格:

namespace Main\Form;

use Zend\Form\Form;

class RegistrationForm extends Form {

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

    $this->add(array(
        'name' => 'username',
        'attributes' => array(
            'type' => 'Zend\Form\Element\Text',
        ),
        'options' => array(
            'label' => 'Потребителско име',
        ),
    ));

    $this->add(array(
        'name' => 'email',
        'attributes' => array(
            'type' => 'Zend\Form\Element\Text',
        ),
        'options' => array(
            'label' => 'Email адрес',
        ),
    ));

    $this->add(array(
        'name' => 'password',
        'attributes' => array(
            'type' => 'Zend\Form\Element\Text',
        ),
        'options' => array(
            'label' => 'Парола',
        ),
    ));

    $this->add(array(
        'name' => 'submit',
        'attributes' => array(
            'type' => 'Zend\Form\Element\Submit',
        ),
        'options' => array(
            'label' => 'Регистрация',
        )
    ));
}

}

这是 Object\Model\Users.php 中用户模型中的 inputFilter:

namespace Object\Model;

use Zend\Validator\StringLength;
use Zend\Validator\NotEmpty;
use Zend\Validator\EmailAddress;
use Zend\Validator\Date;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;

class Users {

public $user_id;
public $username;
public $name;
public $email;
public $password;
public $sex;
public $birthday;
public $avatar;
public $user_level;
public $date_registered;
public $is_active;
public $is_banned;

public function exchangeArray($data) {
    $this->user_id = (isset($data['user_id'])) ? $data['user_id'] : null;
    $this->username = (isset($data['username'])) ? $data['username'] : null;
    $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->sex = (isset($data['sex'])) ? $data['sex'] : null;
    $this->birthday = (isset($data['birthday'])) ? $data['birthday'] : null;
    $this->avatar = (isset($data['avatar'])) ? $data['avatar'] : null;
    $this->user_level = (isset($data['user_level'])) ? $data['user_level'] : null;
    $this->date_registered = (isset($data['date_registered'])) ? $data['date_registered'] : null;
    $this->is_active = (isset($data['is_active'])) ? $data['is_active'] : null;
    $this->is_banned = (isset($data['is_banned'])) ? $data['is_banned'] : null;
}

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

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

        $factory = new InputFactory();

        $inputFilter->add($factory->createInput(array(
                    'name' => 'user_id',
                    'filters' => array(
                        array('name' => 'Int'),
                    ),
                )));

        $inputFilter->add($factory->createInput(array(
                    'name' => 'username',
                    'filters' => array(
                        array('name' => 'StripTags'),
                        array('name' => 'StringTrim'),
                    ),
                    'validators' => array(
                        array(
                            'name' => 'NotEmpty',
                            'options' => array(
                                'messages' => array(
                                    NotEmpty::IS_EMPTY => 'Моля въведете потребителско име!',
                                ),
                            ),
                        ),
                        array(
                            'name' => 'StringLength',
                            'options' => array(
                                'encoding' => 'UTF-8',
                                'min' => 1,
                                'max' => 50,
                                'messages' => array(
                                    StringLength::TOO_SHORT => 'Потребителското име трябва да бъде минимум 1 символ',
                                    StringLength::TOO_LONG => 'Потребителското име не може да надвишава 50 символа',
                                ),
                            ),
                        ),
                    ),
                )));

        $inputFilter->add($factory->createInput(array(
                    'name' => 'name',
                    'filters' => array(
                        array('name' => 'StripTags'),
                        array('name' => 'StringTrim'),
                    ),
                    'validators' => array(
                        array(
                            'name' => 'StringLength',
                            'options' => array(
                                'encoding' => 'UTF-8',
                                'max' => 255,
                                'messages' => array(
                                    StringLength::TOO_LONG => 'Вашите име и фамилия не може да надвишават 255 символа',
                                ),
                            ),
                        ),
                    ),
                )));

        $inputFilter->add($factory->createInput(array(
                    'name' => 'email',
                    'filters' => array(
                        array('name' => 'StripTags'),
                        array('name' => 'StringTrim'),
                    ),
                    'validators' => array(
                        array(
                            'name' => 'NotEmpty',
                            'options' => array(
                                'messages' => array(
                                    NotEmpty::IS_EMPTY => 'Моля въведете email адрес!',
                                ),
                            ),
                        ),
                        array(
                            'name' => 'EmailAddress',
                            'options' => array(
                                'encoding' => 'UTF-8',
                                'min' => 1,
                                'max' => 50,
                                'messages' => array(
                                    EmailAddress::INVALID => 'Email адресът е невалиден.',
                                ),
                            ),
                        ),
                    ),
                )));

        $inputFilter->add($factory->createInput(array(
                    'name' => 'password',
                    'filters' => array(
                        array('name' => 'StripTags'),
                        array('name' => 'StringTrim'),
                    ),
                    'validators' => array(
                        array(
                            'name' => 'NotEmpty',
                            'options' => array(
                                'messages' => array(
                                    NotEmpty::IS_EMPTY => 'Моля въведете парола!',
                                ),
                            ),
                        ),
                        array(
                            'name' => 'StringLength',
                            'options' => array(
                                'encoding' => 'UTF-8',
                                'min' => 4,
                                'max' => 90,
                                'messages' => array(
                                    StringLength::TOO_SHORT => 'Паролата трябва да бъде минимум 4 символ',
                                    StringLength::TOO_LONG => 'Паролата не може да надвишава 90 символа',
                                ),
                            ),
                        ),
                    ),
                )));

        $inputFilter->add($factory->createInput(array(
                    'name' => 'sex',
                    'filters' => array(
                        array('name' => 'Int'),
                    ),
                )));

        $inputFilter->add($factory->createInput(array(
                    'name' => 'birthday',
                    'filters' => array(
                        array('name' => 'StripTags'),
                        array('name' => 'StringTrim'),
                    ),
                    'validators' => array(
                        array(
                            'name' => 'Date',
                            'options' => array(
                                Date::INVALID_DATE => 'Въведената дата е грешна.',
                            ),
                        ),
                    ),
                )));

        $inputFilter->add($factory->createInput(array(
                    'name' => 'avatar',
                    'filters' => array(
                        array('name' => 'StripTags'),
                        array('name' => 'StringTrim'),
                    ),
                )));

        $inputFilter->add($factory->createInput(array(
                    'name' => 'user_level',
                    'filters' => array(
                        array('name' => 'Int'),
                    ),
                )));

        $inputFilter->add($factory->createInput(array(
                    'name' => 'date_registered',
                    'filters' => array(
                        array('name' => 'Int'),
                    ),
                )));

        $inputFilter->add($factory->createInput(array(
                    'name' => 'is_active',
                    'filters' => array(
                        array('name' => 'Int'),
                    ),
                )));

        $inputFilter->add($factory->createInput(array(
                    'name' => 'is_banned',
                    'filters' => array(
                        array('name' => 'Int'),
                    ),
                )));

        $this->inputFilter = $inputFilter;
    }

    return $this->inputFilter;
}

}

当我尝试访问 $form 对象的 isValid() 或 getMessages() 方法时,它们是空的。在 var_dump 中调用它们时,它们总是布尔值错误......有人可以帮助我吗?提前致谢。这只发生在这个模块中。在我的另一个模块中,表格一切正常。这个 ZF2 有时很复杂... :|

4

5 回答 5

9

我和你有同样的问题,isValid() 会返回 false,而 getMessages() 会是一个空数组。

我的问题是我已经为一个字段(例如电子邮件)定义了一个输入验证器,但是我正在验证的表单上不存在该字段。Zend Framework 2 中似乎有一个错误,在这种情况下不会打印错误消息:github 上的错误报告

所以解决办法是把缺失的字段加到表单对象中。

于 2014-02-06T10:47:17.257 回答
6

检查字段名称的拼写是否与输入过滤器中的名称匹配 - 我做了同样的事情。

于 2013-04-10T09:23:58.163 回答
2

我遇到了同样的问题,这是因为表单和过滤器没有相同数量的属性。

您的注册表单的属性少于您的过滤器。

于 2017-02-26T21:36:53.277 回答
0

你写的(在控制器中)是正常的吗?

var_dump($form->isValid());
exit;

如果发生错误,可能只是显示“消息”,不是吗?

于 2013-03-31T13:36:28.887 回答
0

我找不到错误,但这里有一些提示:

您不需要使用 InputFactory 将元素设置为 InputFilter,ZF2 会自动完成(ZF2 Docs

最后,默认的 InputFilter 实现由 Factory 支持。这意味着当调用 add() 时,您可以提供工厂可以理解的规范,并且它将创建适当的对象。您可以以这种方式创建 Input 或 InputFilter 对象。

所以而不是

$inputFilter->add($factory->createInput(array(
                'name' => 'user_id',
                'filters' => array(
                    array('name' => 'Int'),
                ),
            )));

你可以做

$inputFilter->add(array(
    'name' => 'user_id',
         'filters' => array(
             array('name' => 'Int'),
          ),
     ));

另一件事,如果您为同一个实例调用 $form->isValid() 两次,第二次将导致一些不可见的错误,并且其余代码将不会执行。我不知道会发生什么错误,但您可以为每个 $form 实例调用一次 isValid 。

于 2013-08-26T14:57:51.397 回答