Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
672 views
in Technique[技术] by (71.8m points)

php - CodeIgniter's set_value() not re-populating

I've checked and re-checked my code, referencing the CI docs and other posts throughout the web, but I am unsuccessful at implementing the set_value() method for re-populating form fields after failed validation. Perhaps I am missing something very fundamental to the CI framework (I'm rather new to it), but your insight would be much appreciated.

I have the following in my controller method:

public function form_step2(){
    //Form Setup
    $this->load->helper('form');
    $this->load->helper('url');
    $this->load->library('form_validation');
    $data['title'] = $this->base_title;
    $data['base_url'] = base_url();

    //Validation Settings - must be set per step
    $this->form_validation->set_rules('request_type', 'Request Type', 'required');
    *...more of the same set_rules()*


    if ($this->form_validation->run() === FALSE) {
        ### Validation failed or New Form

        // Get form element data
        $data['request_types'] = $this->my_model->get_form_data('request_type');
        *...more of the same get_form_data() calls for loading form elements*

        //Generate Page from View Templates
        $this->load->view('templates/header', $data);
        $this->load->view('templates/form_step2', $data);
        $this->load->view('templates/footer');
    } else {
        ### Save to database
        $this->my_model->set_data($data);

        redirect('my_model/success','refresh');
    }
}

And in my view, a snippet of the code that is not re-populating:

<?php echo form_open('my_model/form_step2', array('class'=>'form-inline')); ?>

<label for="fname">First Name</label>
<input type="text" id="fname" name="fname" value="<?php echo set_value('fname'); ?>" />
<input type="submit" name="submit" value="Submit" />

I can't figure this one out, so thanks in advance for your help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

if you want to use set_data() you need to also use set_rules for that POST/GET field.

Since you've commented out all your set_rules I can not confirm that this is the issue but most likely it is.

please check if you have this line in your code

$this->form_validation->set_rules('fname', 'First name', 'trim|required');

So if you want to re-populate field with name="fname" you need to have set_rules() // as line above for it otherwise it won't process therefore set_value('fname') is empty.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...