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

[eluser]ehicks727[/eluser]
My form in a separate view that I need to display the error messages is 'rendered', I guess you would say.

Code:
$this->load->view('quote1', [what do I put here?], true);

If I just echo the error messages in the controller, they display fine, but they don't seem to get passed to the rendered view.

What I'm doing is stuffing the rendered view into an array variable that I'm passing to the container view... it's my answer to blocks.

Anyway, here's some code... any help is greatly appreciated. This is my first attempt at validation, so I'd love to get this working.

Code:
$this->load->helper('url', 'form');
                $this->load->library('validation');

                $rules['name']    = "required";
        $rules['phone']    = "required";
        $rules['email']    = "required|valid_email";
        $rules['taxid']    = "required|exact_length[9]|numeric";    
        $rules['dba']    = "required";
        $rules['biztype'] = "required";
        $rules['address1']    = "required";
        $rules['city']    = "required";
        $rules['st']    = "required";
        $rules['zipcode']    = "required";
        $rules['yrs']    = "required";
        $rules['description']    = "required";
        $rules['privacy']    = "required";
        
        $this->validation->set_rules($rules);
        
        $fields['name']    = 'Name';
        $fields['phone'] = 'Phone';
        $fields['email'] = 'Email';
        $fields['taxid'] = 'TaxID/EIN';
        $fields['dba'] = 'DBA/Company Name';
        $fields['biztype'] = 'Busines Type';
        $fields['contlic'] = 'Contractor\'s License';
        $fields['address1'] = 'Address';
        $fields['address2'] = 'Suite/Bld.';
        $fields['city'] = 'City';
        $fields['st'] = 'State';
        $fields['zipcode'] = 'Zipcode';
        $fields['website'] = 'Website';
        $fields['yrs'] = 'Years in operation';
        $fields['description'] = 'Description of Business';
        $fields['privacy'] = 'Privacy';
    
        $this->validation->set_fields($fields);

                $this->content->addColumn1Unit($this->load->view('quote1', $this->fields, true));
        $data['content'] = $this->content->generate();
        
        if ($this->validation->run() == FALSE) {
            $this->load->view('layout1', $data);
        } else {
            redirect('/quote/step2', 'location');
        }

Here's some bits from my view

Code:
<div class="form1">
    &lt;form action="/quote" 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>   <snip...>
#2

[eluser]gtech[/eluser]
quoted response below
#3

[eluser]ehicks727[/eluser]
[quote author="gtech" date="1210694414"]I am reading in between the lines some what...

you should not need to pass anything to the view, as $this will be available in the view as its loaded within the controller class.
Code:
$this->content->addColumn1Unit($this->load->view('quote1', array(), true));

so in the quote1 view you should be able to access
Code:
&lt;?=$this->validation->name_error?&gt;
&lt;?=$this->fields['name']?&gt;
[/quote]

That's what I thought too! But it's not working when you 'render' the view into a variable that gets passed to another array... well, at least it's not working for me.

I can call this from the first view that I'm rendering and get nothing
Code:
&lt;?php echo $this->validation->error_string; ?&gt;

However, I can echo that from the controller and it works fine, and I can echo it from the 'final' view and it works fine too.

I'm guessing there's something about the function below that loses scope or something.

Code:
$this->load->view('quote1', $data, true));

Anyone have any ideas? Thanks.
#4

[eluser]gtech[/eluser]
Code:
&lt;?php
class Home extends Controller   {
  function Home(){
    parent::Controller();
  }

  function index() {
    $this->test = 'tree';
    $sss = $this->load->view('testview',array(),true);
    echo $sss;
  
  }
}
?&gt;
The view just echos $this->test; and works fine. so thats not the problem.. you could just echo the result from $this->load->view('quote1',array(),true) in your code to see if that works, before passing it to your content library/helper.

what happens in the 'content' library/helper so I can mimic your code here?

you could try
Code:
$contentdata = $this->load->view('quote1', $this->fields, true)
echo $contentdata;
$this->content->addColumn1Unit($contentdata));
#5

[eluser]ehicks727[/eluser]
[quote author="gtech" date="1210712124"]
The view just echos $this->test; and works fine. so thats not the problem.. you could just echo the result from $this->load->view('quote1',array(),true) in your code to see if that works, before passing it to your content library/helper.

