Mr B
Mr B

Reputation: 4130

How to implement two forms for one model in Zend PHP framework

I am using Zend 1.11. I have a form which takes input from a user and saves the data to a database table which works fine.

However I want to also implement functionality that allows users to import multiple records into a database table using Excel worksheets. I am using the PHPExcel library for this.

Currently all my forms are in \library\App\Form\ with the naming convention of ModelNameForm.php. How would I add a second form for a model which accommodates the file upload?

Many thanks,

Sid.

Upvotes: 0

Views: 306

Answers (3)

Mr B
Mr B

Reputation: 4130

The solution I came accross is simple.

I only found it difficult because I am using Zend for the first time, although I am familiar with the MVC approach.

I have used CakePHP before which requires developers to follow conventions whereas Zend allows the user to form their own conventions.

The Zend application I am working on was created by my colleague who has used his own naming convention for form classes (ModelNameForm.php) which confused me because I wasn't sure whether this naming convention was part of Zend or the form class could be named anything.

After visiting the Zend IRC channel, I was told I could create another form class alongside the other forms and name it whatever I want and specify it in the function of the controller that uses it.

So I just created another form class and specified in the function that used it.

However after doing this I found out that creating a second form wasn't necessary as it was only one field that I had was different from the first form and this could be taken care of using if statements in the first form class.

Upvotes: 0

Jake N
Jake N

Reputation: 10583

I have a similar layout for this but I use static function calls in the form classes to get specific types of form. For example

class User_Model_Form extends My_Form
{
    // All functions can be passed a model to pre-populate the form 
    // when editing and existing model

    public static function getRegisterForm($model = null)
    {
        // Build and return form here
    }

    public static function geLoginForm($model = null)
    {
        // Build and return form here
    }

    public static function getChangePasswordForm($model = null)
    {
        // Build and return form here
    }

    // Etc
}

Upvotes: 1

Yasen
Yasen

Reputation: 16

Why don't you make a property for your model where you can save the form's class instances called. Then you can make a method like for e.g. getForm() in the model so you can access the form by it's name. That's what I have in mind on first think.

Upvotes: 0

Related Questions