views:

140

answers:

2

Here's my RegisterController:

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

        if($request->isPost()){
            //I NEED HELP WITH THE getFormValues() METHOD.
            $formResults = $this->getFormValues();
            $db = $this->_getParam('db');

            $data = array(
            'user'      => $formResults['username'],
            'email'     => $formResults['email'],
            'password'  => $formResults['password'],
            'passwordc' => $formResults['passwordc'],
            'name'      => $formResults['name'],
            'avatar'    => $formResults['avatar'],
            );


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

And here's my registration view:

<body>  
        <h1>Thanks for signing up!</h1> 
        <?php
        $this->form->setAction($this->url(array('controller' => 'registration','action'=>'saveforminformation'))); 
        $this->form->setMethod('post');
        echo $this->form;
        ?>


        <img alt="signupimg" src="/img/signup.png">
    </body>

I'm fairly new to Zend, but I'm eager to learn.

How can I get the values sent in the form?

+4  A: 

You have to pass the data to the form before it can validate and filter it:

if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())
{
    $db->insert($form->getValues());
}

If the data is not valid then the form will render with messages that indicate to the user what's wrong.

Zend_From is an odd class. I think it's a victim of the tension between the 'only use what you want' ethos of the Zend Framework and the attempt to create a comprensive MVC framework. It performs controller duties (fetching user input, passing data to the view) and model duties (validating input).

Benedict Cohen
Actually, it doesn't fetch User Input. You explicitly have to pass the POST data to it. Zend Form on itself is just a FilterChain and ValidationChain for your Model data. It's not even mixing presentation logic into the model, because all the presentation logic is inside the ViewHelpers and those dont get invoked before the Form is rendered in the View. Here is a good article about it: http://weierophinney.net/matthew/archives/200-Using-Zend_Form-in-Your-Models.html
Gordon
@Gordon: Good point. I did created a form following the post you linked to but it still seemed clumsy. If I changed the models properties I would have to edit the form too. If I wanted to set a value without using the form then I'd have to duplicate validation code in the setter (or create a form just to validate one piece of data).I'd like to be able to define the properties of a class and then have a method to generate a form based on these definitions. ZF doesn't include many 'model' classes (and with good reason) but I think there is space for something like I describe.
Benedict Cohen
@Benedict I agree, auto generating the form from a Model would be neat. Without that capability, the form is indeed duplicating the model or replaces it.
Gordon
+2  A: 

The ZF Reference Guide on Zend_Form has all the info you need:

After a form is submitted, you will need to check and see if it passes validations. Each element is checked against the data provided; if a key matching the element name is not present, and the item is marked as required, validations are run with a NULL value.

Summing up from the examples in the reference guide:

    $form = new Your_Form;
    // check if we have a valid Form POST
    if($request->isPost() && $form->isValid($_POST)) {
        // get the filtered values from the Form
        $data = $form->getValues();
        // insert the values to the database
        $db->insert('Usuario', $data);
        // redirect and inform user of success
        $this->_helper->redirector(/*redirect somewhere*/);
    }
    // Set Form to View 
    // If it wasn't a POST, a blank Form will be shown in the View
    // If it's not a valid Form, the form will show old values and errors
    $this->view->form = $form;

The values returned by $form->getValues() will return all values entered into the form after they have been run through the form filters you set inside your form. If the form input names match the column names in your database, you do not have to map them to an appropriate array, before insertion to the database.


This is my 500th Answer! Yay!

Gordon