Welcome Guest, Not a member yet? Register   Sign In
Survey System Form Validation
#1

[eluser]whobutsb[/eluser]
Hi All,
I'm working on building a surveying system for my company. And the way I designed it is modular with different functions doing different parts to put together the survey and its questions.

I have a function in the code that checks to see if a question is required and needs form_validation. The submission function looks like this:
Code:
function submitSurvey($surveyID){
        //Load the Mdoels
        $this->load->model('survey_model');
        $this->load->model('question_model');
        
        //Load the form validation
        $this->load->library('form_validation');
        
        //Select the survey information
        $survey = $this->survey_model->select_survey($surveyID);
        
        
         $this->survey_validation($surveyID);

        //Run the validation
        if ($this->form_validation->run() == FALSE){
            
            $this->takeSurvey($surveyID);
        }
        else{
            echo "Success";
        }
    
    }    //end submitsurvey function

And the survey_validation function looks like this:
Code:
//Check if validation is needed
    function survey_validation($surveyID){
        //Get the questions
        $questions = $this->survey_model->select_survey_questions($surveyID);
        
        $config = array();
        //Set the form validation
        foreach($questions as $question){
            if($question->questionRequired){
                //Get the Question Title
                $questionDetails = $this->question_model->select_question_details($question->questionID);
                
                //Create a rule for that question
                $config[] = array(
                    'field'    => "$question->questionID",
                    'label'    => $questionDetails->questionTitle,
                    'rules'    => 'required'
                );
            }
        }
        $config[] = array(
            'field'    => 'email',
            'label'    => 'Email Address',
            'rules'    => 'required|valid_email'
        );
        $this->form_validation->set_rules($config);
    }

In my validation script do I need to return anything? The Validation is working so I'm guessing not but the repopulation of the fields is not happening. I have a function that also builds the questions and I added the set_values and the set_selects to them. I just can't seem to figure out how to get everything to repopulate.
#2

[eluser]whobutsb[/eluser]
One thing I forgot to note is that I'm building my form and questions within the controller first and then sending them to the view. Here is a example of how i'm building my my contact questions:

Code:
function build_contact_questions(){
        //Must enter the email address and first/lastname
        $contact  = "<div class='form-item text'>\n";
        $contact  .= "<label class='required-question'>Email Address</label>\n";
        $contact  .= form_input('email', set_value('email'));
        $contact  .= "</div>\n";
        
        $contact  .= "<div class='form-item text'>\n";
        $contact  .= "<label>First Name</label>\n";
        $contact  .= form_input('firstName', set_value('firstName'));  
        $contact  .= "</div>\n";
        
        $contact  .= "<div class='form-item text'>\n";
        $contact  .= "<label>Last Name</label>\n";
        $contact  .= form_input('lastName', set_value('lastName'));
        $contact  .= "</div>\n";
        
        $contact  .= "<hr />";
        
        return $contact;
    }




Theme © iAndrew 2016 - Forum software by © MyBB