Welcome Guest, Not a member yet? Register   Sign In
How would I maintain dynamic form HTML after failed validation?
#1

[eluser]Unknown[/eluser]
I'm trying to make a page for creating invoices and there are a series of input arrays. Of course I don't know how many invoice lines will be required from the getgo so I'm using Javascript to append more lines as needed.

Once I submit the invoice form I do the validation and if it fails I want to be able to maintain the dynamic state of the page, but as it is right now it just resets back to the default state of the view.

Here's a snip of my code.

View:

Code:
<tbody>
                            <tr>
                                <td>&lt;?= form_input(array('name' => 'description[]', 'value' => set_value('description[]'))); ?&gt;</td>
                                <td>&lt;?= form_input(array('name' => 'rate[]', 'value' => set_value('rate[]'))); ?&gt;</td>
                                <td>&lt;?= form_input(array('name' => 'hours[]', 'value' => set_value('hours[]'))); ?&gt;</td>
                                <td>&lt;?= form_input(array('name' => 'cost[]', 'value' => set_value('cost[]'))); ?&gt;</td>
                            </tr>
                            <tr>
                                <td>&lt;?= form_input(array('name' => 'description[]', 'value' => set_value('description[]'))); ?&gt;</td>
                                <td>&lt;?= form_input(array('name' => 'rate[]', 'value' => set_value('rate[]'))); ?&gt;</td>
                                <td>&lt;?= form_input(array('name' => 'hours[]', 'value' => set_value('hours[]'))); ?&gt;</td>
                                <td>&lt;?= form_input(array('name' => 'cost[]', 'value' => set_value('cost[]'))); ?&gt;</td>
                            </tr>
                        </tbody>

Controller:
Code:
public function submit()
    {
        $this->load->library('form_validation');

        $this->form_validation->set_error_delimiters("<span  13px; font-size: 11px; vertical-align: middle; color:#ca0000'>", '</span>');
        $this->form_validation->set_rules('description[]', 'description', 'trim|htmlentities|required');
        $this->form_validation->set_rules('rate[]', 'rate', 'trim|htmlentities');
        $this->form_validation->set_rules('hours[]', 'hours', 'trim|htmlentities|required');
        $this->form_validation->set_rules('cost[]', 'cost', 'trim|htmlentities|required');

        if ($this->form_validation->run() == FALSE):
            $this->template->show('invoice_create');
            return;
        endif;
    }

Javascript (JQuery):
Code:
$('#lineAdd').click(function() {
        $('#invoiceLines tbody>tr:last').clone(true).insertAfter('#invoiceLines tbody>tr:last');
        $('#invoiceLines tbody>tr:last input').val('');
        return false;
    });

Hope someone can help. Thanks for any info!




Theme © iAndrew 2016 - Forum software by © MyBB