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
Related
I need to check the presence of any email within a text in a textarea.
If there's any email present the form should fail.
I did regex to fail if there is no email present regex:/[a-zA-Z0-9._%+-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}/ and it should be easy to do the negation but I'm not having any success.
Add this code into AppServiceProvider boot method:
Validator::extend('not_regex', function($attribute, $value, $parameters, $validator) {
if (!is_string($value) && !is_numeric($value)) {
return false;
}
return !preg_match($parameters[0], $value);
});
And then just use your regex like this:
not_regex:"/[a-zA-Z0-9._%+-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}/"
I think the best way to solve this is to create your own custom validation rule. This way you can solve this problem using nicely formatted code.
Laravel comes with an "email" validation rule which is very easy to implement. You can read all about it, and other validation methods in this link.
I am making an application where users can upload questions, and questions can have multiple correct answers. The correct answers have names of the form correctAnswer1 correctAnswer2 etc.
I want to know how to require all submitted fields matching this pattern; I was thinking of using something analogous to
/correctAnswer[0-9]/ => 'required'
I'm not sure of the logic behind your requirement, kinda seems you should do things in a different manner, but again I do not know how your app works so I can't be a judge of that. So if the user can add new correct answers fields on the form, and you wan't them to not be empty it makes some sense.
You can't have a regex in the rule name but you can do the following:
$rules = [
// your other rules
];
$correctAnswers = preg_grep( '/^correctAnswer[1-9]{1}$/', array_keys($this->all()));
// use $this->all() when in Http\Requests\YourRequest
// if you are not using the request method of validation (you validate in controller)
// simply replace $this->all() with $request->all() or Input::all().
foreach ($correctAnswers as $correctAnswer) {
$rules[$correctAnswer] = 'required';
}
return $rules;
This assumes you are using the Laravel 5, Http\Requests to validate your input. If you are doin'g the validation elsewhere (in controller for example), just replace $this->all() with $request->all() or Input::all(). I can't give the exact choice as I do not know exactly how you do the validation and what version of laravel you use.
PS: This will match only correctAnswer1 to correctAnswer9. If you want more just play with the [0-9]{1} part of the regex.
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).
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.