⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

problem with set_rule callback function



briggers
  • profile picture
  • Member

Posted 19 August 2013 - 08:23 AM

Hi,

 

I cannot get this to work, have I misunderstood something?

 

Here is my controller method:

public function test_list()
	{
		$crud = new grocery_CRUD();
		$crud->set_subject('Test');
		$crud->set_table('tests');
		
		$crud->fields('test1','test2');
		
		$crud->set_rules('test2','Test 2','callback_test_check');
		
		$output = $crud->render();

		$this->_test_output($output);
	}
	
	function _test_check($post_array)
	{
		if ($post_array['test2'] == 'test')
		{
			$this->form_validation->set_message('Test 2', 'Test 2 must not be test');
			return false;
		}
		else
		{
			return true;
		}
	}
	
	function _test_output($output = null)
	{
		$this->load->view('admin/admin_template.php',$output);    
	}

I have tried naming the callback function _test_check($post_array) and callback_test_check($post_array) and test_check($post_array)

 

but none work

and if i start the function with

	function _test_check($post_array)
	{
		var_dump($post_array);
		die;
		if....

the var_dump is ignored so I assume it's not calling the test_check function at all

 

I've read both the GC and the CI validation docs and I cannot see where I have gone wrong

Can anyone see what I have missed

 

Thanks

Richard


Amit Shah
  • profile picture
  • Member

Posted 20 August 2013 - 10:19 AM

Well if you see the Codeigniter validation link  you will notice your mistake.. your callback function should be named - test_check  and not _test_check


briggers
  • profile picture
  • Member

Posted 20 August 2013 - 11:33 AM

I have tried it with that

The code at the moment reads

.
.
$crud->fields('test1','test2');
		
		$crud->set_rules('test2','callback_test_check');
		
		$output = $crud->render();

		$this->_test_output($output);
	}
	
	function test_check($str)
	{
		var_dump($str);
		die;
		if ($str == 'test')
		{
			$this->form_validation->set_message('Test 2', 'Test 2 must not be test');
			return false;
		}
		else
		{
			return true;
		}
	}
.
.

And it still fails to call the callback function.


Amit Shah
  • profile picture
  • Member

Posted 20 August 2013 - 14:20 PM

well what happens. . how you say it fails .. where do you call for it / check for it? check on the firebug for the post_insert_validation result. . if it shows the dump of the str ... it means it is being called but since its not returning false .. it probably may consider it a correct call and take it through.


briggers
  • profile picture
  • Member

Posted 20 August 2013 - 15:42 PM

Hi Amit Shah,

 

Thanks for taking the time to consider this.

 

Firebug shows that the POST request shows that the field contains the word test

 

The database is update with the field even though the callback should return false if the field = 'test'

 

The page goes back to the list showing the update and a success message.

 

This is what firebug says:

 

Partsmultipart/form-data
test1 ghjkghjk test2 test
Source
-----------------------------16723893271491002339521612960 Content-Disposition: form-data; name="test1" ghjkghjk -----------------------------16723893271491002339521612960 Content-Disposition: form-data; name="test2" test -----------------------------16723893271491002339521612960--

 

with response

 

<textarea>{"success":true}</textarea>

 

and

<textarea>{"success":true,"insert_primary_key":true,"success_message":"<p>Your data has been successfully updated. <a href='http:\/\....../bic\/admin\/tests'>Go back to list<\/a><\/p>","success_list_url":"http:\/\....../bic\/admin\/tests\/success\/2"}</textarea>

 

 

That all looks correct as far as the client side goes, but...

 

Actually in the function it should dump something and stop at the die and not complete the update. That's what makes me think that it is not calling the callback function at all, but just update/inserts into the database.

 

There are no entries in the httpd error_log

and the httpd access_log shows everything as a success

 

192.168.20.6 - - [20/Aug/2013:16:19:12 +0100] "GET /90_developments/bic/admin/tests/edit/2 HTTP/1.1" 200 4895 "http:/.../90_developments/bic/admin/tests/success/2" "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:23.0) Gecko/20100101 Firefox/23.0"

