CodeIgniter form validation doesn't work - codeigniter

When I use functions in CodeIgniter form validation nothing happens (there are no problems with native CodeIgniter rules such as required).
Example:
$this->form_validation->set_rules('title', 'title', 'trim|strip_tags');

CodeIgniter's form validation rules support any of the validation rules found in the form validation library, prepping functions and any native PHP functions that permit one parameter.
So, native PHP, such as trim or strip_tags, can be used:
$this->form_validation->set_rules('title', 'title', 'trim|strip_tags');
If a native PHP function is to be used with more than one parameter, or additional functionality is required, than a callback should be used.
Validation rule
$this->form_validation->set_rules('title', 'title',
'trim|callback_custom_strip_tags');
Callback function
public function custom_strip_tags($str)
{
// Allow <p> tags, strip all other tags
return strip_tags($str, '<p>');
}

Related

How to share validation rules of several models and a large ajax form that holds them

I have a large ajax managed form with several steps. Obviously, I need to validate data after each submit step. The final validation results in the creation of several business objects.
Some business objects will receive all of their fields, others will not. For example, the customer who will not receive the billing address (it will be requested at the time the billing takes place). Indeed, the form being very long, I do not want to overload it by adding elements not immediately useful.
Some form partial concern only few informations
hold by my model so i can't instanciate model just for validation of little % of his attributes.
In principe, with October, the validation is done at the level of the models by adding the trait validator which will make it possible to use the generic Laravel validation functionalities (perhaps i'm wrong here)
But I'm not sure I can use on the models validation because some will be incomplete at the end of the form filling.
It is necessary to mutualize the validation of all these data without making a gas factory with duplicate validation code but how, where?
For the moment I am on the idea of ​​making a trait added to my object component which handles all my ajax handlers but I am not excited because it does not correspond to my idea to mutualize the validation
Perhaps using behavior instead of traits are better
https://octobercms.com/docs/services/behaviors
to be continued ...
If I had to do it, I'll use the Validation service after each submit step.
https://octobercms.com/docs/services/validation
You can store the info in a session and use them at the final stage.
$myinput = post('myinput');
$validator = Validator::make(
[
'myinput' => $myinput
],
[
'myinput' => 'required|min:8'
]
);
if ($validator->fails()) {
// The given data did not pass validation
}
// store in session
Session::push('myform.myinput', $myinput);
Session::push('myform.step', 2);
PS: on est deux francophones qui se causent en anglais :)

Codeigniter cannot validate the variable

valueWhen I want to validate posted value in Codeigniter, I do it in the following way:
$this->form_validation->set_rules('name', 'Name','required|xss_clean|strip_tags|trim|max_length[50]|alpha');
But I cannot understand how can I do the same validation for $name variable if it is grabbed from url like this:
$name = end($this->uri->segment_array());
I tried to do this and then validate in the same way
$this->form_validation->set_value('name', $name);
but validation did not pass. Could you please help me.
Technically we refer to this particular validation library as Form Validation which works on form data.
There are other methods such as using route files (which is a perfect tool in CI) that you can take advantage of for URL checking.
Beside any already-developed tools (like uri-validator), you can write your own validating functions.

Difference of xss clean and global xss filtering

I am a beginner codeigniter developer and need your help.
I have a form that the is submitted to a function in the controller and I want to apply the form_validation library rules on the post data received from the form.
All I need to know is this:
Does $config['global_xss_filtering'] switch in input class do the same job as xss_clean rule in form_validation class?
Yes it's same. If you have enabled $config['global_xss_filtering'] through the config file you don't have to call xss_clean in form validation,
but if you have switched off $config['global_xss_filtering'], you have to call xss_clean in form validation and also you have to add additional parameter (TRUE or FALSE) in input->post function as well in order to do xss filtering. see below.
$name = trim($this->input->post('name',TRUE));
This will be helpful if you are using WYSIWYG editor in your application, then you have to switch off $config['global_xss_filtering'] and you have to pass additional parameter in input->post function like above, TRUE - for enable filtering and FALSE for disabling (Wherever you use WYSIWYG editor).

Codeigniter Form Validation : order of validation rules

I have a form validation rule of something like this
$this->form_validation->set_rules('name', 'Name', 'trim|required|strip_tags|xss_clean|callback__name_check');
Now if anyone enters <p></p> or equivalent (empty tags), the result will be empty, but somehow required rule will pass as well, resulting in blank name when inserting to database.
I can do an extra check before inserting, but I wonder if there's a way in CI to prevent that?
Thanks
Have you tried to switch required and strip_tags in this rule?
$this->form_validation->set_rules('name', 'Name', 'trim|strip_tags|required|xss_clean|callback__name_check');
Try to delete the strip_tags rule (not appear in the codeigniter user guide) and try with:
$this->form_validation->set_rules('name', 'Name', 'trim|required|prep_for_form|xss_clean|callback__name_check');
I think that this is usefull for you
User guide says:
prep_for_form
Converts special characters so that HTML data can be shown in a form field without breaking it.
http://codeigniter.com/user_guide/libraries/form_validation.html

Using the Codeigniter form validation library, how can I ensure at least one letter is entered?

I am using Codeigniter form validation. In my registration form the Username field allows only numbers like 123456. I don't want this to happen.
My validation rule is as follows
'rules'=>'trim|required|alpha_numeric|min_length[6]|xss_clean'
I want to prevent users entering just numeric strings. Alpha numeric strings are fine, alpha strings are fine, but purely numeric ones are not.
To allow only letters
Add alpha to your rules and remove alpha_numeric from your rules
You can use this page as a point of reference for built in validation rules.
Edit:
Since you've clarified now.
To achieve this, there's no built in validation rule. You will need to extend the Form_validation library by creating a libraries/MY_Form_validation.php file. See this manual page on how to extend libraries.
In this file, create the following function
function at_least_one_letter($string) {
return preg_match('#[a-zA-Z]#', $string);
}
Then you can add the validation rule at_least_one_letter to your rules.
According to the codeigniter user manual :
You can also use any native PHP functions that permit one parameter.
I think in this case is_int could be used as your validation rule.
So, for instance:
'rules'=>'trim|required|is_int|min_length[6]|xss_clean',

Resources