CodeIgniter Twig - Validation Errors - php

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!

Related

CodeIgniter set_message not working

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

form_validation config file in codeigniter (and callbacks)

After abusing Google for over an hour, I've found no answers to this question :
When using form_validation.php to your validation rules, Is it possible to pass a dynamic value to the callbacks?
array(
'field' => 'passwordrepeat',
'label' => 'סיסמא חוזרת',
'rules' => 'passwordsMatch['myDynamicValue']'
),
This clearly doesn't work as it passes "myDynamicValue" as a string.
Now, because this config file is loaded so early, this only available resource in it is CI_Loader, which doesn't help much, So I can't access the input class.
So my question:
Can a dynamic value pass to the config file, Or should that rule be written inline in the controller itself?
$this->form_validation->set_rules('password1', 'Password', 'trim|required|matches[password2]');
$this->form_validation->set_rules('password2', 'Verify Password', 'trim|required');
This is what I have for setting form validation on two passwords. This is what is what comes after you set all of your rules
if ($this->form_validation->run() == FALSE)
{
//Validation failed
}
else
{
//Validation suceeded carry on
}
Here is a link to some documentation
http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#validationrules
The answer to this question is a bit embarrassing.
The solution to this is that you can pass other field names to your callbacks.
However, remember that what you're passing is the field and not the actual value.
To get the actual value you'll need to access it through $_POST['field'].
For example the built in Matches function
public function matches($str, $field)
{
if ( ! isset($_POST[$field]))
{
return FALSE;
}
$field = $_POST[$field];
return ($str !== $field) ? FALSE : TRUE;
}
I feel a bit silly :)
The answer to the question is little tricky but it's easy to understand.
The solution for the asked question is here.
Create a form_validation.php file under the application/config/folder.
and past the code as bellow.
$config = array(
array(
'field' => 'passwordrepeat',
'label' => 'סיסמא חוזרת',
'rules' => 'passwordsMatch['myDynamicValue']'
),
);
the rules will loaded automatically available to the $this->form_validation->run(); this method.
Also you can append more array, I mean more rules for the different controller.
Hope this will help you.

Access entire set_value array?

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();

Unable to access an error message corresponding to your field name

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.

Codeigniter stripping out pound signs (£) using form validation

I've used Codeigniter for a while and whilst I've not always found the form validation as straightforward as I'm sure it should be I've never had any massive problems... until now!
I have a simple form comprised of text inputs and a textarea. The form starts off prepopulated and, if the validation fails, repopulates it with the last changed state.
My problem is this - The textarea needs to accept pound signs (£). It populates text from the database absolutely fine but on submit, whether the form validates or not, it strips them out, regardless of what I do!!
I've scoured the net and can only find solutions about applying things like htmlentities to the validation rules, but if I firephp the post data out, even before the rules, it's already been stripped out.
global_xss_filtering is set to false in my config.
It's driving me mad and wasting way more time than it should... has anyone got a solution to this, I know I'm probably missing something really simple - it's maddening!
Thanks,
Helen
Here's my validation code, although the firephp log at the top shows it to already be stripped out so I can't see how doing anything here will help... I've tried adding the various php function as it suggests HERE (codeigniter manual) but it makes no difference at all.
public function edit_entry2($entry_id, $page_id) {
$this->firephp->log($_POST);
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'required|max_length[255]');
$this->form_validation->set_rules('address1', 'Address line 1', 'max_length[255]');
$this->form_validation->set_rules('address2', 'Address line 2', 'max_length[255]');
$this->form_validation->set_rules('address3', 'Address line 3', 'max_length[255]');
$this->form_validation->set_rules('address4', 'Address line 4', 'max_length[255]');
$this->form_validation->set_rules('county', 'County', 'required|max_length[255]');
$this->form_validation->set_rules('post_code', 'Post Code', 'max_length[10]');
$this->form_validation->set_rules('telephone1', 'Telephone 1', 'required|max_length[12]|is_natural');
$this->form_validation->set_rules('telephone2', 'Telephone 2', 'max_length[12]|is_natural');
$this->form_validation->set_rules('fax', 'Fax', 'max_length[12]|is_natural');
$this->form_validation->set_rules('email', 'Email address', 'valid_email');
$this->form_validation->set_rules('website', 'Website', 'max_length[255]');
$this->form_validation->set_rules('rating_awards', 'Rating/Awards', 'max_length[255]');
$this->form_validation->set_rules('description', 'Description', 'max_length[1000]');
$this->form_validation->set_rules('categories[]', 'Categories', 'callback_categories_check');
if ($this->form_validation->run() == FALSE)
{
$this->edit_entry($entry_id, $page_id);
}
else
{
$updated_entry = array('name'=>$_POST['name'], 'address1'=>$_POST['address1'], 'address2'=>$_POST['address2'], 'address3'=>$_POST['address3'], 'address4'=>$_POST['address4'], 'county'=>$_POST['county'], 'post_code'=>$_POST['post_code'], 'telephone1'=>$_POST['telephone1'], 'telephone2'=>$_POST['telephone2'], 'fax'=>$_POST['fax'], 'email'=>$_POST['email'], 'website'=>$_POST['website'], 'rating_awards'=>$_POST['rating_awards'], 'description'=>$_POST['description']);
$this->tourism_catalogue_model->update_entry($entry_id, $updated_entry, $_POST['categories']);
$this->index($page_id);
}
}
You can use validation callbacks. So, when your validation rule is triggered, it will then fire a callback function that you could then use to strip out or inject the pound symbols as desired.
Form Validation Callbacks

Categories