Form validation rules not successful - php

For some reason when I run the submit function it returns an error that tells me the form did not submit properly but while looking over the docs I was not able to find out how to display what rules were not validated correctly.
{"output_status":"Error","output_title":"Form Not Validated","output_message":"The form did not validate successfully!"}
This is my controller code:
public function form_is_valid()
{
/* Set validation rules for post data */
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|min_length[6]|max_length[12]|regex_match[/[a-z0-9]/]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|min_length[6]|max_length[12]|regex_match[/[a-z0-9]/]');
//$this->form_validation->set_rules('remember', 'Remember Me', 'trim|xss_clean|integer');
/* Form validation passed */
return $this->form_validation->run();
}
public function submit()
{
if (!$this->form_is_valid())
{
$this->output('The form did not validate successfully!', 'Form Not Validated', 'Error');
return;
}
... rest of submit function code...
}
The posted values are:
password testpass
username testuser

I was able to with the comments in this question thread use the function with use of the user guide on the codeigniter website. And find out that I was forgetting the username had to be atleast 6 characters long.

Related

valid_email not working along with callback function in codeigniter

Hello all i was using form_validation Library in codeigniter inside my application. I am making a password retrieving function. I have made a submit form of the email . Now, on the email input field i want to apply these validations.
required
valid_email
check email exist or not.
For the 3rd one i am using call back function to check the database and it worked fine. But with the call back function valid_email is not functioning. This is my controller functions.
public function password_retrieve()
{
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|callback__email_exists');
if ($this->form_validation->run() == false) {
$this->load->view('login_header');
$this->load->view('password_retrieve');
$this->load->view('login_footer');
} else {
}
} else {
$this->load->view('login_header');
$this->load->view('password_retrieve');
$this->load->view('login_footer');
}
}
function _email_exists($email)
{
$this->load->model('users_model');
$result = $result = $this->users_model->check_email_is_exist($email);
if (!$result) {
$this->form_validation->set_message(__FUNCTION__, 'This %s address does not exist!');
return false;
} else {
return true;
}
}
It should checked the valid_email rather than the going to the callback function.
In other mean i want to know what is the order of the rules. Is callback rule runs before the valid_email?
Try to remove "trim" and for check if email exist don't use another function. But use "is_unique[table_name.email]".
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[table_name.email]');
By Searching the official documentation and git repository i have found out that there is not a particular order in which a function will run in codeigniter. This means $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|callback__email_exists');
In this case valid_email will run after the callback__email_exists. There is not a order in which first trim, then required and then valid_email will run.
So what i have done is to make a new function in my callback function which runs after the required but before the check_email.
Answering this question so that in future people can get the benefit from it. Cheers!

Codeigniter Form Validation Callback Issue

I have this code below. My problem is it only triggers the callback. The required and valid_email rules are not called. I'm expecting to execute the rules in order. But when I remove the callback it runs normal. TIA.
$this->form_validation->set_rules('search_word', 'Email address', 'trim|required|valid_email|callback_has_valid_credentials');
$this->form_validation->set_message('has_valid_credentials', '{field} is not registered in the system.');
if ($this->form_validation->run() == FALSE){
$this->load->view('login/reset');
return;
}
You don't have a function named has_valid_credentials defined.
public function has_valid_credentials($str) {
/** Write validation code here **/
}
For your reference, check out the documentation.
https://www.codeigniter.com/userguide3/libraries/form_validation.html#callbacks-your-own-validation-methods

CodeIgniter getting form field value to lowercase

