Welcome Guest, Not a member yet? Register   Sign In
Repopulating a form
#1

[eluser]leinad[/eluser]
I know this problem has been posted about numerous times and I have read those threads, but I am still having a problem that seems like it should be simple to solve. Anyway, here is my setup:

Controller:
Code:
function createevent()
  {
    $this->freakauth_light->check('user');
    $this->_prepForm();

    $this->load->model('rushevents');

    $create['events'] = $this->rushevents->getEvents()->result();

    $data['body'] = $this->load->view('createevent', $create, TRUE );

    $this->load->view('main', $data);
  }

  function create()
  {
    if( isset( $_POST['createevent'] ) )
    {
      $this->freakauth_light->check();

      if( $this->validation->run() == FALSE )
      {
        redirect( 'brothers/createevent' );
      }

      unset( $_POST['createevent'] );
      $this->load->model('rushevents');
      $this->rushevents->addEvent( $_POST );

      $this->db_session->set_flashdata('flashMessage', 'Event successfully created.' );

    }
    redirect( 'brothers/createevent' );
  }

  function _prepForm()
  {
    $this->load->library('validation');

    $rules['Event'] = 'required|xss_clean';
    $rules['Date_and_Time'] = 'xss_clean';
    $rules['Location'] = 'xss_clean';
    $rules['Additional_Info'] = 'xss_clean';

    $this->validation->set_rules( $rules );

    $fields['Event'] = 'Event';
    $fields['Date_and_Time'] = 'Date and Time';
    $fields['Location'] = 'Location';
    $fields['Additional_Info'] = 'Additional Info';

    $this->validation->set_fields( $fields );
  }

View
Code:
<?php echo $this->validation->error_string; ?>

<?php echo form_open('brothers/create', array('id' => 'generic_form')) ?>

  <p><label for="Event">Event:</label>
  &lt;?php echo form_input( array( 'name'=>'Event',
                                'id'=>'Event',
                                'maxlength'=>'128',
                                'size'=>'45',
                                'value'=>$this->validation->Event ) )?&gt;

  </p>

  <p><label for="Date_and_Time">Date and Time:</label>
  &lt;?php echo form_input( array( 'name'=>'Date_and_Time',
                                'id'=>'Date_and_Time',
                                'maxlength'=>'128',
                                'size'=>'45',
                                'value'=>$this->validation->Date_and_Time ) )?&gt;

  </p>

etc..

When a user goes to brothers/createevent, the form is then submitted to brothers/create which will validate the information then redirect back to the form so another event can be added. This will also allow the user to hit refresh without data being reposted. The _prepForm idea was shamelessly stolen from another CI user on these boards because I think it's a good idea.

The problem is when the data does not validate, the old values of the form are not placed in the text fields with $this->validation->variable_name and I can't figure out why. Anyone see something I am missing?

Thanks.
#2

[eluser]imzyos[/eluser]
yeahp i have the same problem, som times i make and extra view to manage with this or just recall the same view but diaplay and error from validation class, $this->validation->Date_and_Time, but remember $this->input->post('Date_and_Time'); the value are there too
#3

[eluser]Grahack[/eluser]
I have no solution for you but a link to a tutorial: http://ellislab.com/forums/viewthread/67643/




Theme © iAndrew 2016 - Forum software by © MyBB