CodeIgniter Forums
Problem using Form Validation library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Problem using Form Validation library (/showthread.php?tid=12419)



Problem using Form Validation library - El Forum - 10-18-2008

[eluser]Bramme[/eluser]
Okay, I'm using the new form validation library, but it doesn't seem to be working and I don't see anything I'm doing wrong.

controller:
Code:
function register() {
        
        $this->load->library('form_validation');
        
        $data['register_form'] = array(
                                    'name' => array('name' => 'name',  'value' => set_value('name')),
                                    'password' => array('name' => 'password',  'value' => ""),
                                    'password_check' => array('name' => 'password_check',  'value' => ""),
                                    'email' => array('name' => 'email',  'value' => set_value('email')),
                                    'question' => array('name' => 'question',  'value' => set_value('question')),
                                    'answer' => array('name' => 'answer',  'value' => set_value('answer')),
                                    'submit' => array('name' => 'subm_registration', 'value' => 'Register')
        );
        
        if ($this->form_validation->run('register') == TRUE)
        {
            $this->template->write_view('ciforum_content', 'ciforum/register_success');
        }
        else {
            $this->template->write_view('ciforum_content', 'ciforum/register', $data);
        }    
        $this->template->render();
    }
My rules are set in a config file
Code:
$config = array(
    'register' => array(    
        array(    
            'field' => 'name',
            'label' => 'username',
            'rules' => 'trim|required|xss_clean'
        ),
        array(
            'field' => 'password',
            'label' => 'password',
            'rules' => 'trim|required|min_lenght[6]|xss_clean'
        ),
        array(
            'field' => 'password_check',
            'label' => 'second password',
            'rules' => 'trim|required|matches[password]|xss_clean'
        ),
        array(
            'field' => 'email',
            'label' => 'e-mail address',
            'rules' => 'trim|required|xss_clean'
        ),
        array(
            'field' => 'question',
            'label' => 'secret question',
            'rules' => 'trim|required|xss_clean'
        ),
        array(
            'field' => 'answer',
            'label' => 'answer',
            'rules' => 'trim|required|xss_clean'
        )
    )
);
and the form looks like this:
Code:
<?= form_open($this->uri->uri_string()) ?>
<p>&lt;?= validation_errors() ?&gt;</p>
<ul class="form_table">
    <li>&lt;?= form_label("<span>Your name</span>", "form_input", $register_form['name']) ?&gt;</li>
    <li>&lt;?= form_label("<span>Your password</span>", "form_password", $register_form['password']) ?&gt;</li>
    <li>&lt;?= form_label("<span>Enter again</span>", "form_password", $register_form['password_check']) ?&gt;</li>
    <li>&lt;?= form_label("<span>Your e-mail</span>", "form_input", $register_form['email']) ?&gt;</li>
    <li>&lt;?= form_label("<span>Your super secret question</span>", "form_input", $register_form['question']) ?&gt;</li>
    <li>&lt;?= form_label("<span>The super secret answer</span>", "form_input", $register_form['answer']) ?&gt;</li>
    <li class="right">&lt;?= form_submit($register_form['submit']) ?&gt;</li>
</ul>
&lt;?= form_close() ?&gt;

Can anyone of you see something wrong?


Problem using Form Validation library - El Forum - 10-18-2008

[eluser]Bramme[/eluser]
Solved. I's a bug in the form_validation library that had allready been submitted: http://codeigniter.com/bug_tracker/bug/5504/


Problem using Form Validation library - El Forum - 06-30-2009

[eluser]whobutsb[/eluser]
Hey Bramme,
I'm trying to do something very similar to what you were trying to do, basically set the value of the form input in the controller and then sending it to the view. How did you fix this issue?

Steve


Problem using Form Validation library - El Forum - 07-01-2009

[eluser]Bramme[/eluser]
I have no idea what could be causing the problem at your end without more information.

One thing comes to mind: are you setting any rules?


Problem using Form Validation library - El Forum - 07-01-2009

[eluser]xwero[/eluser]
I have to say it's a strange bug. it should be located in the loader _ci_class_init method where it looks for the associate config file. The filename formats that are search for are an all lowercase and a first letter capitalised string. This means the bug should affect all class config file calls, could you try to create a config file for another class to confirm the bug?

There is something off with the form_label functions in your view file. Why do you add a span tag? And where are the inputs, or is this not the whole view file?


