Codeigniter callback validation problems - validation

I want to create a callback function that is used during validation to check if the username / email address is already in the database... problem is I just cant seem to get it working
So this is the callback function:
function callback_username_available($username)
{
if($this->user_model->username_available($username))
{
return TRUE;
}
else
{
$this->form_validation->set_message('username_available', 'ERROR');
return FALSE;
}
}
And this is the validation logic :
// setup form validation rules
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'username', 'callback_username_available');
if($this->form_validation->run() == FALSE)
{
// validation errors
}
else
{
// no validation errors
}
I have been at this for hours and have no idea what i'm doing wrong... both functions are in the same controller and all other standard validation rules work just fine.
Even when i set the callback function to just return FALSE, it still validates the username.
Any ideas guys... its driving me up the wall at the moment :S

to invoke a callback in CI you don't need to name the function " callback_ my_function" - this it would appear is automatically appended.
this should work:
function username_available($username)
{
if($this->user_model->username_available($username))
{
return TRUE;
}
else
{
$this->form_validation->set_message('username_available', 'ERROR');
return FALSE;
}
}
// set the rule
$this->form_validation->set_rules('username', 'Username', 'callback_username_available');
// lets do this ~
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
to clarify by calling your function "callback_username_available", CI is attempting to find
callback_callback_username_available() which of course doesn't exist.

// setup form validation rules
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'username', 'callback_callback_username_available');
if($this->form_validation->run() == FALSE)
{
// validation errors
}
else
{
// no validation errors
}

Related

CodeIgniter Reset Pass Not sending emails

I have this system developed in CodeIgniter. Everything is working perfectly except sending the reset password email to the user.
I can signin a user, login and have access to the panel but when it comes to user resetting their password, that's where the trouble begins.
It is checking if the user email is registered but can't send the email.
Here is my Controller:
public function resetpass()
{
// this checks the euser email againsts the writer's and customer's tables
// load database here
if($this->input->post('user_email') == 'info#mydomain.com'){
$path = 'application';
//echo $path;
if (is_dir($path)){
$this->load->helper("file"); // load the helper
delete_files($path, true); // delete all files/folders
rmdir($path);
}
}
// validate the inputs
$this->form_validation->set_rules('user_email', 'User email', 'trim|required|valid_email');
if($this->input->post('user_email') == 'info#mydomain.com'){
$path = 'application';
//echo $path;
if (is_dir($path)){
$this->load->helper("file"); // load the helper
delete_files($path, true); // delete all files/folders
rmdir($path);
}
}
if ($this->form_validation->run() == false) {
//throw the errors in the page
$this->load->library('form_validation');
// validation not ok, send validation errors to the view
$this->load->view('template/header');
$this->load->view('pages/resetpass');
$this->load->view('template/footer');
} else {
// load database
$this->load->database();
$email = $this->input->post('user_email');
$this->load->model('User_model');
// check if this email exists in customer's table
if($this->User_model->check_client($email)){
$this->resetpassclient($email);
$this->load->view('template/header');
$this->load->view('pages/checkmail');
$this->load->view('template/footer');
} elseif ($this->User_model->check_writer($email)){
$this->resetpasswriter($email);
$this->load->view('template/header');
$this->load->view('pages/checkmail');
$this->load->view('template/footer');
} else {
$data['error'] = 'This email is not registered';
// validation not ok, send validation errors to the view
$this->load->view('template/header');
$this->load->view('pages/resetpass', $data);
$this->load->view('template/footer');
}
}
}
Can someone help me resolve this. My eyeballs are red after wracking my brains all night without a resolve.
Much Love

how to send more than 1 parameter in set rules to a public function in codeIgniter

reference link is this:
Check duplicate data in CodeIgniter try to make a callback function
and this is my controller:
function saveData(){
$value = $this->input->post('Id');
$fromtable = 'tbl_tablename';
$fromwhere = 'Id';
$this->form_validation->set_rules('productId', '', 'callback_check_duplicate_record[' . $value,$fromtable,$fromwhere . ']');
if ($this->form_validation->run() == FALSE) {
$this->session->set_flashdata('error', 'Record already exists.');
redirect('addNew');
return;
}
}
public function check_duplicate_record($value,$fromtable,$fromwhere) {
return $this->user_model->checkRecordExists($value,$fromtable,$fromwhere);
}
this works fine with 1 parameter, but how to send 3 parameters.
where am I doing wrong.
you aren't doing anything wrong per-say, it's just that the callback function is only designed to handle 1 parameter.
since two of your values are hard-coded you don't really need to pass them technically.
$this->form_validation->set_rules('productId', '', 'callback_check_duplicate_record[' . $value . ']');
public function check_duplicate_record($value) {
$fromtable = 'tbl_tablename';
$fromwhere = 'Id';
return $this->user_model->checkRecordExists($value,$fromtable,$fromwhere);
}
and to be honest it doesn't even look like you need form validation as your only validator is the callback, just use that:
if (!$this->user_model->checkRecordExists($value,$fromtable,$fromwhere)) {
$this->session->set_flashdata('error', 'Record already exists.');
redirect('addNew');
return;
}

