public function doAssignerSU() {
$this->form_validation->set_rules('username', 'Username', 'required|alpha_numeric');
$this->form_validation->set_rules('password', 'Password', 'required|alpha_numeric|min_length[4]');
$this->form_validation->set_message('min_length','Minimum of 4 characters');
When I display my validation error in my view, it still displays the preset 'The {field} must be minimum of ..'
what's my mistake? Thanks in advance
Set your custom message like this..
$this->form_validation->set_rules('password', 'Password', 'required|alpha_numeric|min_length[4]',array('min_length'=>'Minimum of 4 characters'));
Hint:You can directly set your custom messages when setting rules.
For more see Codeigniter Form Validation
Related
I have a bit of problem with Twig in CodeIgniter.
I have tried to integrate Twig in CodeIgniter, so I would have clean templates.
CodeIgniter Simple and Secure Twig
This is really good, it is my first time to use templating engine.
But I encountered a problem. My form validation messages won't show if I submit the form.
public function user_register()
{
$this->form_validation->set_rules('username', 'Username', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required');
$this->form_validation->set_rules('firstname', 'Firstname', 'trim|required');
$this->form_validation->set_rules('lastname', 'Lastname', 'trim|required');
if ($this->form_validation->run() === FALSE) {
$data = [
'title' => 'Register',
];
$this->twig->display('user_register', $data);
}
}
If I do:
<?php echo validation_errors(); ?>
It won't output anything. My observation is, now that I have used Twig for my views, I cannot use php tags. Is it right to assume that?
If yes, how can I output validation errors in CodeIgniter using Twig?
I would appreciate any suggestions, and links that you will post. Hope you can guide me or lead me where to look at.
Thankyou.
Edit
I have tried using {{ validation_errors() }}
But it gives me an error: Please see screenshot:
You can do it using a very simple process like below code -
1. write this code in your controller
$data['errors'] = validation_errors();
$this->twig->display('user_register', $data);
2.
then inside your view, write {{ $errors }}
I have tested it & it's working without any problem.
I finally found the answer. In the package I included in my question, I just edited the file application/libraries/Twig.php:
private $functions_safe = [
'form_open',
'form_close',
'form_error',
'set_value',
'form_hidden',
'validation_errors', /** I just add this line and validation errors worked perfectly */
'form_input',
'form_password',
];
For anyone who will use the package above, hope this helps. Just add the function that you want to use in your view in the $functions_safe array.
Thanks!
I have an error message problem about form validation matches.
Here is what I set:
$this->load->library('form_validation');
$this->form_validation->set_rules('password', 'Password', 'required|trim');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'required|trim|matches[password]');
and of course, I have a form with two password input field.
form_password('password');
form_password('cpassword');
I am dealing with error messages using validation_errors() function.
if I let two password fields blank, i got:
The Password field is required.
The Confirm Password field is required.
And if I type something in password and let the Confirm Password field blank, I got:
The Confirm Password field is required.
so far so good until:
I type something in Confirm Password field and let the Password field blank, I got:
The Password field is required.
The Confirm Password field does not match the Password field.
I got two messages instead of one.
I just need the "The Password field is required." only.
What can I do for this? Please help, thanks.
In my own applications, I set up my validation rules like this
$this->form_validation->set_rules('password', 'Password', 'required|trim|matches[cpassword]');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'trim');
The password is required, and since it has to match cpassword then you are validating cpassword by default.
As James Lalor said above. here is the working code.
$this->load->library('form_validation');
$this->form_validation->set_rules('password', 'Password', 'required|trim');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'required|trim');
if (!empty($this->input->post('password'))) {
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'required|trim|matches[password]');
}
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;
}
}
I user validation method in my codeigniter project.
In that i set rules for validation like.
$this->form_validation->set_rules('firstname', 'First Name', 'required');
$this->form_validation->set_rules('lastname', 'Last Name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
Here display three difference errors when validation failed.
I want to display only one error like 'All fields are required' instead of follwing three.
You can add the attribute required in your inputs, so you don't need to send data to the server to get it checked.
<input type="text" name="fieldname" required />
and you'll get on every input a message saying that it's required.
add this line in controller
$this->form_validation->set_message('required', 'All fields are required');
$this->form_validation->set_message('valid_email', 'All fields are required'); // using this we overrideerror message found in the language file
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.