[eluser]RobertSall[/eluser]
Hi!
I'm using this code to create widgets, but I came to a problem. In a widget I'm trying to use the form_validation library. When I just use it the class dosen't return any errors even tho I leave the fields empty. But when I've tryed to extend the Wdiget form to the Controller class it worked with the form_validation, but then the classes that should be autoloaded didn't load so I couldn't use a few objects (session in this example).
Here's some code:
Code: class Login extends Widget
{
function run()
{
$this->load('form_validation');
$this->load->database();
#$this->load('session');
#$this->session->set_userdata('test');
$this->form_validation->set_rules('username','Användarnamn','required|callback__check_user_pw');
$this->form_validation->set_rules('password','Lösenord','required');
$this->form_validation->set_message('required','Du måste fylla i %s');
if ($this->form_validation->run() == FALSE)
{
echo validation_errors();
echo "incorrect";
}
else
{
echo "correkt";
}
}
}
Note this isn't the real code, this just shows that the validation_errors() function, the word "incorrect" shows on the screen, but no validation errors.
[eluser]wiredesignz[/eluser]
Loading the Form_validation library in the widget makes it invisible to CI, hence the validation_errors() helper does not work correctly.
Try using the Form_validation library methods which is what the helper does anyway.
Code: echo $this->form_validation->error_string();
[eluser]RobertSall[/eluser]
I see, I worked it out with the function you said. Thank you  .
[eluser]Unknown[/eluser]
My first post, and I am not new..just busy, so I just wanted to say, thank you, thank you very much, I love widgets, and this is just perfect for me...seems to work just fine, a nice, tidy little spot for widgets, that works just fine..
I am a big fan of CI, I am running the latest backendpro and I modified the whole thing to use the ajaxify library thing, http://maxblog.me/ajaxify , and it all is working so nicely..adding widgets is a perfect end to a long day..
Keep up the good work!
[eluser]Unknown[/eluser]
A neat solution for a common issue, Thanks.
BTW, how can I load a model from the widget. Is it possible?
[eluser]Unknown[/eluser]
Thanks wiredesignz for sharing this code. I have done something similar with a lesser compact/elegant syntax to invoke them. In these approaches, I am not too happy w/ the following:
1) typically in a MVC world, the controller executes the biz logic and invokes the template to render. In thus case the controller renders the template, then the template invokes the widgets which in turn calls the associated its biz logic. The interaction is somehow doesn't feel natural to me
2) passing parameters into the widgets is not very practical. The controllers needs to pass the params into the template and pass back to the widget.
BTW, this may be obvious, but I didn't understand as what would trigger the call to the render() method of the widget in your case?
Regards
[eluser]wiredesignz[/eluser]
Widgets display view partials only. They are not designed to process "biz" logic. Hence parameters are pushed to widgets from the view.
@victorlenus, Load the model from it's page controller and pass it to the widget via the view.
[eluser]Mikle[/eluser]
wiredesignz, can you convert your widgets code to php 5?
[eluser]wiredesignz[/eluser]
Widget plugin PHP5 only
Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Widget Plugin
*
* Install this file as application/plugins/widget_pi.php
*
* @version: 0.2
* $copyright Copyright (c) Wiredesignz 2009-08-24
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
class Widget
{
function run($name) {
$args = func_get_args();
require_once APPPATH.'widgets/'.$name.EXT;
$name = ucfirst($name);
$widget =& new $name();
return call_user_func_array(array(&$widget, 'run'), array_slice($args, 1));
}
function render($view, $data = array()) {
extract($data);
include APPPATH.'widgets/views/'.$view.EXT;
}
function load($object) {
$this->$object =& load_class(ucfirst($object));
}
function __get($var) {
static $ci;
isset($ci) OR $ci = get_instance();
return $ci->$var;
}
}
[eluser]Mikle[/eluser]
[quote author="wiredesignz" date="1250933551"]Widget plugin PHP5 only
Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Widget Plugin
*
* Install this file as application/plugins/widget_pi.php
*
* @version: 0.2
* $copyright Copyright (c) Wiredesignz 2009-08-24
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
class Widget
{
function run($name) {
$args = func_get_args();
require_once APPPATH.'widgets/'.$name.EXT;
$name = ucfirst($name);
$widget =& new $name();
return call_user_func_array(array(&$widget, 'run'), array_slice($args, 1));
}
function render($view, $data = array()) {
extract($data);
include APPPATH.'widgets/views/'.$view.EXT;
}
function load($object) {
$this->$object =& load_class(ucfirst($object));
}
function __get($var) {
static $ci;
isset($ci) OR $ci = get_instance();
return $ci->$var;
}
}
[/quote]
Thank you for php 5 ver!
Functions of widget class must be public or private?
|