Welcome Guest, Not a member yet? Register   Sign In
Validation ...
#1

[eluser]vclef[/eluser]
Hi,

I'm trying to do some validation and I can validation form inputs alright. However, if there's a database error, such as during an insert, I would like to catch that error and display the error message myself rather than seeing the default error generated by CI.

Also, if I were to live with the default error message, is there away I could tag other things on top of it?

Could someone please give me some pointer? Thanks.

Henry
#2

[eluser]vclef[/eluser]
Bump .... any taker? Smile
#3

[eluser]ELRafael[/eluser]
let'me understand you...

you need to catch this kind of error?

Code:
INSERT INTO table (field1, field2, field3) VALUES ('value1', 'value2');

SGDB errors?
#4

[eluser]vclef[/eluser]
More like:
Code:
$this->db->insert();

If there's an error with an insert, CI spits out errors. I'd like to do something like:
Code:
if ($this->db->insert(....)){
    echo "My error";
}
#5

[eluser]ELRafael[/eluser]
In the user_guide, there's a section, error handling.

If you'll use active record, see /application/error/error_db.php

is a pointer, hum? ;-P

i never used "try.. catch" in codeigniter...
#6

[eluser]vclef[/eluser]
Thanks. Looking into it it.
#7

[eluser][email protected][/eluser]
I am looking for this solution also, I have looked into the error handling, however, I can't seem to intercept the CI error handling for example

insert ('name') values ('hello', 'yo');

throws an error, and I would like to catch this and
just display 'error' (I would like this functionality for ajax, instead of returning a huge page to have to parse for an error message.)
#8

[eluser]llbbl[/eluser]
http://ellislab.com/codeigniter/user-gui...ation.html

Read everything on that page throughly. Try the examples. It helps. Wink


If you want set your own error message other than the default one use this:

Code:
$this->validation->set_message('required', 'Your custom message here');

That will change the error message for any field that is checking for the rule "required".

Quote:required No Returns FALSE if the form element is empty.


................................


Ok so if you want to check for something else like if the field exists already in the database this is one way to do it.

in a controller:
- do the built in validation
- if no errors w/ built in validation, call a function in some library
- if field exists set $error = 'username' then return error
- if field doesnt exist insert or whatever set $error = 'success' then return error
- check value of error
- if error = username

Code:
// basic controller junk
// some function stuff ---> basically what you see on the user guide

if ($error == "username"){
$rules['username']  = "callback_username_check";
$this->validation->set_rules($rules);
$this->validation->run();
$this->load->view('myform');
}

//////////////
// if statement and other code for if $error == "success"
//////////////

} // end function

function username_check($str){
  $this->validation->set_message('username_check', 'username taken dumbass');
  return FALSE;    
}
#9

[eluser]llbbl[/eluser]
[quote author="[email protected]" date="1191489679"]I am looking for this solution also, I have looked into the error handling, however, I can't seem to intercept the CI error handling for example

insert ('name') values ('hello', 'yo');

throws an error, and I would like to catch this and
just display 'error' (I would like this functionality for ajax, instead of returning a huge page to have to parse for an error message.)[/quote]

this is best way IMO to do insert statements.

Code:
$this->db->query("INSERT INTO `databasename` SET field1='something', field2='something', field3='something'");

of course you need to load or autoload the CI database library for this to work. if its still causing you problems, it will be a mysql database configuration issue. i suggest echoing the query if it isn't already, then copy paste to phpmyadmin .. till you get your query right... the errors there usually will give you a better hint has to what you are doing wrong database wise.




Theme © iAndrew 2016 - Forum software by © MyBB