Form validation code igniter - php

I've made a utility, now when a user applies for forget password, he's shown one text box, where he can enter his email address or password, how to validate email address, because i've i apply valid_email, it'll reject password OR i should show two fields and user has to enter either one, but how to validate that he must enter one of the fields?

function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('email', 'Email', 'valid_email');
$this->form_validation->set_rules('email', 'Email', 'callback_email_check');
if ($this->form_validation->run() == FALSE)
{
//fail
}
else
{
//success
}
}
function email_check($str)
{
if (stristr($str,'#uni-email-1.com') !== false) return true;
if (stristr($str,'#uni-email-2.com') !== false) return true;
if (stristr($str,'#uni-email-3.com') !== false) return true;
$this->form_validation->set_message('email', 'Please provide an acceptable email address.');
return FALSE;
}
Try this. or for more details check:
http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html

Related

When form validation fails how to redirect to different function based on inputs

I have a input page with 3 inputs username, password, OTP. When password is wrong i want to send it 1 function and if OTP fails i want to send it other function. How can i achieve this. Below is a sample code.
public function verifyLogin() {
$data['BASE_URL'] = base_url();
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
$this->form_validation->set_rules('otp', 'OTP', 'trim|xss_clean|callback_check_otpNum');
if ($this->form_validation->run() == false) {
if(//passwordFails)
$this->index();
else if(//otpfails)
$this->loginPage();
}
}
Try using $this->form_validation->error_array() to get all failed validations and then equating accordingly

Codeigniter form validate for optional fileds

I have form fields email and phone.Out of these any one need to be filled.
$this->form_validation->set_rules('phone[]', 'Phone', 'trim|required|xss_clean');
$this->form_validation->set_rules('email[]', 'Email', 'trim|required|xss_clean');
How can I do this?
You need to call a callbabk function
$this->form_validation->set_rules('phone[]', 'Phone', 'trim|xss_clean');
$this->form_validation->set_rules('email[]', 'Email', 'trim|xss_clean|callback_check_validation');// call a calback
In your callback you have to check empty fields for email and phone.
function check_validation() {
$phone=$this->input->post('phone');
$email=$this->input->post('email');
if(empty($phone) && empty($email)){// check empty of both filed
$this->form_validation->set_message("check_validation", 'Atleast phone or email required');
return FALSE;
}else {
return TRUE;
}
}

Display flashdata message in codeigniter callback function

I've this registration function and the related callback to check if an username already exists in the database. It works all perfectly, but I have problems with flashdata message
Here's the code:
/*
* Checks registration
*/
public function verify(){
$this->form_validation->set_rules('nome', 'Nome', 'trim|required|min_length[2]|xss_clean');
$this->form_validation->set_rules('cognome', 'Cognome', 'trim|required|min_length[2]|xss_clean');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|xss_clean|callback_check_username');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('password_conf', 'Password Confirmation', 'trim|required|matches[password]');
if($this->form_validation->run() == FALSE)
{
$this->session->set_flashdata('error_reg', 'Something goes wrong, please check your registration form');
redirect('registration');
}
else
{
$this->registration_m->add_user();
$this->session->set_flashdata('success_reg', 'Registration Successful!');
redirect('registration');
}
}
public function check_username($username){
$result = $this->registration_m->check_username_m($username);
if ($result) {
$this->session->set_flashdata('username', 'Username già in uso, riprovare.');
return false;
} else {
return true;
}
}
As you can see there's a redirect on the function verify(), and the flashdata before is correctly shown in case of errors.
My Question is: is there a way to show (in case of error) the flashdata inside the callback function without changing the logic of this piece of code?
Hope I was clear at all, cheers.
Modify your code as below
public function check_username($username){
$result = $this->registration_m->check_username_m($username);
if ($result) {
$this->form_validation->set_message('check_username', 'The %s field can not be the empty');
return false;
} else {
return true;
}
}
See differences in code
$this->session->set_flashdata('username', 'Username già in uso, riprovare.');
$this->form_validation->set_message('check_username', 'The %s field can not be the empty');

Why isn't CodeIgniter's form validation not validating anything?

Because of the bug in AngularJS, I need more than ever to make CodeIgniter validate my registration form.
I have done everything I can think of to troubleshoot this problem. Maybe it just needs a different set of eyes? Or maybe someone has dealt with this before?
public function register_validation() {
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username',
'required|trim|is_unique[users.username]|max_length[32]');
$this->form_validation->set_rules('password', 'Password',
'required|trim|min_length[6]');
$this->form_validation->set_rules('email', 'Email',
'required|trim|valid_email|is_unique[users.email]');
if($this->form_validation->run()) {
echo "Pass!";
} else {
echo "NOPE!";
$this->load->view('home');
}
}
This line:
$this->form_validation->set_rules('username', 'Password',
'required|trim|min_length[6]');
Should be:
$this->form_validation->set_rules('password', 'Password',
'required|trim|min_length[6]');
You used username twice. You should change your run() check to:
if($this->form_validation->run() == FALSE))
{
echo "NOPE!";
$this->load->view('home');
}
else
{
echo "Pass!";
}
this line is correct
$this->form_validation->set_rules('username', 'Username',
'required|trim|is_unique[users.username]|max_length');
first username is your field name and second one is which show in the error.
but this line
this->form_validation->set_rules('username', 'Password',
'required|trim|min_length[6]');
here you used username name which is already validating instead of username you used password
there are many ways for applying conditions
first one is
if($this->form_validation->run() == FALSE)) {
$this->load->view('home');
} else {
echo "Pass!";
}
second one is
if($this->form_validation->run() != FALSE) {
echo "Pass!";
} else {
$this->load->view('home');
}
Change
if($this->form_validation->run()) {
to
if($this->form_validation->run($this)) {

Validating Forms

I have a form that I submit with jQuery ajax and have it being sent to a controller function called submit to validate and do any other tasks I need to with the form data. I'm trying to find out why my form validation library isn't showing an error when the username doesn't contain only lowercase letters and numbers.
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|strtolower');
POST Value after form submission:
username TestingUSER
EDIT:
As far as I know it gets to the php server side properly.
PHP:
public function submit()
{
$output_status = 'Notice';
$output_title = 'Not Processed';
$output_message = 'The request was unprocessed!';
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|strtolower');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
$this->form_validation->set_rules('remember', 'Remember Me', 'trim|xss_clean|integer');
if ($this->form_validation->run() == TRUE)
{
}
else
{
$output_status = 'Error';
$output_title = 'Form Not Validated';
$output_message = validation_errors();
}
echo json_encode(array('output_status' => $output_status, 'output_title' => $output_title, 'output_message' => $output_message));
}
EDIT 2:
Based off of Sheikh answer. I am getting a response back that says "Unable to access an error message corresponding to your field name." It does say Form Not Validated for the title so the message isn't working.
public function check_username($str)
{
if (preg_match('#[0-9]#', $str) && preg_match('#[a-z]#', $str))
{
return TRUE;
}
$this->form_validation->set_message('username', 'This is not have an accepted value!');
return FALSE;
}
EDIT 3:
What I'm wanting to do is have it report back that there there are validation errors but not the specific errors in the pnotify response. However I do want it to display the specific errors under the form elements.
jQuery Code:
http://pastebin.com/1KehMJkh
Login Form:
http://pastebin.com/EfpBfbfN
I think you can use a callback function in your controller
public function check_username($str)
{
if (preg_match('#[a-z0-9]#', $str)) {
return TRUE;
}
$this->form_validation->set_message('check_username', 'This is not have an accepted value!');
return FALSE;
}
Validation rules for username
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|callback_check_username');
You may like this too.

Categories