Well here is a example error Codeigniter will shoot out at the user if they don't type in their email.
email: The Email field is required.
Now the "email" part specifically. How can I change it to whatever I like? It seems its using the inputs name/id.
Example of how I setup form validation for email.
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[user.email]');
So maybe a better example .. say I wanted to change it from saying "email" to "The Email"
Thanks in advnace.
You should use set_message function in form_validation library.For example;
$this->form_validation->set_message('email', '%s is entered email adress, it's wrong!');
CodeIgniter's user-guide is very easy and interactive.You should check the documentation for these type of basic questions.
CodeIgniter user-guide
This is how I do it:
If you want to alter the message use set_message:
$this->form_validation->set_message('required', 'Your custom message here');
Using a Callback is a better way to do it:
$this->form_validation->set_rules('username', 'Username', 'check_user_name');
public function username_check($str)
{
if (strlen($str)<0)
{
$this->form_validation->set_message('check_user_name', 'The %s field is empty"');
return FALSE;
}
else
{
return TRUE;
}
}
Related
There's a way to add custom error messages to CodeIgniter validation_errors();?
Example, if I wanted a field with a 123456 value, and the user inputs 12345 I'd want to set a message to say:
The number 6 is required!
And any other custom rules I may want to add. Like a specific pattern or any other things.
Sorry for my english.
Yes, that is possible.
Set rules with callback like,
$this->form_validation->set_rules('field_name', 'Number', 'callback_custom_validation');
and define callback in the same controller like,
public function custom_validation($str)
{
if ($str != '123456')
{
$this->form_validation->set_message('field_name', 'The %s field requires 123456');
return false;
}
else
{
return true;
}
}
Display your errors in view with <?php echo form_error('field_name')?>
More info on callbacks here.
I'm trying to make a validation for 2 field that must have a different value. I only know how to set the rules for validate matching value.
$this->form_validation->set_rules('book1','Book1','required|matches[book2]');
$this->form_validation->set_rules('book2','Book2','required|matches[book1]');
if I input book1=novel, and book2=novel, the code above will return TRUE.
How can I validate 2 field where the value of each field is not matching each other? So if I input book1=novel and book2=comic, it will return TRUE.
You should use callback_ method for custom validation, CI form validation library does not provide notMatch type validation rule, see below sample code.
$this->form_validation->set_rules('book1','Book1','required');
$this->form_validation->set_rules('book2','Book2','required|callback__notMatch[book1]');
AND place method in controller class
function _notMatch($book2Value, $book1FieldName){
if($book2Value != $this->input->post($book1FieldName){
$this->form_validation->set_message('_notMatch', 'book1 and book2 values are not matching');
return false;
}
return true;
}
In codeigniter 3 you can use the differs[] set rule, to enforce that field values don't match.
$this->form_validation->set_rules('book1', 'Book 1', 'required|differs[book2]');
$this->form_validation->set_rules('book2', 'Book 2', 'required|differs[book1]');
This means you don't need to create an unnecessary callback. However, for older versions you will.
See the documentations for more: Codeigniter 3 Documentation
You can use differs like so:
$this->form_validation->set_rules('password', 'current password', 'max_length[25]|min_length[5]|required');
$this->form_validation->set_rules('new_password', 'new password', 'max_length[25]|min_length[5]|required|differs[password]');
$this->form_validation->set_rules('confirm_password', 'confirm password', 'required|max_length[25]|min_length[5]|matches[new_password]');
Ok I don't know what's not working. I know my form validation is definitely working because all my other functions work properly, but I am setting messages whether it's true OR false and none of them show up so I feel like it's skipping right over the validation rule.. which is weird...
$this->form_validation->set_rules('region', 'required|valid_region');
The rule in MY_Form_validation.php in my libraries folder. The library IS loaded first. As I said all my other validations work properly such as my reCaptcha and everything.
function valid_region($str) {
$this->load->database();
if($this->db->query('SELECT id
FROM region
WHERE name = ?
LIMIT 1', array($str))->num_rows() == 0) {
//not a valid region name
$this->set_message('valid_region', 'The %s field does not have a valid value!');
return false;
}
$this->set_message('valid_region', 'Why is it validating?');
}
None of the messages will set so I have a feeling nothing is validating!
set_rules() function takes 3 parameters
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.
You put the validation rules as second parameter. That is why the validation is not running. Try this instead:
$this->form_validation->set_rules('region', 'Region', 'required|valid_region');
instead of
$this->form_validation->set_rules('region', 'required|valid_region');
try
$this->form_validation->set_rules('region', 'required|callback_valid_region');
when using custom validation rules you should use
callback to prepend the function name.
UPDATE
and use
$this->form_validation->set_message
instead of
$this->set_message
and in function valid_region
use return true when validation is successfull
$this->form_validation->set_rules('region', 'Region', 'required|valid_region');
function valid_region() {
$str = $this->input->post('name_of_input');
$this->load->database();
if($this->db->query('SELECT id
FROM region
WHERE name = ?
LIMIT 1', array($str))->num_rows() == 0) { // why compare "=" between `name` field and array() ?
//not a valid region name
$this->form_validation->set_message('valid_region', 'The %s field does not have a valid value!');
return false;
}
$this->form_validation->set_message('valid_region', 'Why is it validating?');
return true;
}
I have a callback function that check_captcha which sees if $row is ==0 or == 1 (this information is queried from sql).
The problem is that I can not call it from $self->form_validation->set_rule('captcha', 'call_back_check_captcha') due to the fact that my function takes in a $row var. The way I'm calling it now I get a Unable to access error message. How can I make this work?
function check_captcha( $row)
{
if($row ==0)//didnt find any
{
$this->form_validation->set_message('captcha', 'text dont match captcha');
return FALSE;
}
else
{
return TRUE;
}
}
function create_member()
{
$past = time() - 7200;
$this->db->query("DELETE FROM captcha WHERE captcha_time <".$past);
$sql = "SELECT COUNT(*) AS count FROM captcha WHERE word =? AND ip_address =?";
$binds = array($_POST['captcha'], $this->input->ip_address(), $past);
$query= $this->db->query($sql, $binds);
$row = $query->row(); //row query rows : if it found an entry =1
$self->check_captcha($row->count);
//VALIDATIONS
$this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
$this->form_validation->set_rules( 'email_address', 'Email Address', 'trim|required|valid_email|unique[user.email_address]');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|unique[user.username]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_leng[32]');
$this->form_validation->set_rules('password2', 'Password Confirmation','trim|required|matches[password]');
if(!$_POST['captcha']){
$this->form_validation->set_rules('captcha', 'Captcha','trim|required');}else{
$this->form_validation->set_rules('captcha', 'Captcha', 'callback_check_captcha');}
if($this->form_validation->run()==FALSE)
{ //this -> to the curr obj(UserController) && registraion() points to the the function in controller
$this->registration(); //reloads reg page so they can fill out right stuff
}
else
$this->form_validation->set_message('check_captcha', 'text dont match captcha');
The message name corresponds to the function, not the field. So setting it to "check_captcha" will fix your bug. The error message will use the correct field name.
Actually the best way, instead of write the error message directly on controller, would be add this entry "check_captcha" on languages.
In my case, the message for validation rule (form validation) "less_than" was not present.
I changed the file /system/language/??/form_validation_lang.php. I've added the missing entry.
That helped me
go to application/config/autoload.php and add "Security" helper class there.
$autoload['helper'] = array('security');
Or add this before your form validation
$this->load->helper('security');
You can set error message in set_rules :
$this->form_validation->set_rules('captcha', 'Captcha', 'callback_check_captcha',
array('check_captcha' => 'text dont match captcha'));
add a entry to your language file with named of the part inside the (yourfieldname) of the errormessage - thats solved the problem
Even if the question is already answered, there is another error that can lead to the same error message:
If you call your callback checkCaptcha, it will not work. Prefer always a name like check_captcha as recommended in Codeigniter/General Topics/PHP style guide.
O have a form that the fields are prefilled by jQuery. When a user clicks in the field, the field empties itself, and they type their information. However if they don enter information in each filed the default value is submitted. I would like to use Codeigniter's built in validation to disallow users creating an account with a first name of "First Name".
See here: otis.team2648.com/auth/register
Thanks,
Blake
You can use a callback function:
// your rules
$this->form_validation->set_rules('first_name', 'required|callback__no_first_name');
// callback
function _no_first_name($str) {
if ($str !== 'First Name') {
return TRUE;
}
else
{
$this->form_validation->set_message('_no_first_name', 'You should not have "First Name as the first name"');
return FALSE;
}
}
I would extend the Form_validation library and turn it into a more valuable form validation rule that you could reuse easily...
example - (Change relevant info for your version of CI)
class MY_Form_validation extends CI_Form_validation {
function __construct() {
parent::CI_Form_validation();
}
function disallow_string($str,$word)
{
return ( strpos($str,$word) === FALSE ) ? TRUE : TRUE;
}
}
Place above code in MY_Form_Validation.php in application/libraries
and in your validation, just use the rule like this
$this->form_validation->set_rules('first_name', 'required|disallow_string[First Name]');
note that you can then use this same rule for all fields, as other uses I can envision.
Don't write the default text as value in the field! Use this construction:
<input type="text" name="uname" id="uname" placeholder="Your name" />
If the user set the focus on this field, the text disappears. But no value is submitted if the user insert nothing.