192.168.20.6 - - [20/Aug/2013:16:19:27 +0100] "POST /90_developments/bic/admin/tests/update_validation/2 HTTP/1.1" 200 37 "http://.../90_developments/bic/admin/tests/edit/2" "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:23.0) Gecko/20100101 Firefox/23.0"
192.168.20.6 - - [20/Aug/2013:16:19:27 +0100] "POST /90_developments/bic/admin/tests/update/2 HTTP/1.1" 200 318 "http://.../90_developments/bic/admin/tests/edit/2" "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:23.0) Gecko/20100101 Firefox/23.0"
192.168.20.6 - - [20/Aug/2013:16:19:27 +0100] "GET /90_developments/bic/admin/tests/success/2 HTTP/1.1" 200 12361 "http://.../90_developments/bic/admin/tests/edit/2" "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:23.0) Gecko/20100101 Firefox/23.0"

 

 

This sort of issue is usually a syntax error or the like but I cannot see anything like that. I think I have followed the docs to the letter

 

This is the complete controller as tested above

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Tests extends CI_Controller {

	function __construct()
	{
		parent::__construct();

		/* Standard Libraries of codeigniter are required */
		$this->load->database();
		$this->load->helper('url');
		$this->load->helper('text');
		$this->load->helper('inflector');
		$this->load->library('form_validation');
		/* ------------------ */ 

		$this->load->library('grocery_CRUD');

	}

	public function test_list()
	{
		$crud = new grocery_CRUD();
		$crud->set_subject('Test');
		$crud->set_table('tests');
		
		$crud->fields('test1','test2');
		
		$crud->set_rules('test2','callback_test_check');
		
		$output = $crud->render();

		$this->_test_output($output);
	}
	
	function test_check($str)
	{
		var_dump($str);
		die;
		if ($str == 'test')
		{
			$this->form_validation->set_message('Test 2', 'Test 2 must not be test');
			return false;
		}
		else
		{
			return true;
		}
	}
	
	function _test_output($output = null)
	{
		$this->load->view('admin/admin_template.php',$output);    
	}
}

/* End of file tests.php */
/* Location: ./application/controllers/admin/tests.php */

The database table is just this:

 

mysql> desc tests;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| test1 | varchar(16) | NO   |     | NULL    |                |
| test2 | varchar(16) | NO   |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
 

It's all very simple but I don't know what to try next


Amit Shah
  • profile picture
  • Member

Posted 21 August 2013 - 08:14 AM

Well.. sorry .. even i missed out but if you notice the function call...

void set_rules(mixed $field [, string $label [, string $rules] ])

You need to specify the label too .. and then the rule. Else .. when u pass the second param it considers it as a label only and not the actual rule..

so you can make it like

$crud->set_rules('test2', 'Test 2', 'callback_test_check');

and that should work!!


davidoster
  • profile picture
  • Member

Posted 21 August 2013 - 10:55 AM

Also if we need to follow to the letter the documentation, the callback test_check should be defined as a public function.


briggers
  • profile picture
  • Member

Posted 21 August 2013 - 15:01 PM

Ahaa,

 

Thanks guys.

Actually I now realised a couple of other points.

I changed the rule to be:

$crud->set_rules('test2','Test2','callback_test_check');

and

function test_check($str)

then discovered/realised that the set_message line refers to the rule and not the field, so

$this->form_validation->set_message('test_check', 'Test2 must not be test');

works and gives error message, but

$this->form_validation->set_message('test2', 'Test2 must not be test');

gives the error message

 

Unable to access an error message corresponding to your field name.

 

 

Also you cannot follow the normal CI thing of calling methods  function _some_method(){...

with the "_" in front of the name to make it internal. It does not find the rule then at all if you do.

 

So I hope this helps someone else.

 

Now all I have to do is to work out how to do a validity check that one field is bigger than another, like end_date is later than start_date  :angry: