Welcome Guest, Not a member yet? Register   Sign In
Using $fields to set validation vars
#1

[eluser]absurdparadox[/eluser]
To preface this, I'm quite a php noob (with 8 years development experience in other languages). I'm trying to figure out how to preset my validation variables from a query for a form that is an Add/Edit form. So far what I have is the following:

Code:
if (empty($_POST))
        {      
            $row = $this->getuser($id);
        
            $this->validation->id = $row->id;
            $this->validation->username = $row->username;
            $this->validation->email = $row->email;
            $this->validation->firstname = $row->firstname;
            $this->validation->lastname = $row->lastname;
            $this->validation->company = $row->company;
            $this->validation->address1 = $row->address1;
            $this->validation->address2 = $row->address2;
            $this->validation->city = $row->city;
            $this->validation->state = $row->state;
            $this->validation->zip = $row->zip;
            $this->validation->wphone = $row->wphone;
            $this->validation->hphone = $row->hphone;
            $this->validation->mphone = $row->mphone;
            $this->validation->status = $row->status;
            $this->validation->roleid = $row->roleid;
            
          
        }

What I would like to do is use the $fields array set for the set_fields() function to set these progamatically, example

Code:
while( $element = each( $fields ) )
            {
                $fieldname = $element['key'];
                $this->validation->$fieldname = $row->$fieldname;
            }

Not sure how to do this in PHP though... I found stuff about using double $$ for variable of variables, but I can't seem to find anything that works. Can someone give me a hand?
#2

[eluser]absurdparadox[/eluser]
To answer my own post:

Code:
if (empty($_POST))
        {      
            $query = $this->getuser($id);
            $row = $query->row(1);
        
            while( $element = each( $fields ) )
            {
                $this->validation->{$element['key']} =  $row->{$element['key']};
            }
}

This will set everything you have in your fields array to your validation array using your query data.

Now, I guess my next question is, is there a better way of pre-loading an add/edit form than this?
#3

[eluser]Colin Williams[/eluser]
We've discussed this before, that Validation could use something like a set_values() method, but I currently use a similar method. I, however, don't load up default values as properties of the Validation object (I don't see the point in doing that). I go the other way around. I have a $form['values'] array that I use in my View to populate fields. Then, after a failed submission, I load up this array with the submitted values, and not the default ones

Code:
if ( ! $this->validation->run())
{
  if ($this->validation->error_string)
  {
    foreach ($form['fields'] as $key => $label)
    {
      $form['values'][$key] = $this->validation->$key;
    }
  }
  // yadda yadda yadda
}




Theme © iAndrew 2016 - Forum software by © MyBB