Hi I'm trying to make my own Form validator just like the authenticate method in the LoginForm that is generated on the default configs.
public function rules()
{
return array(
// username and password are required
array('mnemonic, target_reg, source_reg', 'required'),
// rememberMe needs to be a boolean
array('target_reg_indirection, source_reg_indirection', 'boolean'),
array('mnemonic','foo'),
);
}
and here is the validator method:
public function foo($attribute,$params){
$this->addError('mnemonic', 'there was an error, you foo!');
}
it just doesn't work for me... notice how I added a rule that should not-work everytime. I just made it so I could see how it worked. But I never get to see the error message in my view. The default validators (like the one that checks for required fields) work.
Any ideas?
Because not all validators map to a client-side validator.
IN addition to that, I've created the code to handle ajax form validation and I've enabled ajaxValidation in my CActiveForm.
So now all works great. The validator I've created is working via ajax validation.
I had the same problem, in my case wasn't to enabled ajaxValidation, but the "safe" validator and a mix of other things.
Here I post some reading that helped me to solve the problem. Hope this will help others with the same problem.
I read a little bit about "safe" validator. (http://www.yiiframework.com/wiki/161/understanding-safe-validation-rules/)
I Understand the difference between AjaxValidation and ClientValidation. (http://www.yiiframework.com/doc/api/1.1/CActiveForm)
I created my own validator class. (search "custom validation yii" on google).
Related
I have a question about enabling the is_unique() rule for form validation in CodeIgniter.
In another explanation (link), they don't include the model query builder for standard usage of is_unique()
I need to use the rule is_unique(table.field) for my id field.
What should I do for making this function work on my model file to initiate table.field from my database? Because at documentation, I didn't see an explanation for enabling the is_unique rule.
My current code is still use matching data manually, but I need to know how to use this rules
$this->form_validation->set_rules('siteid', 'Site ID', 'trim|required|max_length[100]|is_unique[site_tower.site_id_tlp]');
I have just gone through the link you posted, There are 2 ways to use such validation. If you have set in your configuration files.
With that you can use the code as is is_unique[TABLE_NAME.FIELD] and it will work automatically. But at times this logic might not necessarily meet your need and you will need something more complex.
For example lets say you have a members registration that requires you to check if the email already exists, you can run is_unique and it will work perfectly. Now let's say you want to edit the same member, running is_unique on an edit function will render the user unable to save the data if no data is edited. WHY? because is_unique would determine that the email is already registered although it belongs to the current user that is being edited.
How do we fix this? We run our own callback in which we specify the logic.
You do it by specifying a method within the controller (or a model -- slightly different) but you prefix the method name with callback_ so that it is detected.
$this->form_validation->set_rules('username', 'Username', 'callback_username_check');
This will then look for a method in your controller called 'username_check'
public function username_check($str)
{
if ($str == 'test')
{
$this->form_validation->set_message('username_check', 'The {field} field can not be the word "test"');
return FALSE;
}
else
{
return TRUE;
}
}
Of course you can use a query within the callback to check against the db rather than check for just a string as it shows in the example.
more information can be found on Ci3 documentation.
LINK
Use CTRL + F and search for callback or is_unique
You might have missed this?
$this->load->library('database');
works instantly after adding database lib.
This is probably a silly question, but i could't figure it out. I have the following:
array('email', 'email','message'=>'The email isnĀ“t correct'),
What would this validation validate exactly? That the input text contains '#' and a '.' ?
The validator uses a regular expression to validate the email.
For the specific expression it uses, look at the source. You can then use an online tool like reFiddle to quickly check if the regex matches any particular input.
Yii is a great framework for form validation,today i am going to show you how to validate the email field in the form before submitting.Its very simple just follow the below example.
if you created a user model with email as one attribute then in the rules function add the following code.
public function rules() {
return array(
...
array('email', 'email','checkMX'=>true),
...
); }
thats it
I have a form with a URL field. The default value for this field is: http://. But the field is not required. The user can skip it and submit the form. It shouldn't return an error because it's not required and because they didn't enter a URL. But right now it does, because of the http://.
I heard I can use beforeValidate() to check if it's http://, and then clear the URL field, allowing me to skip the error message.
But I don't know how to use beforeValidate(). I searched Google, but I did not find any working examples. Where do I place the code for beforeValidate()? Is it a function? How do I access the submitted form data from there?
Thanks.
Yes, beforeValidate() is a function of the model. So every model has it. How you should use it:
class YourModel extends AppModel {
function beforeValidate(){
if($this->data['YourModel']['url_field'] == 'http://'){
unset($this->data['YourModel']['url_field']);
}
return true; //this is required, otherwise validation will always fail
}
}
instead of hard coding http:// into the form, add proper validation for urls and use the following to allow blanks
'allowEmpty' => true
HI guys,
I building an app using CodeIgniter and I came to a problem. I have a form with a textarea in which the user puts his text using a simple editor powered by jwysiwyg.jquery. The problem is that is need to clean this input of garbage code (link the one that comes with pasting directly from Word).
The form is validated with the form_validation library from CodeIgniter, with this rule:
array(
'field' => 'job[description]',
'label' => 'Description',
'rules' => 'trim|required|callback_clean_html'
),
Then I have a clean_html method that simply does a:
return strip_tags($text,'<a><p><br><strong><em><h3><h4><h5><ul><ol><li>');
The problem is that this is simply ignored and the original text gets inserted in the database. The method runs (I've tested). I asume it's because a callback should return TRUE or FALSE, but then xss_clean doesn't return a BOOL. The documentation isn't much help.
Any thoughs?
Thanks in advance.
I think form_validation callbacks do need to return a bool. I find that form_validation is most useful when you need to display an error message to a user usually to resubmit the form. Although the prepping functions can be convenient, they don't need to be there to validate. Why not pass the submitted string through the strip_tags function after the form is submitted but before you send it to your db?
Have you tried removing callback_ in the rule? You can do regular PHP functions like trim so this should work.
Something aI always do just to be double safe, after setting the rules for the input I also run them through this
`$string = filter_var($string, FILTER_SANITIZE_STRING);`
That will strip out the html
I too have run into situations lately where the input totally ignores the rules that have been set.
xss_clean and other CI validation functions return non-bool values. I just tested the following callback function in CI version 1.7.2:
function test_string_change($str)
{
return "$str **";
}
The string was changed successfully using callback_test_string_change. I know there were some issues with the callback functions in 1.7.0, are you using the latest version?
From what you posted, it should work. Both the "callback_" prefix and the return are correct. Validation methods can return non-bools, which will replace the value. Check the form_validation documentation, it explicitly says that.
So your problem must be in some place of the code that you didn't post.
With the new version of CodeIgniter; you can only set rules in a static form_validation.php file. I need to analyze the posted info (i.e. only if they select a checkbox). Only then do I want certain fields to be validated. What's the best way to do this, or must I use the old form validation class that is deprecated now?
You cannot only set rules in the config/form_validation.php file. You can also set them with:
$this->form_validation->set_rules();
More info on: http://codeigniter.com/user_guide/libraries/form_validation.html#validationrules
However, the order of preference that CI has, is to first check if there are rules set with set_rules(), if not, see if there are rules defined in the config file.
So, if you have added rules in the config file, but you make a call to set_rules() in the action, the config rules will never be reached.
Knowing that, for conditional validations, I would have a specific method in a model that initializes the form_validation object depending on the input (for that particular action). The typical situation where I've had the need to do this, is on validating shipping and billing addresses (are they the same or different).
Hope that helps. :)
You could write your own function which checks whether said checkbox is selected, and applies the validation manually.
function checkbox_selected($content) {
if (isset($_REQUEST['checkbox'])) {
return valid_email($content);
}
}
$this->form_validation->set_rules('email', 'Email', 'callback_checkbox_selected');
If you want to avoid writing your own validation function, I came across this site which suggests that, if you're dynamically setting your rules using the Form Validation class, you can simply build up the rule string argument to set_rules() dynamically.
You first test the POST data to determine if your condition is satisfied (eg. checkbox selected) and then, as necessary, add a "|required" to the rule string you pass to set_rules().