what happens in the 'content' library/helper so I can mimic your code here?[/quote]

It just stuffs into the div structure I want and returns html. Thanks for helping!!

Code:
&lt;?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Content {
    
    private $html = '';

    function Content() {
    }
    
    function addPageTitle($html) {
        $this->html .= '<h1 class="pagetitle">'.$html.'</h1>';
    }
    
    function addBlockTitle($html) {
        $this->html .= '<h1 class="block">'.$html.'</h1>';
    }
    
    function addBlockPTitle($html) {
        $this->html .= '<p align="center" class="block">'.$html.'</p>';
    }
    
    function addHR() {
        $this->html .= '<hr class="clear-contentunit" />';
    }
    
    function addClear() {
        $this->html .= '<p id="after">&nbsp;</p>';
    }
    
    function addColumn1Unit($html) {
        if (is_string($html)) {
            $this->html .= '<div class="column1-unit">'.$html.'</div>';
        } else if (is_array($html)) {
            
        } else {
            show_error('unknown data type');
            die;
        }
    }
    
    function addColumn2Unit($html) {
        if (is_string($html)) {
            show_error('String found, requires array');
            die;
        } else if (is_array($html)) {
            $this->html .= '<div class="column2-unit-left">'.$html[0].'</div><div class="column2-unit-right">'.$html[1].'</div>';
        } else {
            echo 'unknown data type';
            die;
        }
    }
    
    function addColumn2UnitOffset($html) {
        if (is_string($html)) {
            show_error('String found, requires array');
            die;
        } else if (is_array($html)) {
            $this->html .= '<div class="column2-unit-left-66">'.$html[0].'</div><div class="column2-unit-right-33">'.$html[1].'</div>';
        } else {
            echo 'unknown data type';
            die;
        }
    }
    
    function addColumn3Unit($html) {
        if (is_string($html)) {
            show_error('String found, requires array');
            die;
        } else if (is_array($html)) {
            $this->html .= '<div class="column3-unit-left">'.$html[0].'</div><div class="column3-unit-middle">'.$html[1].'</div><div class="column3-unit-right">'.$html[2].'</div>';
        } else {
            echo 'unknown data type';
            die;
        }
    }
    
    function generate() {
        return $this->html;
    }
}

?&gt;
#6

[eluser]gtech[/eluser]
home.php (controller)
Code:
&lt;?php
class Home extends Controller   {
  function Home(){
    parent::Controller();
  }

  function index() {
    $this->load->library('content');
    $this->test = 'tree';
    $this->content->addColumn1Unit($this->load->view('testview', array(), true));
    echo $this->content->generate();
  }
}
?&gt;
testview.php (view)
Code:
&lt;?=$this->test?&gt;
RESULT:
tree
#7

[eluser]ehicks727[/eluser]
Let me try again... sometimes you stare at this stuff so long, you lose sight of what you're doing. I appreciate you helping me troubleshoot this, btw.
#8

[eluser]gtech[/eluser]
you are saying
&lt;?php echo $this->validation->error_string; ?&gt;
is blank.. so maybee the first time you run the form the error_string is blank
does
[strike]
&lt;?php echo $this->fields['name']; ?&gt;
print anything?
[/strike]
if you put a hr in the view are you seeing that, at least then you will know its actually getting to the view.
#9

[eluser]ehicks727[/eluser]
[quote author="gtech" date="1210713349"]you are saying
&lt;?php echo $this->validation->error_string; ?&gt;
is blank.. so maybee the first time you run the form the error_string is blank
does
&lt;?php echo $this->fields['name']; ?&gt;
print anything?[/quote]

I get an error

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: CI_Loader::$fields

Filename: views/quote1.php

Line Number: 4
#10

[eluser]ehicks727[/eluser]
What should I put in this line??? Anything?

Code:
$this->content->addColumn1Unit($this->load->view('quote1', null, true));

I'm specifically wondering if I should put anything where NULL is. you're saying that $this->[whatever] should work, regardless of what I pass to the view, right?




Theme © iAndrew 2016 - Forum software by © MyBB