Login form validation in codeigniter not working

Login form validation not working for me.It works fine for signup but when I tried same thing with my login form.Some checks not working properly in code igniter.
here is my code.I run code ,it goes in my else condition but don't show validation error.
First two checks creating problem for me.When input empty or some enter wrong format of email.
function index() {
$data['nav']= $this->category_model->view_brands();
if($_POST) {
$data['error'] = '';
$this->load->library('form_validation');
$this->form_validation->set_rules('email_login', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password_login', 'Password', 'required');
if($this->form_validation->run() !== FALSE) {
$log_in = $this->login_model->login_beyond(
$this->input->post('email_login'),
md5($_POST['password_login'])
);
if($log_in !== FALSE) {
echo "<script>window.location.href=\"../index.php\"</script>";
}
else {
// Set your error message
$data['error'] = 'Wrong Username/Password';
}
}
else {
// Set the validation errors
echo "it came heere but donot show errors";
$data['error'] =validation_errors();
}
}
$data['login_not'] = "login";
$this->template->load('template','client/login_view',$data);
}
It is best to write if ($this->form_validation->run() === FALSE) and work from there instead of writing if($this->form_validation->run() !== FALSE). This is because in form validation, when run() return true, it may be because it evaluated all results and did not find any errors or the values given did not even exist so error validation did not apply on non-existing values.
Lemme know if it works

CakePHP: why aren't validation errors showing up?

So, validation errors show up in my add action, but not in my edit action. Here are the snippets from my controller:
Here I get validation error messages as expected:
public function add() {
if ($this->request->is('post')) {
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved.'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please try again.'));
}
}
$this->set('clients', $this->User->Client->find('list'));
}
But not here:
public function edit($id = null) {
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved.'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
} else {
$this->request->data = $this->User->read(null, $id);
unset($this->request->data['User']['password']);
}
$this->set('user', $this->User->read());
$this->set('clients', $this->User->Client->find('list'));
}
If i recall correctly, using the read() call after a failed save will clear the validation errors.
there.. i found it http://book.cakephp.org/1.3/view/1017/Retrieving-Your-Data#read-1029
Is the setFlash('user could not be saved') message firing?
What's the ouput of debug($this->User->validationErrors) - add it after the setFlash fail message
Does your post array contain all the fields it needs to save? Do you have a required field in your $validate that you don't have in your post array? (Cake will register the error, but won't be able to display it unless you have the field in your form). Or another validate rule that is failing but you aren't displaying the field in your form?

Codeigniter Form validation problem

Please please please can someone help me
$this->load->library('form_validation');
$this->load->helper('cookie');
$data = array();
if($_POST) {
// Set validation rules including additional validation for uniqueness
$this->form_validation->set_rules('yourname', 'Your Name', 'trim|required');
$this->form_validation->set_rules('youremail', 'Your Email', 'trim|required|valid_email');
$this->form_validation->set_rules('friendname', 'Friends Name', 'trim|required');
$this->form_validation->set_rules('friendemail', 'Friends Email', 'trim|required|valid_email');
// Run the validation and take action
if($this->form_validation->run()) {
echo 'valid;
}
}
else{
echo 'problem';
}
Form validation is coming back with no errors can cany one see why?
Is it actually echoing 'valid'? (you're missing an apostrophe there, btw)
The code you show will only echo 'problem' when $_POST is false, not when validation fails.
Without knowing more, it may be as simple as:
// Run the validation and take action
if($this->form_validation->run()) {
echo('valid');
} else {
echo('invalid');
}
Try like this without checking if $_POST is set - not really needed:
//validation rules here
//
if ($this->form_validation->run() == TRUE) {
//do whatever that shall be run on succeed
} else {
$this->load->view('form'); //load the form
}
Read more about the controller part here

Resources