Welcome Guest, Not a member yet? Register   Sign In
Undefined Variable problem
#1

[eluser]joeizang[/eluser]
hello everyone,

happy new year first of all. I have a question. I am developing an application using CI 2.10 with the following libraries: ion_auth, phil sturgeons template library. At the moment this is all the libraries I will be using for now. the problem I have is that I created a form and have validation errors sent to a partial as a variable from my controller like so:

Code:
//Controller method processing form
//proper validation happens here.
        if($this->form_validation->run() == FALSE)
        {
            //set dynamic data to be added to the view using the template library style
            
                 $data1 = array(
                     'valid_errors' => validation_errors()
                 );
            $this->template->set_partial('maincontent','partials/student_login')
                    ->build('layouts/default',$data1);

Code:
//this is a snippet from the partial on the view
<p>
                  &lt;form action="loginstudent" method="POST" id="studentlogin"&gt;
                  <p><label for="Username"><strong>Username :</strong>&lt;input name="user_name" id="uname" type="text"/&gt;&lt;/label></p>
                  <p><label for="Password"><strong>Password :</strong>&lt;input name="password" id="password" type="password"/&gt;&lt;/label></p>
                  <p>&lt;input name="submitbtn" id="logbtn" type="submit" value="Login"/&gt;&lt;/p>
                  &lt;/form&gt;
           </p>
              &lt;?php
                    echo '<p class="errors"><strong>'.$this->ion_auth->errors().'</strong></p>';
                    echo '<p class="errors"><strong>'.$valid_errors.'</strong></p>';
                    
              ?&gt;

but all I get in the regions where I have data from the controller I get this:

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: valid_errors
Filename: partials/student_login.php
Line Number: 15

But when there is an error from trying to login, the errors appear accordingly so where am I missing it?

Thanks for y'all help;-)
#2

[eluser]InsiteFX[/eluser]
Try passing $data not $data1
#3

[eluser]joeizang[/eluser]
That did not work insiteFX. the errors even worse since there ain't that variable in my method anyways but thanks for trying though.
#4

[eluser]PhilTem[/eluser]
You get the error before submitting the form? For me it looks at least pretty obvious. If the if-case isn't validated to TRUE, then it won't get inside the if-part. So no variable $data1 will be generated therefore the row

Code:
->build('layouts/default',$data1);

accesses a variable that doesn't exist (undefined variable). That's prompted by CI since it's very sensitive towards missing variables. Try to initiate $data as an empty array before you have the if-part Wink
#5

[eluser]InsiteFX[/eluser]
This is what PhilTem is saying to do.
Code:
$data1 = array();

//Controller method processing form
//proper validation happens here.
        if($this->form_validation->run() == FALSE)
        {
#6

[eluser]joeizang[/eluser]
Hey InsiteFX,

thanks for helping me thus far. I did just what you said and still I get that error. I thought I should maybe do this to data1:

Code:
$this->data1 = array();
at the top of the class but I still get no love from this. Am I missing something? I am sure I am cos I have looked at pyrocms that uses the same template library and there this type of usage is the same there?!
#7

[eluser]InsiteFX[/eluser]
Try placing a comma on the end.
Code:
$data1 = array(
    'valid_errors' => validation_errors(),
);

// or try this:
$this->load->vars($data1);
$this->template->set_partial('maincontent','partials/student_login')
               ->build('layouts/default');

You may need to assign the validation_errors() to a varianle first then pass that in your $data1 array.
#8

[eluser]CroNiX[/eluser]
Why pass it at all? In the view, just echo validation_errors(). If there are no errors, nothing will print.
#9

[eluser]joeizang[/eluser]
Cronix,

thanks for that. why I was going the other way was for a while there using the template library and a lower version of CI (CI 2.0.3) I just couldn't see the validation_errors() echoing in my view but I could do that if I used the method that had been giving all the trouble. Now using 2.10 I just continued using the code I had written before so thanks for stating what should've been obvious. Big thanks to InsiteFX and PhilTerm for taking the time to talk to me about this. Much obliged.




Theme © iAndrew 2016 - Forum software by © MyBB