Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9103237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:35:23+00:00 2026-06-17T01:35:23+00:00

In my MVC, I populate forms with data from the model using an array.

  • 0

In my MVC, I populate forms with data from the model using an array. I am using PDO and fetch associative array this gives an array of fields that I can use to populate the view.

When the user requests a new record, the array is empty and unless I explicitly test the value of every variable before use, the script falls over with a non-existant index.

My approach previously was, for new records, to setup an empty array and pass this to the view. This is fine for simple forms, but when the view needs to use 10-15 variables this is a bit tedious.

What is the preferred method of doing this?
Should my view test every field before use, or should I create the empty array. If so, should the empty array be created in the controller or the model?


As an example, If the user wants to edit an existing record, the controller passes the ID number to the model and asks it for an array of current values for the record.

The controller passes this array to the view which then incorporates the values into a form for editing.

In contrast, when the controller receives a request for a new record, it knows there is no point in asking the model for the record because its new. It can’t just call the view because the passed array does not contain the keys required by the form.

My current approach to this is to initialise an array of empty keys but this seems tedious, time wasting and prone to error as it needs to be maintained if the model changes.

eg:

$this->view->class['Code']=NULL;
$this->view->class['Description']=NULL;
$this->view->class['Image']=NULL;
$this->view->class['Judge']=NULL;
$this->view->class['Entries']=NULL;
$this->view->class['Absentees']=NULL;

etc

There has to be a better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T01:35:24+00:00Added an answer on June 17, 2026 at 1:35 am

    This is kind of a band-aid for poor MVC design, but you could use a function like this,

    function idx(&$array, $index = 0, $default = null) {
      return isset($array[$index]) ? $array[$index] : $default
    }
    

    In the view if you normal did $class['Description']

    you would now use idx($class, 'Description')


    You should not have to pass each attribute of your model individually. Instead, just pass the entire model object and access it from the view. Example:

    $this->view->entry = $myModelInstance; where $myModelInstance is an instance of Entry or whatever class has the Code, Description, Image, Judges, etc attributes.

    Then in the view, use something like $entry->Description

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple MVC 3 forms that need form validation on certain fields (mostly
I need to dynamically populate a form from fields in a database using EXT
I want to pass html data from Controller to JSP. I am using Spring
I know you can can do this using MVC. But not sure if you
I'm trying to set up JSR-303 validation of forms using Spring MVC. I had
In ASP.net MVC: How should/Can I pass Form data (From the View) to the
I need to pass data from one view to another in MVC 4.0. I
I am looking for a way to pre-populate a Spring MVC form from values
Ok I know this is a long shot. I am using asp.net mvc on
I am using ASP.NET MVC 3 and am attempting to populate a list of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.