开发者

Form is trying to save the login value of the submit button to my DB

开发者 https://www.devze.com 2022-12-22 08:14 出处:网络
Here\'s my Zend code: <?php require_once (\'Zend\\Form.php\'); class Sergio_Form_registrationform extends Zend_Form {

Here's my Zend code:

<?php

require_once ('Zend\Form.php');

class Sergio_Form_registrationform extends Zend_Form {
    public function init(){     
        /*********************USERNAME**********************/
        $username = new Zend_Form_Element_Text('username');
        $alnumValidator = new Zend_Validate_Alnum();

        $username   ->setRequired(true)
                    ->setLabel('Username:')
                    ->addFilter('StringToLower')
                    ->addValidator('alnum')
                    ->addValidator('regex', false, array('/^[a-z]+/'))
                    ->addValidator('stringLength',false,array(6,20));
        $this->addElement($username);

        /*********************EMAIL**********************/
        $email = new Zend_Form_Element_Text('email');
        $alnumValidator = new Zend_Validate_Alnum();


        $email      ->setRequired(true)
                    ->setLabel('EMail:')
                    ->addFilter('StringToLower')
                    ->addValidator('alnum')
                    ->addValidator('regex', false, array('/^[a-z]+/'))
                    ->addValidator('stringLength',false,array(6,20));
        $this->addElement($email);

        /*********************PASSWORD**********************/
        $password = new Zend_Form_Element_Password('password');
        $alnumValidator = new Zend_Validate_Alnum();


        $password   ->setRe开发者_Go百科quired(true)
                    ->setLabel('Password:')
                    ->addFilter('StringToLower')
                    ->addValidator('alnum')
                    ->addValidator('regex', false, array('/^[a-z]+/'))
                    ->addValidator('stringLength',false,array(6,20));
        $this->addElement($password);

        /*********************NAME**********************/
        $name = new Zend_Form_Element_Text('name');
        $alnumValidator = new Zend_Validate_Alnum();


        $name       ->setRequired(true)
                    ->setLabel('Name:')
                    ->addFilter('StringToLower')
                    ->addValidator('alnum')
                    ->addValidator('regex', false, array('/^[a-z]+/'))
                    ->addValidator('stringLength',false,array(6,20));
        $this->addElement($name);

        /*********************LASTNAME**********************/
        $lastname = new Zend_Form_Element_Text('lastname');
        $alnumValidator = new Zend_Validate_Alnum();


        $lastname   ->setRequired(true)
                    ->setLabel('Last Name:')
                    ->addFilter('StringToLower')
                    ->addValidator('alnum')
                    ->addValidator('regex', false, array('/^[a-z]+/'))
                    ->addValidator('stringLength',false,array(6,20));
        $this->addElement($lastname);

        /*********************DATEOFBIRTH**********************/
        $dateofbirth = new Zend_Form_Element_Text('dateofbirth');
        $alnumValidator = new Zend_Validate_Alnum();


        $dateofbirth->setRequired(true)
                    ->setLabel('Date of Birth:')
                    ->addFilter('StringToLower')
                    ->addValidator('alnum')
                    ->addValidator('regex', false, array('/^[a-z]+/'))
                    ->addValidator('stringLength',false,array(6,20));
        $this->addElement($dateofbirth);

        /*********************AVATAR**********************/
        $avatar = new Zend_Form_Element_File('avatar');
        $alnumValidator = new Zend_Validate_Alnum();


        $avatar     ->setRequired(true)
                    ->setLabel('Please select a display picture:');
        $this->addElement($avatar);

        /*********************SUBMIT**********************/

        $this->addElement('submit', 'login', array('label' => 'Login'));
    }
}
?>

Here's the code I use to save the values:

public function saveforminformationAction(){
        $form = new Sergio_Form_registrationform();
        $request = $this->getRequest();

        //if($request->isPost() && $form->isValid($_POST)){         
            $data = $form->getValues();
            $db = $this->_getParam('db');

            $db->insert('user',$data);          
        //}     
    }

When trying to save the values, I recieve a ghastly error:

Column 'login' not found.


You are getting all values from the form :

$data = $form->getValues();

And trying to save them all to the DB :

$db->insert('user',$data);

But :

  • In your DB there is no login column
  • And there is a login field in your form (the submit button)


I would see at least two possible solutions :

First idea would be to remove the login item from the $data array, before trying to insert ; something like this, I'd say, might do the trick :

$data = $form->getValues();
unset($data['login']);
$db->insert('user',$data);

But this kind of feels like some kind of "hack"...


Or you could use a new layer, that will act as a mapper between your form and your DB :

  • Fetch the values from the form
  • Pass those to the mapper
  • The mapper will then return what should be used for the DB
  • insert the data returned by the mapper.

Of course, it means a bit more work, but it will ensure both your form and your DB still work, even if one of them change : you'll just have to adapt/fix the mapper.


Use getPost() instead of getRequest().

0

精彩评论

暂无评论...
验证码 换一张
取 消