Welcome Guest, Not a member yet? Register   Sign In
One function form handling
#1

[eluser]Maglok[/eluser]
I have to make forms a lot in webapplications. I usually just make a form in a function and then create the code again if I need an edit form.

Example: I have create user and edit user. Both are basically the same form, but the edit form has preset values from the database.

I have a private function that makes the form for me at the moment. I just can't seem to get it to handle both default values and database values simply.

Code:
private function form_account() {
        $this->config->load('pona');
        $permissions = $this->config->item('pona_permissions');
        $faculties = $this->config->item('pona_faculties');

        $this->form
            ->text('first_name', 'First name', 'required|max_length[40]')
            ->text('last_name', 'Last name', 'required|max_length[40]')
            ->text('email', 'Email address', 'required|max_length[40]|valid_email')
            ->select('permission', $permissions, 'Access level', '1', 'required')
            ->select('faculty', $faculties, 'Faculty', '1', 'required')
            ->password('password', 'Password', 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') .']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[password_confirm]')
            ->password('password_confirm', 'Password confirmation', 'required')
            ->submit('Create');
    }

This is using the Form Generation Library library, but it hooks to the CI one so that shouldn't matter.

I can add something crude like parameter with database info, if it is set then display the form with database values, otherwise show default values, but that would required a big if statement that would still require me to write out the form twice and it kinda defeats the purpose.

It's probably a thing people encountered before, I just can't seem to think of a quick way to be able to say:

Code:
$this->form
    ->text('first_name', 'First name', 'required|max_length[40]', $accountinfo->first_name)

And it would automatically NOT fill that out if it wasn't filled in. Instead it throws me errors, because $accountinfo->first_name is not set.
#2

[eluser]eoinmcg[/eluser]
one approach is that if $accountinfo is empty, then you generate a object with all the relevant properties.
for example:
Code:
if (!is_object($accountinfo) )
{
    $accountinfo = $this->crud->get_empty_record()
}


    //then in my crud model...
    
    //you'll need to expand this a bit if you want to join multiple tables
    function get_empty_record($table, $array = FALSE)
    {

        $fields = $this->db->list_fields($table);

        if($array)
        {
            $row = array();
            foreach ($query->list_fields() as $field)
            {
                $row[$field] = '';
            }
        }
        else
        {
            $row = new stdClass();
            foreach ($query->list_fields() as $field)
            {
                  $row->$field = '';
            }
        }

        return $row;


    }

although there probably is a cleaner way to do it.

i'd also be interesting in hearing other solutions...
#3

[eluser]Maglok[/eluser]
Ah you mean implement a model function the returns an empty row? Sounds a bit sloppy indeed, but at least shorter in code then most I had thought of so far.
#4

[eluser]Maglok[/eluser]
At the moment I am leaning towards Eoinmcg's solution of the problem.

It'll have to do.
#5

[eluser]eoinmcg[/eluser]
[quote author="Maglok" date="1296585039"]At the moment I am leaning towards Eoinmcg's solution of the problem.

It’ll have to do.
[/quote]

well, hey, you're welcome Tongue

seriously, though, i was also hoping to see someone chip in with a better approach.

just a thought but, you could extend the form creation library, delegateingthe db lookup and populating the field values to the lib. so, for example when you load the library you can, optionally, map the required db fields to the relevant form inputs plus record id. although, that would require even more lines of code and effort but at least you wouldn't have to fill up your controllers with extra checks and model calls...
#6

[eluser]Maglok[/eluser]
The worst bit is that I am trying to code a pretty dynamic way of making forms for a client that wants to be able to add forms. Tongue Irony.
#7

[eluser]seanloving[/eluser]
Here are the basics of how I do it - these are methods in my products controller application/modules/inventory/controllers/products
Code:
function add()
{
  // MODEL  
   $headerdata['title'] = "Add Product";
   $maindata = $this->_form(); // get the blank form as a view partial  
  // VIEW
   $this->load->view('header', $headerdata);
   $this->load->view('inventory/products', $maindata);
   $this->load->view('footer');
}

function edit($pn)
{
  // MODEL  
   $headerdata['title'] = "Edit Product";
   if( ! isset($_POST) ) $_POST = $this->products_model->get_row( array('pn'=>$pn) );
   $maindata = $this->_form( $_POST ); // get the pre-populated form as a view partial
  // VIEW
   $this->load->view('header', $headerdata);
   $this->load->view('inventory/products', $maindata);
   $this->load->view('footer');
}

// returns the form as a view partial (either blank, or else pre-populated with data)
function _form( $post )
{
  // form request (or sumbission) from either add() or edit($pn)
       $action = isset($post) ? 'edit' : 'add';
  
  // submits back to 'add' or 'edit'
   $this->form->open('inventory/products/'.$action)
   ->text('product|productID', 'Product', 'required|trim|max_length[10]', $this->input->post('product'), array('style'=>''))
   ->text('description|descriptionID', 'Description', 'required|trim|max_length[80]', $this->input->post('description'), array('style'=>'') )
   ->submit( ($action=='add') ? 'Add': 'Update', $action )
   ->model('products_model', $action.'_product') //interacts with database if submitted form is valid
   ->validate(); // not sure if FGL requires calling this validate() method

  if( $this->form->valid )
  {
   redirect( 'somewhere' ); // set some flash data to give a nice success message
  }
  $data['form'] = $this->form->get(); // gets the submitted form data as a string
  $data['errors'] = $this->form->errors;
  return $data;
}

cheers





Theme © iAndrew 2016 - Forum software by © MyBB