Target
Mobile field is optional, and depending whether it has or has no value, there will be a validation rule to it, accordingly. But seems like, that even though the right validation rule is set, it always return true for the mobile field.
My Code
$client['mobile'] = "asdf";
$config = array(
'member_form' => array(
array( 'field' => 'client[firstName]', 'label' => 'First Name', 'rules' => 'required|trim'),
array( 'field' => 'client[lastName]', 'label' => 'Last Name', 'rules' => 'required|trim')
);
if($client["mobile"])
array_push($config["member_form"], array( 'field' => 'client[mobile]', 'label' => 'Mobile Number', 'rules' => 'required|trim|numeric'));
else
array_push($config["member_form"], array( 'field' => 'client[mobile]', 'label' => 'Mobile Number', 'rules' => 'trim'));
if ($this->form_validation->run("member_form") == TRUE) {
. . .
} else {
. . .
}
Thank you in advance.
Related
Is CodeIgniter able to interpret config/form_validation.php to display appropriate error messages?
There is a way with validation when using controller: $this->form_validation->set_message('min_length', '{field} must have at least {param} characters.');
Current validation rules (and the last array is what I'm interested in, if there is a way to add validation messages in the same array as the rules.):
'users' => array(
array(
'field' => 'first_name',
'label' => 'First Name',
'rules' => 'trim|required'
),
array(
'field' => 'last_name',
'label' => 'Last Name',
'rules' => 'trim|required'
),
array(
'field' => 'email_address',
'label' => 'Email Address',
'rules' => 'trim|required|valid_email'
, 'messages'=> array('valid_email'=>'Please make sure your email is correct.')))
You Can Read And Follow The Given Example in Below Link.I Think This Will Help You.
Custom Error Messages - Form Validation Codeigniter
CodeIgniter allows to add validation messages as described in the docs. The array would be as follows:
'users' => array(
array(
'field' => 'first_name',
'label' => 'First Name',
'rules' => 'trim|required'
),
array(
'field' => 'last_name',
'label' => 'Last Name',
'rules' => 'trim|required'
),
array(
'field' => 'email_address',
'label' => 'Email Address',
'rules' => 'trim|required|valid_email'
, 'errors'=> array('valid_email'=>'Please make sure your email is correct.')))
I am not new to CI, but trying something different and moving my validations from my controller (there's lots and its getting messy) to the form_validation.php file in the /application/config directory.\
The method I am trying to use is the function based on the controller/method where it should auto-load the rules based on where you run $this->form_validation->run()
I have read the documentation (many times) and I have seen other posts on stackoverflow and none have given me a solution...
my current setup is below...
application/config/form_validation.php
//I know the file is being loaded as these work
$config['error_prefix'] = '<span class="text-danger">';
$config['error_suffix'] = '</span>';
/**
* METHOD SPECIFIC VALIDATIONS
*/
/* Controller: Account
* Method: Register
*/
$config = array(
'account/register' => array(
'field' => 'company',
'label' => 'Company',
'rules' => 'required|is_unique[company.companyName]',
array(
'required' => 'You have not provided {field}.',
'is_unique' => 'This {field} already exists.'
)
),
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'alpha_numeric|trim|required|is_unique[users.username]',
array(
'required' => 'You have not provided {field}.',
'is_unique' => 'This {field} already exists.'
)
),
array(
'field' => 'firstname',
'label' => 'First Name',
'rules' => 'required'
),
array(
'field' => 'lastname',
'label' => 'Last Name',
'rules' => 'required'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'required|min_length[6]',
array(
'min_length' => '{field} must have at least {param} characters.'
)
),
array(
'field' => 'passconf',
'label' => 'Confirm Password',
'rules' => 'required|matches[password]'
),
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'trim|required|valid_email|is_unique[users.email]',
array(
'required' => 'You have not provided {field}.',
'is_unique' => 'This {field} already exists.'
)
)
);
Controller:
class Account extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->model('account_model');
}
public function register()
{
//Form not yet submitted, user not logged in, display login page
if ($this->form_validation->run() == FALSE and $this->session->userdata('loginuser') == FALSE) {
$this->load->view('templates/header');
$this->load->view('account/register');
$this->load->view('templates/loadjs');
} else {
}
View Snippet:
<input class="form-control" name="company" placeholder="Company Name" type="text" value="<?php echo set_value('company'); ?>" autofocus />
</div>
<div><?php echo form_error('company'); ?></div>
Going by the documentation I linked, you should be able to just use $this->form_validation->run() and it will auto-call these rules?
For those playing at home the answer was that I was over-writing the $config array with my original method. As the validation file is included and not separate, it assigns rather than appends the $config array.
$config['account/register'] = array(
array(
'field' => 'company',
'label' => 'Company',
'rules' => 'required|is_unique[company.name]',
'errors' => array(
'required' => 'You have not provided {field}.',
'is_unique' => 'This {field} already exists.'
)
),
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'alpha_numeric|trim|required|is_unique[users.username]',
'errors' => array(
'required' => 'You have not provided {field}.',
'is_unique' => 'This {field} already exists.'
)
),
array(
'field' => 'firstname',
'label' => 'First Name',
'rules' => 'required'
),
array(
'field' => 'lastname',
'label' => 'Last Name',
'rules' => 'required'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'required|min_length[5]',
'errors' => array(
'min_length' => '{field} must have at least {param} characters.'
)
),
array(
'field' => 'passconf',
'label' => 'Confirm Password',
'rules' => 'required|matches[password]'
),
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'trim|required|valid_email|is_unique[users.email]',
'errors' => array(
'required' => 'You have not provided {field}.',
'is_unique' => 'This {field} already exists.'
)
)
);
Looking at your code again, I think I see a mismatch. I see that you are loading the form_validation as a library, however that should mean there is something in the library folder you are not showing us, or maybe you meant to use a config class instead to access it since it is in the config folder. If that is true you might want to look at the config class.
I'm trying to do an email validation whereby the domain of the email would be #abc123.com. I've separated my form validation rules into another file in the application/config folder called form_validation.php. One of my rules consists of a callback_email_check.
Where should I put the function? In the main controller or together with the form_validation.php file where all my form validation rules are? I've tried putting at both options but at where I display my error message I'm getting an output saying Unable to access an error message corresponding to your field name Email.(email_check).
function email_check($email)
{
if( strpos($email, '#abc123.com') !== FALSE ) return TRUE;
$this->form_validation->set_message('email', 'Please use abc123 email only.');
return FALSE;
}
form_validation.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/* Form Validation Rules */
$config = array(
'login' => array(
array(
'field' => 'user_id',
'label' => 'User ID',
'rules' => 'trim|required'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'trim|required'
)
),
'sign_up' => array(
array(
'field' => 'user_id',
'label' => 'User ID',
'rules' => 'trim|required'
),
array(
'field' => 'name',
'label' => 'Name',
'rules' => 'trim|required'
),
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'trim|required|valid_email|callback_email_check'
),
array(
'field' => 'department',
'label' => 'Department',
'rules' => 'trim|required'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'trim|required'
),
array(
'field' => 'cfm_password',
'label' => 'Re-type Password',
'rules' => 'trim|required|matches[password]'
)
),
'edit_profile' => array(
array(
'field' => 'new_password',
'label' => 'New Password',
'rules' => 'trim|required'
),
array(
'field' => 'retype_password',
'label' => 'Re-type Password',
'rules' => 'trim|required|matches[new_password]'
)
),
'forgot_password' => array(
array(
'field' => 'user_id',
'label' => 'User ID',
'rules' => 'trim|required'
),
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'trim|required|valid_email|callback_email_check'
)
)
);
?>
On your function email_check, the set_message is not correct it should be the same name as the function.
Change this
$this->form_validation->set_message('email', 'Please use abc123 email only.');
To
$this->form_validation->set_message('email_check', 'Please use abc123 email only.');
Call backs http://www.codeigniter.com/userguide2/libraries/form_validation.html#callbacks
I am also facing the same problem and this is how i resolved it...
You can put email_check function in same controller. In case you are not getting the error message in callback then pass $this in your run()
if ($this->form_validation->run($this)) { ...}
and associating a Controller Method with a Rule Group -
$config = array(
'controller/method' => array(...));
view link for more : [associating a Controller Method with a Rule Group][1]
cheers !!
Just add this line in your config:
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'trim|required|valid_email|callback_email_check',
**'errors' => array('email_check' => 'Your Error Message')**
),
I am using codeigniter form validation to validate user data when creating new user. I want to add some sort of password criteria for example password must have
at least one capital letter, a number, and one of !, #, #, $ etc) and there have to be 6-25 characters.
This is the array that I am using for validation rules:
$config = array(
array(
'field' => 'title',
'label' => 'Title',
'rules' => 'trim|required'
),
array(
'field' => 'firstname',
'label' => 'First Name',
'rules' => 'trim|required|min_length[2]|max_length[100]|xss_clean'
),
array(
'field' => 'lastname',
'label' => 'Last Name',
'rules' => 'trim|required|min_length[2]|max_length[100]|xss_clean'
),
array(
'field' => 'phone',
'label' => 'Telephone',
'rules' => 'trim|required|intiger|min_length[11]|max_length[11]|xss_clean'
),
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'trim|required|valid_email|is_unique[ci_user.email]|matches[conf_email]|xss_clean'
),
array(
'field' => 'conf_email',
'label' => 'Confirm Email',
'rules' => 'trim|required|valid_email|xss_clean'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'trim|required|min_length[6]|max_length[25]|matches[conf_password]|xss_clean'
),
array(
'field' => 'conf_password',
'label' => 'Confirm Password',
'rules' => 'trim|required|xss_clean'
));
Can someone please guide me on how to achieve what I need. Thank you
you can setup call back function to check password strong validation. and call this function callback_is_password_strong in this line of your code.
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'trim|required|min_length[6]|max_length[25]|matches[conf_password]|xss_clean|callback_is_password_strong'
),
and if you look this function will return true or false and password array rule key is validated only when it returns true
public function is_password_strong($password)
{
if (preg_match('#[0-9]#', $password) && preg_match('#[a-zA-Z]#', $password)) {
return TRUE;
}
return FALSE;
}
Using callback function
public function check_strong_password($str)
{
if (preg_match('#[0-9]#', $str) && preg_match('#[a-zA-Z]#', $str)) {
return TRUE;
}
$this->form_validation->set_message('check_strong_password', 'The password field must be contains at least one letter and one digit.');
return FALSE;
}
Usage
$this->form_validation->set_rules('adminPassword', 'password', 'required|min_length[8]|max_length[25]|callback_check_strong_password');
I'd like some help please.
This is the array that holds all the validation on a contact form
class Contact_Form extends CI_Controller
{
private $_validation = array(
'fullname' => array(
'field' => 'fullname',
'label' => 'Fullname',
'rules' => 'trim|required|max_length[255]'
),
'email' => array(
'field' => 'email',
'label' => 'Email Address',
'rules' => 'trim|required|max_length[255]|valid_email'
),
'phone' => array(
'field' => 'phone',
'label' => 'Phone',
'rules' => 'trim|max_length[10]|integer'
),
'message' => array(
'field' => 'message',
'label' => 'Message',
'rules' => 'trim|required'
),
'captcha' => array(
'field' => 'captcha',
'label' => 'Security Code',
'rules' => 'trim|required|callback_validate_captcha'
)
);
// This is the part where I validate my contact form inside a method
$this->load->library('form_validation');
$this->form_validation->set_rules($this->_validation);
if ($this->form_validation->run() === true) {
echo 'works!';
}
This is the callback function that validates the captcha
public function callback_validate_captcha($str) {
$post_captcha = $this->input->post('captcha');
$set_captcha = $this->session->userdata('captcha');
if (strcmp($set_captcha, $post_captcha) !== 0) {
$this->form_validation->set_message('validate_captcha', '%s is wrong');
return false;
}
return true;
}
If i hit submit on an empty form I get the error that idicates that captcha is a required field, but if i submit a wrong code i don't get any error at all, which means that the callback is being ignored.
I tried to change my if statement
// change this (althought i feel its more correct)
if (strcmp($set_captcha, $post_captcha) !== 0)
// to this
if ($set_captcha != $post_captcha)
but the problem remains. Any ideas what's wrong?
you are making major mistake you have to make function validate_captcha instead of callback_validate_captcha.
Because callback is form keyword to call a function just try and bingo