<?php
class Form extends CI_Controller {
public function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('confpassword', 'Password', 'required|matches[password]', 'callback__matcherror');
//$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
//$this->form_validation->set_rules('email', 'Email', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('login');
}
else
{
$this->load->view('insert_dream');
}
}
public function _matcherror() {
$this->form_validation->set_message('_matcherror', 'Passwords should match');
return FALSE;
}
}
?>
i am a newbie to codeigniter. The above code doesnt display passwords should match error message. Is something wrong with the callback or Am i missing something.
Take a look here. You don't need to make a callback.
You are passing callback__matcherror as fourth parameter of set_rules function.It should be 3rd parameter. Use this way
$this->form_validation->set_rules('confpassword', 'Password', 'required|matches[password]|callback__matcherror');
Note
You will get this error message if your password fields match.Because you applying 3 rule there.3rd rule(call_back_function) will apply when 2nd rule is success. Your 2nd rule will valid when passwords matches.
matches[password]
will automatically check for password. You need not to use callback function callback__matcherror
Related
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
I have a website build in CodeIgniter framework. The website contain a lot of forms for different purposes. I am submitting these forms directly to a controller function.
What are the things I should apply in this input before I save it to database through the model?
If I directly send this data without doing anything, what will be the security risk?
You need to use form_validation
https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html
Best way will be if you will set rules for each field
like this
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
And you don't need to use any special escape
Or try to understand how to use PDO in CodeIgniter
http://codebyjeff.com/blog/2013/03/codeigniter-with-pdo
When form_validation->run() returns FALSE, I need to know which rule didn't pass the validation test.
I know I can echo form_error() or validation_errors(), but this needs to be done before a view is loaded.
The controller needs to act differently according to which input didn't pass the validation.
Example:
public function index()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run())
{
$this->load->view('something');
}
else
{
//check which rule didn't pass and act accordingly
}
}
I tried using empty on form_error('something') from within the if statement but this returns an error as empty doesn't take functions.
Also note, I can't change the error delimiters because they are used elsewhere in the script.
How would I check which rule was triggered without echoing validation_errors()?
YOU CAN USE error() for this-
public function index()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run())
{
$this->load->view('something');
}
else
{
print_r($this->form_validation->error());
}
}
To display an error message using a specific field
form_error('username')
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)) {
I am trying to use CodeIgniter's form validation class. I've used the "valid_email" parameter, as can be seen from the code below, but even when an invalid email address is entered it still passes the validation check. I tested with the string: testing123
public function login()
{
$this->form_validation->set_rules('authEmail', 'trim|required|valid_email|xss_clean');
$this->form_validation->set_rules('authPassword', 'trim|required');
$email = $this->input->post('authEmail');
$password = $this->input->post('authPassword');
if($this->form_validation->run() === FALSE) {
$this->session->set_flashdata('formValidationError', validation_errors('<p class="error">', '</p>'));
redirect('/');
} else {
// Begin authentication
}
}
Anyone have any idea what I'm doing wrong or if this is a CodeIgniter issue?
To note, I am setting a flashdata session as opposed to using:
<?php echo validation_errors(); ?>
... this is because I am doing a redirect back to the homepage (which is the login page as it's a private site).
Try this:
public function login()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('authEmail', 'Email', 'trim|required|valid_email|xss_clean');
$this->form_validation->set_rules('authPassword', 'Password', 'trim|required');
if($this->form_validation->run() !== false){
//validation passed
$email = $this->input->post('authEmail');
$password = $this->input->post('authPassword');
// Begin authentication
}
else {
$this->session->set_flashdata('formValidationError', validation_errors('<p class="error">', '</p>'));
redirect('/');
}
}
I'm just learning to use it as well, but don't you need three parameters? This is from their form validation page:
$this->form_validation->set_rules('email', 'Email', 'required');
From http://codeigniter.com/user_guide/libraries/form_validation.html#validationrules
The field name - the exact name you've given the form field.
A "human" name for this field, which will be inserted into the error message. For example, if your field is named "user" you might give it a human name of "Username". Note: If you would like the field name to be stored in a language file, please see Translating Field Names.
The validation rules for this form field.