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;
}
}
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'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
PHP / CodeIgniter.
In order to set up a form that validates the logic: "either one, or both, of the fields is required" I have to use inline form validation like this (source is http://ellislab.com/forums/viewthread/136417/#672903):
if ( ! $this->input->post('email'))
{
$this->form_validation->set_rules('phone', 'Phone Number', 'required');
}
else
{
$this->form_validation->set_rules('phone', 'Phone Number', '');
}
// If no phone number, email is required
if ( ! $this->input->post('phone'))
{
$this->form_validation->set_rules('email', 'Email Address', 'required|valid_email');
}
else
{
$this->form_validation->set_rules('email', 'Email Address', 'valid_email');
}
But I have a whole lot of other forms where I'd prefer to use config file based form validation.
I cannot think of a way to get the two to co-exist, and I don't really want to now go and bring all my rules into the code body.
Any suggestions?
You can use rule sets from the config file or inline rules just fine in the same application.
config/form_validation.php
$config = array(
'ruleset1' => array(
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'required|trim|alpha'
),
)
);
controller example
public function ruleset()
{
if ($this->input->post())
{
// runs validation using ruleset1 from the config
if ($this->form_validation->run('ruleset1') == FALSE)
{
...
}
}
}
public function inline_rules()
{
if ($this->input->post())
{
// ignores the config and uses the inline rules
$this->form_validation->set_rules('username', 'Username', 'required|trim|alpha_numeric');
if ($this->form_validation->run() == FALSE)
{
...
}
}
}
Note: I found that trying to mix them for the same form does not work. Specifying inline rules and a ruleset on the same form will cause the ruleset to be ignored completely and the inline rules to be applied.
Create a file in your libraries name it MY_Form_validation
class MY_Form_validation extends CI_Form_validation
{
public function __construct($rules = array())
{
parent::__construct($rules);
$this->CI->lang->load('MY_form_validation');
}
function email_phone($str)
{
if(!$str)
{
// if POST phone exists validate the phone entries
//validation for phone
return TRUE;
}else{
//if no phone was entered
//check the email
$email = $this->input->post('email'));
//use the systems built in validation for the email
//set your error message here
return $this->valid_email($email) && $this->required($email);
}
}
}
//or set the message here
$this->form_validation->set_message('email_phone','Please enter either an email or phone.');
$this->form_validation->set_rules('phone', 'Phone Number', 'email_phone');
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.
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.