I have just started on learning how to use CodeIgniter and have never use any framework before so I only know a bit of how is the flow. Right now I have 1 issue that I want to set the input username to lowercase and I do not have any idea how to write the function for the convert_lowercase().
Below is my code:
public function signup_validation()
{
$this ->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required|trim|is_unique[userinfo.username]|convert_lowercase');
$this->form_validation->set_rules('password', 'Password', 'required|trim');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'required|trim|matches[password]');
$this->form_validation->set_message('is_unique', 'That Username Already Exists.');
if($this->form_validation->run()){
}else{
$this->load->view('signup');
}
}
public function convert_lowercase()
{
strtolower($this->input->post('username'));
}
I am not sure am I doing the right way.
And is it best to just put the strtolower in the set_rules parameter? Or it is best to put in a function?
And if separate it, how should it be done and how do I get the final username data to insert into database?
Any kind souls out there can help me on this?
Thanks in advance.
You can provide php native functions for form validation to CodeIgniter. Here is how your code should be
public function signup_validation()
{
$this ->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required|trim|is_unique[userinfo.username]|strtolower');
$this->form_validation->set_rules('password', 'Password', 'required|trim');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'required|trim|matches[password]');
$this->form_validation->set_message('is_unique', 'That Username Already Exists.');
if($this->form_validation->run()){
}else{
$this->load->view('signup');
}
}
You should check their documentation on form validation: http://ellislab.com/codeigniter%20/user-guide/libraries/form_validation.html
add callback_ to the rules.
$this->form_validation->set_rules('username', 'Username', 'required|trim|is_unique[userinfo.username]|callback_convert_lowercase');
and the callback function should return some value.
public function convert_lowercase() {
return strtolower($this->input->post('username'));
}
I will try to explain the best I can! Have you set up your database config file correctly? Have you set up the database correctly? Make sure that is all good before you do this..
Here is a bit of what is going on
if($this->form_validation->run()){
//Right here is what happens if the form passes your test!
$this->insert_user();
}else{
$this->load->view('signup');
}
if($this->form_validation->run()) takes the rules you gave it and if it returns "true" it runs in that if statement otherwise it will return you to the signup page
Here is the function that I set an example for
public function insert_user()
{
$data = array(
'username' => strtolower($this->input->post('username')),
'password' => $this->input->post('password'),
);
$this->db->insert('users', $data);
}
I also suggest you look into encrypting your passwords and other CI documentation, it is fantastic
Regarding Codeigniter 4 it is no longer possible to modify data during validation.
They add this information in official documentation :
"You can also use any native PHP functions that return boolean and permit at least one parameter, the field data to validate. The Validation library never alters the data to validate."

HTTP 500 error using CodeIgniter Form Validation callback functions

So I'm trying to use a callback function with the Form_validation library in CodeIgniter (v2.1.4) to check whether a user with a given username or email exists in the database before creating a new user.
login.php (Controller)
function create_member()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|callback_value_check[USERNAME]');
if($this->form_validation->run() != FALSE)
{
// Validation passed; create the new user.
$this->load->model("members_model");
if($query = $this->members_model->create_member())
{
// Load the success page view.
}
else
{
// Reload the signup page view.
}
}
else
{
// Reload the signup page view.
}
}
function _value_check($value, $column)
{
$this->load->model("members_model");
if($this->members_model->check_exist_value($column, $value))
{
$this->form_validation->set_message('value_check', '%s is already taken.');
return FALSE;
}
else
{
return TRUE;
}
}
members_model.php (Model)
function check_exist_value($column, $value)
{
$this->db->where($column, $value);
$result = $this->db->get('MEMBERS');
if($result->num_rows() > 0)
{
// A user with that unique value already exists in the database.
return TRUE;
}
else
{
// There is no user with that unique value in the database.
return FALSE;
}
}
As seen in the code above, I'm only currently testing for an existing username. The standard validation messages appear correctly (i.e. required, min_length, etc). However, if I enter a value that I know to be already in the database (meaning the custom callback validation function should fail) I instead get an HTTP 500 error (Chrome's default 'Server error' page).
Does anyone have any insight as to why I'm getting an HTTP 500 error instead of seeing my custom error message?
I don't know why you are using you callback for the email/username unique check when codeigniter already provides this functionality in the form_validation class just add the unique rule check in the validation rules with the table name and column that has the emails/usernames also provide the table is column
$this->form_validation->set_rules('username', 'Username',
'trim|required|min_length[4]|unique[tablename.column_email_username,tablename.id]');
$this->form_validation->set_rules('username', 'Username',
'trim|required|min_length[4]|is_unique[tablename.column_email_username]');
Hope unique[tablename.column_email_username,tablename.id] does the job and you will not face the server error
OR try this for is_unique
Ahaha, turned out there was a type in the section reloading the view when the validation failed. Just needed to take a break and re-read through what I had to spot it!

CodeIgniter Form Validation - Get the Result as "array" Instead of "string"

