Using codeigniter you retrieve a single value from the form validation by using set_value('example'), but can you access the entire array somehow and use it in a foreach such as this below?
$this->form_validation->set_rules('address', 'Street Address', 'required');
$this->form_validation->set_rules('city', 'City', 'required');
if($this->form_validation->run()){
$entered_values = set_value()
foreach($entered_values as $value){
}
}
Think outside CI and use php:
$entered_values = $_POST
Or if you want to use the CI input class:
$entered_values = $this->input->post();
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'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]');
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.
Assuming a form with errors, is there a way to get an array of key (the field name) / value (the error message) pairs? For example:
['name'] => 'The name field is required',
['age'] => 'The name must be greater than 18'
If there is no native way to do this, I will extend the form validation library and expose the protected property *_error_array*.
I ended up extending the core class:
class MY_Form_validation extends CI_Form_validation
{
public function error_array()
{
return $this->_error_array;
}
}
This might be way belated but I want to share a solution that I use that does not involve extending CI form validation library. The question already has an accepted answer but I hope that it will benefit someone else.
Unfortunately, CodeIgniter's $this->form_validation->error_array() method returns only the errors that emanate from the set_rules() method. It does not account for the name of the form field that generated the error. However, we can leverage another of CodeIgniter's method $this->form_validation->error(), which returns the error(s) associated with a particular form field, by passing the name of the field as parameter.
//form validation rules
$this->form_validation->set_rules('f_name', 'First Name', 'trim|required',
['required' => 'First Name is required']
);
$this->form_validation->set_rules('l_name', 'Last Name', 'trim|required',
['required' => 'Last Name is required']
);
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[users.email]',
[
'required' => 'Email is required',
'valid_email' => 'Email format is invalid',
'is_unique' => 'Email already exists'
]
);
$error_arr = []; //array to hold the errors
//array of form field names
$field_names= ['f_name', 'l_name', 'email'];
//Iterate through the form fields to build a field=error associative pair.
foreach ($field_names as $field) {
$error = $this->form_validation->error($field);
//field has error?
if (strlen($error)) $error_arr[$field] = $error;
}
//print_r($error_arr);