Problem using Form Validation library - El Forum - 07-01-2009

[eluser]Bramme[/eluser]
My problem has long been solved, as it was a topic from october 2008. No idea what whobutsb is using in his code.

Afaik, the bug has also been resolved.


Problem using Form Validation library - El Forum - 07-01-2009

[eluser]xwero[/eluser]
oops i didn't check the date on the first posts, sorry.


Problem using Form Validation library - El Forum - 07-01-2009

[eluser]whobutsb[/eluser]
Sorry about not giving more information about the problem. Here is the thread I started yesterday. http://ellislab.com/forums/viewthread/121691/

Basically what I have done is create a Surveying system that dynamically creates questions for the survey taker depending on either a survey session ID or a surveyID. The difference between the two is with the survey session id we already have their contact information, from a excel list, or a database table. I'm building the questions dynamically in a function

Code:
$data['survey_questions'] = $this->questions->build_survey_questions($surveyID)
;

The output of this I send to the view. I also have another function that sets the form validation. I call it like this:
Code:
$this->validation->survey_validation($surveyID);

But functions are in there own libraries, and called within the controller. When I create the questions I always set the value using one of the form_validation set_value(), set_radio, set_dropdown, etc. No matter if there is required questions or not. Here is a sample of how I create the questions.

Code:
function build_survey_questions($surveyID){
        
        $CI =& get_instance();
        
        $CI->load->model('question_model');
        
        $survey_questions = '';
        //Get the survey questions
        $surveyQuestions = $CI->survey_model->select_survey_questions($surveyID);
        
        foreach($surveyQuestions as $surveyQuestion){
                
            //Get the survey question title and type
            $question = $CI->question_model->select_question_details($surveyQuestion->questionID);
            
            //Get the survey answer options
            $questionAnswers = $CI->question_model->select_question_options($question->questionID);
            
            
            //Check if its a required question and build the title
            if($surveyQuestion->questionRequired){
                $questionTitle = "<label class='required-question'>" .$question->questionTitle ."*</label>\n";  
            }else{
                $questionTitle = "<label>" .$question->questionTitle ."</label>\n";
            }
                        
            switch($question->type){
                case "TEXT":
                    //Textarea
                    
                    $survey_questions .= "<div class='form-item'>\n";
                    $survey_questions .= $questionTitle;
                    $data = array(
                        'name'        => $question->questionID,
                        'value'        => set_value($question->questionID)
                    );
                    $survey_questions .= form_textarea($data);
                break;
                
                case "SHORT":
                    //Short Answer Questions
                    $survey_questions .= "<div class='form-item text'>\n";
                    $survey_questions .= $questionTitle;  
                    $data = array(
                        'name'    => $question->questionID,
                        'value'    => set_value($question->questionID)
                    );
                    $survey_questions .= form_input($data);
                break;
                
                case "DROPDOWN":
                    //Drop Down Menus
                    $survey_questions .= "<div class='form-item'>\n";
                    $survey_questions .= $questionTitle;
                    foreach($questionAnswers as $questionAnswer){
                        $options[$questionAnswer->answer_value] = $questionAnswer->answer_title;
                    }
                    $survey_questions .= form_dropdown($question->questionID, $options, set_radio("$question->questionID"));
                break;
                
                case "MULTIPLE":
                     //Radio Buttons
                    $survey_questions .= "<div class='form-item radio'>\n";
                    $survey_questions .= $questionTitle;  
                    
                    foreach($questionAnswers as $questionAnswer){
                    
                        $data = array(
                            'name'        => $question->questionID,
                            'value'        => $questionAnswer->answer_value
                        );
                        $survey_questions .= form_radio($data) ."<span>" .$questionAnswer->answer_title ."</span>\n";
                    }
                break;
                
                case "LABEL":
                    //Section Labels
                    $survey_questions .= "<div class='form-item'>\n";
                    $survey_questions .= "<h2>" .$question->questionTitle ."</h2>";
                break;
            }    
        
            $survey_questions .= "</div>\n"; // .form-item
            
        }
        
        return $survey_questions;
    
    } //build_survey_questions

The validation works fine it forces the user to fill out the appropriate fields. My main problem is the validation is not returning the values that the user inputted. And with some surveys that are 30 questions long it would get pretty annoying to re enter your answers.