Using form_builder to create 2 forms in one view.

edited January 2013 in Modules
Good evening,

I try to use form_builder to create 2 different form in 1 view.

$this->load->library('form_builder', array('text_size_limit' => '20', 'submit_value' => 'Update', 'form_attrs' => array('action' => site_url('Update'), 'method' => 'post'), 'show_required' => FALSE)); $form_1 = $this->form_builder->render_divs(); $this->load->library('form_builder', array('text_size_limit' => '20', 'submit_value' => 'Convert', 'form_attrs' => array('action' => site_url('Convert'), 'method' => 'post'), 'show_required' => FALSE)); $form_2 = $this->form_builder->render_divs(); // Then I use this in the same view. <div> <?php echo $form_1; ?> </div> <div> <?php echo $form_2; ?> </div>

The result is that both forms have the same submit value = Update.

When I look into the code it seems like the submit_value only init once from the form_1.
In this case, how could I make the second form has the different value than the form_1?

Thank you very much.

Comments

  • edited 6:22AM
    That's odd. This is n 1.0?

    Have your tried setting the public param like:

    # after second load
    $this->form_builder->submit_value = 'Convert';
    $this->form_builder->render_divs();

    Appears to be what set_params() is doing anyway.
  • edited 6:22AM
    I still using the 0.9.3 version.
    Your solution is working like a charm! Thank you very much!

    I will try this again with 1.0.
  • edited January 2013
    I'd perhaps use the initialize method to load the parameters for each like so:
    $this->load->library('form_builder'); $this->form_builder->initialize(array('text_size_limit' => '20', 'submit_value' => 'Update', 'form_attrs' => array('action' => site_url('Update'), 'method' => 'post'), 'show_required' => FALSE)); $form_1 = $this->form_builder->render_divs(); $this->form_builder->initialize(array('text_size_limit' => '20', 'submit_value' => 'Convert', 'form_attrs' => array('action' => site_url('Convert'), 'method' => 'post'), 'show_required' => FALSE)); $form_2 = $this->form_builder->render_divs(); // Then I use this in the same view. <div> <?php echo $form_1; ?> </div> <div> <?php echo $form_2; ?> </div>
  • edited 6:22AM
    I have another question about using 2 forms please.

    I observed that if one of the form does not pass the validation, it will display error in both forms.
    In this case, what is the best way to display the error in the correct form?

    Thank you very much.
  • edited 6:22AM
    You can pass a different validator object to the second form:
    $this->load->library('form_builder'); $this->load->library('validator'); $this->form_builder->initialize(array('text_size_limit' => '20', 'submit_value' => 'Update', 'form_attrs' => array('action' => site_url('Update'), 'method' => 'post'), 'show_required' => FALSE)); $form_1 = $this->form_builder->render_divs(); $this->form_builder->initialize(array('text_size_limit' => '20', 'submit_value' => 'Convert', 'form_attrs' => array('action' => site_url('Convert'), 'method' => 'post'), 'show_required' => FALSE)); $validator2 = new Validator(); $this->form_builder->set_validator($validator2); $form_2 = $this->form_builder->render_divs(); // Then I use this in the same view. <div> <?php echo $form_1; ?> </div> <div> <?php echo $form_2; ?> </div>
Sign In or Register to comment.