Welcome Guest, Not a member yet? Register   Sign In
Validation and view within a view
#11

[eluser]gtech[/eluser]
my mistake! you wont get fields printed out as you do not have a $this-> in front of setting up the fields array... so you have to pass the field array as so:
Code:
$this->content->addColumn1Unit($this->load->view('quote1', $fields, true));

then in the quote1 view you can access

<?=$name?>
<?=$phone?>
[strike]
also note that
<?=$this->validation->name_error?>
should work in the quote1 view. this is because the validation object has a $this reference... if its blank it is because it is not set. (it wont get set through the first run through the form) it will only get displayed when you submit the form to the controller function again with blank postdata for the name field.

if you did not have access to the variable in the view you would get a php error.
[/strike]
#12

[eluser]ehicks727[/eluser]
[quote author="gtech" date="1210714209"]

also note that
<?=$this->validation->name_error?>
should work in the quote1 view as that is part of the $this... if its blank it is because it is not set. (it wont get set through the first run through the form) it will only get displayed when you submit the form to the controller function again with blank postdata for the name field.

if you did not have access to the variable in the view you would get a php error.[/quote]

Thanks for your help. I'm going to have to play with this some more, obviously, but I've got to switch gears and work on something else for a while. If I figure out a solution, or what I'm doing wrong, I'll post it here.

thanks again for your help.
#13

[eluser]gtech[/eluser]
ahh its blank because you need to do the validation run BEFORE you load the view
#14

[eluser]gtech[/eluser]
here you are I posted a solution up
home.php (controller)
Code:
<?php
class Home extends Controller   {
  function Home(){
    parent::Controller();
  }

  function posttome() {
        $this->load->helper('url', 'form');
        $this->load->library('validation');

        // I load your library here, take out if in the autoload list.
        $this->load->library('content');

        $rules['name']    = "required";
        $rules['phone']    = "required";        
        $this->validation->set_rules($rules);        
        $fields['name']    = 'Name';
        $fields['phone'] = 'Phone';

        $this->validation->set_fields($fields);
        //need to do validation run here as this sets the validation variables
        $res = $this->validation->run();
        $this->content->addColumn1Unit($this->load->view('quote1', $fields, true));
        $data['content'] = $this->content->generate();
      
        //check the result of the validation run
        if ($res == FALSE) {
            $this->load->view('quote2', $data);
        } else {
            echo "FORM OK";      
        }
    }
}
?>

quote1.php (view) just to show whats available
Code:
<hr>
fieldname: &lt;?=$name?&gt;<br>
validation name: &lt;?=$this->validation->name?&gt;<br>
validation name error: &lt;?=$this->validation->name_error?&gt;<br>
validation error_string: &lt;?=$this->validation->error_string?&gt;
<hr>

quote2.php (view) I echo $content at the start
Code:
&lt;?=$content?&gt;
<div class="form1">
  &lt;form action="&lt;?=site_url()?&gt;/home/posttome" method="POST"&gt;
  <fieldset><legend>Organization Information required for Quote</legend>
     <p><label for="name" class="left">Your Name (requestor)</label>
        &lt;input type="text" name="name" value="&lt;?=$this-&gt;validation->name;?&gt;" maxlength="100" size="9" class="field" tabindex="1" > * &lt;?=$this->validation->name_error; ?&gt;
     </p>
     <p><label for="phone" class="left">Phone Number (contact)</label>
        &lt;input type="text" name="phone" value="&lt;?=$this-&gt;validation->phone;?&gt;" maxlength="50" class="field" tabindex="1" > *
     </p>
     &lt;input type="submit"&gt;
  &lt;/form&gt;
</div>

Then browse to .../index.php/home/posttome/
you will see a blank form, if you submit a blank form you will see all the error messages appear in the quote1 view.

hope it works for you.
#15

[eluser]ehicks727[/eluser]
SWEEEET!!! Thank you so much for your help and diligence to problem solve this. Your solution worked great. Good catch on the run() method. I totally missed that. You deserve some karma points, or something for this. I really appreciate it.
#16

[eluser]gtech[/eluser]
no problems glad to help, its one of those kick your self answers.




Theme © iAndrew 2016 - Forum software by © MyBB