I am writing my form validation class using CodeIgniter. Is there any way so that I can get error messages in name value pair? For example, in a sample form there are four fields: user_name, password, password_conf, and timezone. Among them user_name and password validation has failed after executing the following:
$result = $this->form_validation->run();
If the above function returns false, I want to get the errors in name value pairs like the following:
Array
{
'user_name' => 'user name is required',
'password' => 'passord is required'
}
I truly want to form a JSON, which I can pass back to the AJAX call. I have a (dirty) solution: I can call validation methods one by one like the following:
$this->form_validation->required($user_name);
$this->form_validation->required($password);
Is there any other way, to get all the error messages at once in name value pair?
EDIT: I was suggested to do validation with jQuery from one of the answers:
jQuery will help in client side validation, but what about server side, there I am using CodeIgniter validation.
I have designed it so that:
I post all the values using AJAX.
Validate in server side (PHP).
Perform the desired operation if the inputs are valid; else return error to the user.
I have found one way myself by looking into the CodeIgniter code:
I have extended the library CI_Form_validation like: -
class MY_Form_validation extends CI_Form_validation
{
public function getErrorsArray()
{
return $this->_error_array;
}
}
I know, this is a hack, but will serve my need for the time. I hope CodeIginter team soon come up with an interface to access that array.
You can simply use,
$this->form_validation->error_array();
This is from the class reference documentation of CodeIgniter.
The solution I like best doesn't involve adding a function anywhere or extending the validation library:
$validator =& _get_validation_object();
$error_messages = $validator->_error_array;
Reference: http://thesimplesynthesis.com/post/how-to-get-form-validation-errors-as-an-array-in-codeigniter/
You should be able to call this at any point in your code. Also, it's worth noting there is a newer thread that discusses this as well.
You can create an private function in your controller
private function return_form_validation_error($input)
{
$output = array();
foreach ($input as $key => $value)
{
$output[$key] = form_error($key);
}
return $output;
}
and then in your validation method, just call this, here is mine
public function add_cat_form()
{
$this->output->unset_template();
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'required');
if ($this->form_validation->run())
{
if (IS_AJAX)
{
$dataForInsert = $this->input->post();
if ($dataForInsert['parentid'] == -1)
{
unset($dataForInsert['parentid']);
}
$this->post_model->add_cat($dataForInsert);
echo json_encode('success');
} else
{
#in case of not using AJAX, the AJAX const defined
}
} else
{
if (IS_AJAX)
{
#This will be return form_validation error in an array
$output = $this->return_form_validation_error($this->input->post());
echo $output = json_encode($output);
} else
{
#in case of not using AJAX, the AJAX const defined
}
}
}
Seeing as code igniter validation gives you the error messages on a normal page refresh wouldn't it be better to use something like the jquery validation plugin which will validate entirely client side before sending the form? No AJAX necessary that way.
EDIT:
I'm not suggesting NOT DOING server side validation, just that posting with AJAX in order to validate isn't necessary and will reduce server hits if you do it client side. It will gracefully degrade to the regular page refresh with errors on or a success message.
there's already a correct answer but i think this one is more understandable on how to execute the process.
Do it in your Controller
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if($this->form_validation->run() === FALSE){
$data['error'] = validation_errors();
}else{
//success
}
echo json_encode($data);
Do it in your JS
success:function(response){
if(response.error){
$('#notif').html(response.error);
}else{
//do stuff here...
}
}
and finally display the error corresponding to the given id in your js.
Do it in your HTML
<span id="notif"></span>
This might be way belated but I want to share a solution that I used in hope that it will benefit someone else.
Unfortunately, CodeIgniter's $this->form_validation->error_array() method returns only the errors that emanate from the set_rules() method. It does not account for the name of the form field that generated the error. However, we can leverage another of CodeIgniter's method $this->form_validation->error(), which returns the error(s) associated with a particular form field, by passing the name of the field as parameter.
//form validation rules
$this->form_validation->set_rules('f_name', 'First Name', 'trim|required',
['required' => 'First Name is required']
);
$this->form_validation->set_rules('l_name', 'Last Name', 'trim|required',
['required' => 'Last Name is required']
);
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[users.email]',
[
'required' => 'Email is required',
'valid_email' => 'Email format is invalid',
'is_unique' => 'Email already exists'
]
);
$error_arr = []; //array to hold the errors
//array of form field names
$field_names= ['f_name', 'l_name', 'email'];
//Iterate through the form fields to build a field=error associative pair.
foreach ($field_names as $field) {
$error = $this->form_validation->error($field);
//field has error?
if (strlen($error)) $error_arr[$field] = $error;
}
//print_r($error_arr);

Categories