0

我有一个简单的注册表单和使用 md5 加密和 salt 散列的密码,我收到一条错误消息,说“uni”字段不能为空,我假设这是因为我不确定如何添加将 $salt 放入表单中(它不是隐藏元素吗?就像 id 字段一样?)。我已经完成了以下模型:

public function addUser($first_name, $surname, $email, $username, $password, $salt, $age, $gender, $uni) {

    $salt=substr(md5(mt_rand()),0,20);
    $md5Password=md5($salt.$password);

    $data = array(
        'first_name' => $first_name,
        'surname' => $surname,
        'email' => $email,
        'username' => $username,
        'salt' => $salt,
        'password' => $md5Password,
        'age' => $age,
        'gender' => $gender,
        'uni' => $uni,
    );
    $this->insert($data);
} 

这是表格:

<?php

class Application_Form_Register extends Zend_Form
{


    public function init() {
        $this->setName('register');
        $id = new Zend_Form_Element_Hidden('id');
  $id->addFilter('Int');
        $first_name = new Zend_Form_Element_Text('first_name');
        $first_name->setLabel('Firstname:')
                ->setRequired(true)
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');
        $surname = new Zend_Form_Element_Text('surname');
        $surname->setLabel('Surname:')
                ->setRequired(true)
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');
    $email = new Zend_Form_Element_Text('email');
    $email->setLabel('Email:')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty');
    $username = new Zend_Form_Element_Text('username');
    $username->setLabel('Username:')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty');
    $password = new Zend_Form_Element_Password('password');
    $password->setLabel('Password:')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty');
    $salt = new Zend_Form_Element_Hidden('salt');
    $age = new Zend_Form_Element_Text('age');
    $age->setLabel('Age:')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty');
    $gender = new Zend_Form_Element_Text('gender');
    $gender->setLabel('Gender:')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty');
    $uni = new Zend_Form_Element_Text('uni');
    $uni->setLabel('University:')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty');
    $submit = new Zend_Form_Element_Submit('submit');
    $submit->setAttrib('id', 'submitbutton');
    $this->addElements(array($id, $first_name, $surname, $email, $username, $password, $salt, $age, $gender, $uni, $submit));
}
}

谢谢

里克

4

1 回答 1

0

第一个“uni”是一个表单域。

$uni = new Zend_Form_Element_Text('uni');
    $uni->setLabel('University:')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty');

当您将“null”值传递给已声明为“not null”的 db 表列时,会发生此错误。要解决此问题,请将“not null”值传递给“addUser()”方法,在方法 api 中为“uni”设置默认值,或允许 db 表“uni”列接受“null”值。

就个人而言,我通常将数组传递给这些方法,然后取出我想要的位。就像是:

public function addStation(array $data) {
        $data = array(
            'station' => $data['station'],
            'comment' => $data['comment']
        );

        $this->insert($data);
    }

再次就个人而言,我根本不会将盐字段放入表单中,我会在模型中分配盐。毫无意义地向世界展示我的盐。

祝你好运。

于 2012-04-26T13:22:29.583 回答