Welcome Guest, Not a member yet? Register   Sign In
Setting Default Form Values with Validation and Post Variables
#1

[eluser]Minus[/eluser]
Although my question is code related, it's more of a conceptual issue I'm having trouble wrapping my head around.

I've been doing more work with CI and forms and have ran into something I'm sure there is an easy way to handle, I'm just missing it Smile

I'm building out a simple CRUD app. When designing the "edit" state to perform an update, I build out a form using the CI form helpers (I create an array in my controller and pass the variables to the view). The form helper array takes a value parameter that I can easily set to be whatever value I pull from the DB. Simple enough.

When I want to perform an insert and have a "new" item to add with my CRUD, I set my value to check the POST variable of the field so that if validation fails, the value the user entered becomes "sticky".

My issue is how do I combine these two things? I want to set a default or base value for my field, but if/when the user edits it (it then becomes the POST value) and needs to be returned (vs. the original value I set).

Is there an easy way to do this? Am I missing something? Smile

For smaller forms I've done something like checking to see if POST is set and then assigning that value to my value field otherwise use the default value. This doesn't seem ideal and when you have bigger forms doesn't scale well.

Any help or thoughts would be appreciated.

-Minus
#2

[eluser]regal2157[/eluser]
I may be a bit lost, but are you talking about using the set_value() part of the form helper?

e.g.
Code:
<?php
echo form_input('Username', set_value('Username'));

Sorry if I misunderstood your question..
#3

[eluser]Minus[/eluser]
Here is part of my controller:
Code:
if (isset($reset['new_password']))
            {
                $pass_value = $reset['new_password'];
            }
            else
            {
                $pass_value = $_POST['old'];
            }

            if (isset($reset['identity']))
            {
                $email_value = $reset['identity'];
            }
            else
            {
                $email_value = $_POST['email'];
            }
            
            $this->form_validation->set_rules('old', 'Old Password', 'required');
            $this->form_validation->set_rules('new', 'New Password', 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[new_confirm]');
            $this->form_validation->set_rules('new_confirm', 'Confirm New Password', 'required');
            $this->form_validation->set_rules('email', 'Email', 'required');
            
            if ($this->form_validation->run() == false)
            {     //display the form
                //set the flash data error message if there is one
                $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');

                $this->data['old_password'] = array('name' => 'old',
                    'id' => 'old',
                    'type' => 'hidden',
                    'value' => $pass_value,
                );
                $this->data['new_password'] = array('name' => 'new',
                    'id' => 'new',
                    'type' => 'password',
                    'value' => $this->form_validation->set_value('new'),
                );
                $this->data['new_password_confirm'] = array('name' => 'new_confirm',
                    'id' => 'new_confirm',
                    'type' => 'password',
                    'value' => $this->form_validation->set_value('new_confirm'),            
                );
                $this->data['user_id'] = array('name' => 'email',
                    'id' => 'email',
                    'type' => 'hidden',
                    'value' => $email_value,
                );

                $this->data['email'] = $email_value;
                
                //render
                $this->load->view('password_choose', $this->data);
                }
                else
                {        
                    $change = $this->ion_auth->change_password($this->input->post('email'), $this->input->post('old'), $this->input->post('new'));

                    if ($change)
                    { //if the password was successfully changed
                                    
                        $logout = $this->ion_auth->logout();
                    
                        $this->session->set_flashdata('notification_success', 'Please use your new password to sign in below.');
                        redirect('login', 'refresh');
                                            }
                    else
                    {
                        $this->session->set_flashdata('message', 'We had a problem updating your password.  Please retry by entering your email again below.');
                        redirect('password/forgot', 'refresh');
                    }
                }    
        }
        else
        {
            $this->session->set_flashdata('notification_info', 'Please use the form below to choose a new password.');
            redirect('password/forgot', 'refresh');
        }
    }

Here is my View:

Code:
<div class="box-header">
        <h2>&lt;?php echo $this->config->item('app_name');?&gt; Choose Password</h2>
    </div>
    <div class="box">
            
          &lt;?php  if (!empty($message))     { echo '<div class="notification error">'.$message.'</div>'; }  ?&gt;      
        
        <p>Please choose a password for <b>&lt;?php echo $email; ?&gt;</b></p>
            
        &lt;?php echo form_open('password/choose','class="form"');?&gt;

          <p>
              &lt;?php echo form_input($old_password);?&gt;
          </p>
      
          <p><label class="strong" for="new_password">New Password:</label>
              &lt;?php echo form_input($new_password,'','class="small"');?&gt;
          </p>
      
          <p><label class="strong" for="confirm_new_password">Confirm New Password:</label>
              &lt;?php echo form_input($new_password_confirm,'','class="small"');?&gt;
          </p>
              
              &lt;?php echo form_input($user_id);?&gt;
          <p>&lt;?php echo form_submit('submit', 'Save Password', 'class="button"');?&gt;
          </p>
        &lt;?php echo form_close();?&gt;
    </div>
I've been passing the "value" for my input with the $data array from my controller.
Code:
$this->data['user_id'] = array('name' => 'email',
                    'id' => 'email',
                    'type' => 'hidden',
                    'value' => $email_value,
                );
or
Code:
$this->data['new_password_confirm'] = array('name' => 'new_confirm',
                    'id' => 'new_confirm',
                    'type' => 'password',
                    'value' => $this->form_validation->set_value('new'),
#4

[eluser]Minus[/eluser]
I started running out of characters in the last post...

The crux of my issue is that I'm always going to need to set different values in the fields depending on if the form has been submitted (submission has been attempted) or not. I suppose I could set something like a hidden field and then check to see if that POST value is set when building the page.

So the logic would look something like:

- Check if foo POST value exists

- If not, build form with default values from the DB

- If does exist, use POST values

Just wondering if there is a more elegant way to do it...
#5

[eluser]Kamarg[/eluser]
Use the set_value functions. They allow you to have a default value in the event there is no post data. So you pass your database value as the default and it will automatically use the correct data (post or database).




Theme © iAndrew 2016 - Forum software by © MyBB