CodeIgniter Forums
Modular Extensions - Version 4.3 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Modular Extensions - Version 4.3 (/showthread.php?tid=6550)



Modular Extensions - Version 4.3 - El Forum - 03-20-2008

[eluser]dfau[/eluser]
I have this controller (non ME):
Code:
<?php
class User extends Controller {
    function
    User() {
        parent::Controller();
    }

    function
    login() {
        $this->load->library('validation');

        $rules['email'] = 'required|trim|valid_email';
        $this->validation->set_rules($rules);

        $fields['email'] = 'Email';
        $this->validation->set_fields($fields);

        if ($this->validation->run()) {
            $this->load->view('loggedin');
        }

        $this->load->view('login');
    }
}
?>
and this view
Code:
<h2>Validation errors</h2>
<p>&lt;?=$this->validation->error_string?&gt;</p>

&lt;?=form_open('user/login')?&gt;
<table>
    <tr>
        <th>Email</th>
        <td>&lt;?=form_input(array('name' => 'email', 'size' => 16))?&gt;</td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td>&lt;?=form_submit('submit', 'Login')?&gt;</td>
    </tr>
</table>
&lt;?=form_close()?&gt;
which works fine. When place the same code in as a module (and inherit from Module rather than Controller) I get this under 4.0.19:
Quote:Undefined property: Loader::$validation
Under 4.0.21 $this->validation appears "clean" ie. it doesn't hold over (say) $this->validation->error_string.

Any ideas?


Modular Extensions - Version 4.3 - El Forum - 03-20-2008

[eluser]gerben[/eluser]
Wiredezignz, that did the trick!

Thanx a lot!


Modular Extensions - Version 4.3 - El Forum - 03-20-2008

[eluser]wiredesignz[/eluser]
@dfau, 4.0.19 was not assigning libraries to the module correctly. Which is why we continue to develop ME.

You should not really call libraries directly from a view, because it makes the view dependent on the library.

I recommend passing a reference to the object into the view, or extracting the $data from the library first.
Code:
$this->load->view('login', array('errors' => $this->validation->error_string))

In any case I will try to duplicate your code to see if there is an issue.


Modular Extensions - Version 4.3 - El Forum - 03-20-2008

[eluser]wiredesignz[/eluser]
[quote author="gerben" date="1206081372"]Wiredezignz, that did the trick!
Thanx a lot![/quote]

You're welcome gerben, I will update the wiki to Version 4.0.22 soon.


Modular Extensions - Version 4.3 - El Forum - 03-20-2008

[eluser]wiredesignz[/eluser]
@dfau, this works fine for me. v4.0.21
Code:
//module
    function index()
    {        
        $this->load->library('validation');
        $this->validation->error_string = 'test';

        $this->load->view('login');
    }

//login view
&lt;?php echo $this->validation->error_string ?&gt;



Modular Extensions - Version 4.3 - El Forum - 03-20-2008

[eluser]wiredesignz[/eluser]
Version 4.0.22 is available on the wiki.
Updated modules file loader. Thanks gerben Wink


Modular Extensions - Version 4.3 - El Forum - 03-20-2008

[eluser]marios[/eluser]
[quote author="wiredesignz" date="1206082883"]@dfau, this works fine for me. v4.0.21
Code:
//module
    function index()
    {        
        $this->load->library('validation');
        $this->validation->error_string = 'test';

        $this->load->view('login');
    }

//login view
&lt;?php echo $this->validation->error_string ?&gt;
[/quote]

I can't get that to work that way. I am using version 4.0.22
the only way i get no error is if i use the $_me reference like this :
$this->_me->validation->error_string in the view.
here is my code sample:

for the module:
Code:
&lt;?php
class Submit extends Module
{

    function Submit()
    {
        parent::Module();
        $this->load->library('validation');
        
        $field_names['fieldone'] = "FieldOne";
        
        $this->validation->set_fields($field_names);
        $this->load->helper('date');
    }

    function index($data)
    {
        
        $data = array();
        return $this->load->module->view('submit_view', $data, true);
    }


    function add($data)
    {

        $field_rules['fieldone']  = 'trim|required|max_length[300]|xss_clean';
        $this->validation->set_rules($field_rules);

        $this->validation->set_error_delimiters('<div class="error"><span>', '</span></div>');

        if (!$this->validation->run())
        {
            $data = array();
            return $this->load->module->view('submit_view', $data, true);

        }
        else
        {
            //do something
        }
    }
    
}
?&gt;

and for the view:

Code:
<div id="submit">
&lt;?=form_open("main/add")?&gt;

<p>
    <label for="fieldone">FielldOne:</label>
    &lt;?=form_textarea(array('name'=>'fieldone', 'id'=>'fieldone', 'value'=>$this->_me->validation->fieldone, 'rows'=>10, 'cols'=>60, 'class'=>'imax' ))?&gt;
    &lt;?=$this->_me->validation->fieldone_error; ?&gt;
</p>

&lt;?=form_submit('submit', 'submit');?&gt;

&lt;?=form_close()?&gt;



Modular Extensions - Version 4.3 - El Forum - 03-20-2008

[eluser]dfau[/eluser]
Hi wiredesignz,

I'm still having trouble getting system libraries to work properly from view. The validation library is good to use from the view as it saves a lot of work in the controller for setting up error responses.
I've not been using CI for long and am learning as I go. In your library loading function you load the system libraries around line 238:
Code:
if (is_file(BASEPATH.'libraries/'.$library.EXT) OR is_file(APPPATH.'libraries/'.$library.EXT))
        {
            parent::library($_library);

            $my = $this->_me->config->item('subclass_prefix');

            $library = (class_exists($my.$library)) ? $my.$library : 'CI_'.$library;

            $this->_update_ci_classes();
        }

and then call the constructor for the same library and assign to _me around line 345:
Code:
$this->_me->$_library =& new $library($params);
This causes (in my example) the validation library to be loaded twice, and bound in two different spots; one in $this->_me and the other in global $ci. These aren't referenced, but are two separate instances. When I manipulate $this->validation in my module, I'm not manipulating $ci->validation, which is the one used in the view.

As a hack, if I replace the second constructor call with:
Code:
$ci =& get_instance();
$this->_me->$_library =& $ci->$_library;
$this->validation and $ci->validation are now pointing to the same thing. I think.

Could you please have a double check to see exactly what the behaviour should be? I'm happy to do any testing required.

Many thanks,
dfau


Modular Extensions - Version 4.3 - El Forum - 03-20-2008

[eluser]wiredesignz[/eluser]
Version 4.0.23 is available on the wiki
Corrected an issue with the CI library loader, Thanks marios and dfau. Wink

There is an issue with the download version of CI where load_class() causes an error due to the CI loader using `include` instead of `include_once`, I have had to adapt my code to compensate for this. Thus there were two instances of CI libraries being loaded as you mention. I have corrected this in 4.0.23.

Derek Allard has kindly altered CI recently to fix this, so the latest SVN version library loader now uses include_once.

Please note: $this->load->module->anything() is deprecated, just use $this->load->anything(). Thanks


Modular Extensions - Version 4.3 - El Forum - 03-20-2008

[eluser]dfau[/eluser]
Thanks wiredesignz, will give it a play.

EDIT: All good wiredesignz, thanks again for your time.