I'm using the following rule for my input:
callback_validate_host
I need to make the following condition:
if callback_validate_host is FALSE afterwards it should use the valid_ip validation rule.
So if validation of both: callback_validate_host and valid_ip on one input if FALSE then is should throw an error message.
How can I do that?
How about using your existing validate_host() method in conjunction with the Input class' $this->input->valid_ip($ip) method to create a single callback? Something like this:
public function your_custom_rule($input) {
if (! $this->validate_host($input) && ! $this->input->valid_ip($input)) {
// validate_host() returned FALSE *and* it's not a valid IP
$this->form_validation->set_message('your_custom_rule', 'Error msg');
return FALSE;
} else {
return TRUE;
}
}
Related
I am trying to include the below condition into my test but I can't seem to get it to work and receive the below error, any ideas why?
Essentially, I want to test that a input is / is not empty:
cy.get(`[class^='input-module_field']`).eq(0).then(($input) => {
if ($input.should('have.value', '')) {
cy.get(`[class^='input-module_field']`).eq(0).should('be.visible').type(foo)
cy.get(`[class^='input-module_field']`).eq(1).should('be.visible').type(bar)
cy.get(`[class^='input-module_field']`).eq(2).should('be.visible').type(foo-bar)
cy.get(`[class^='input-module_field']`).eq(3).should('be.visible').type(foo-bar-foo)
} else {
cy.get(`[class^='input-module_field']`).eq(1).should('be.visible').type(bar)
cy.get(`[class^='input-module_field']`).eq(2).should('be.visible').type(foo-bar)
cy.get(`[class^='input-module_field']`).eq(3).should('be.visible').type(foo-bar-foo)
}
})
The error i get:
$input.should is not a function
When yielded from a .then(), the $input variable is just a JQuery element, and can't use Cypress commands. In this case, even using a Cypress command, such as .should() wouldn't work, because that does not yield a boolean value for the if/else.
Instead, we'll want to use JQuery's .val() function.
...
if ($input.val()) { // if $input.val() returns an empty string, this evaluates to false
// code to run if the $input element has a value
} else {
// code to run if the $input element does not have a value.
}
...
Note: I reversed the order of what you had, with the $input.val() === '' being the else, instead of the if.
The check can be on the value itself
cy.get(`[class^='input-module_field']`).eq(0)
.then(($input) => {
const field0Val = $input.val() || 'foo' // if empty use "foo"
cy.get(`[class^='input-module_field']`).eq(0).type(field0Val)
cy.get(`[class^='input-module_field']`).eq(1).type('bar')
cy.get(`[class^='input-module_field']`).eq(2).type('foo-bar')
cy.get(`[class^='input-module_field']`).eq(3).type('foo-bar-foo')
})
I would like to check a FlexForm field with the additional behaviour: If the entered value is not correct, it should not be possible to save the form. This is a similar behaviour as the "required" eval function, which refuses to save empty fields.
The code for evaluation already exists (but I am not adding entire code):
class UsernameEvaluation
{
public function evaluateFieldValue($value, $is_in, &$set)
{
if ($value) {
$errorCode = StudipPerson::checkUsername($value);
// if wrong username, should not be possible to save form
if ($errorCode != StudipPerson::USERNAME_ERROR_OK) {
$set = false;
}
}
return $value;
}
}
Even though invalid data is entered and I checked with a debugger that $set was set to false, the value is saved.
I was trying to check the validity of individual attributes using isValid method. It is returning true for an invalid attribute. My code is as follows:
person = Backbone.Model.extend({
defaults:{
name:"default name",
age:0
},
initialize:function(){
this.on("invalid",function(model,errors){
console.log(JSON.stringify(errors));
});
},
validate:function(attrs){
errors=[];
if(attrs.age<0){
errors.push({attribName:"age",errorMsg:"age should be grater than 0"});
}
return errors.length>0?errors:false;
}
});
var person1 = new person();
person1.set({
age:-5
});
console.log("checking validity of model:"+person1.isValid());
console.log("checking for validity of age attribute:"+person1.isValid('age'));
isValid() works fine if used to check the validity of the model as a whole and returns false. But when I try to check the age attribute i.e isValid('age') it returns true when it should return false.
isValid() is an underscore.js function, right? Doesn't it support passing an attribute to check for its validity? What am I missing here?
Short version
Model.isValid doesn't accept an attribute name as argument and has to be used on the whole model. If you don't, you're on undocumented territory and you will get weird behaviors.
To check individual attributes, you will have to set up your own mechanism.
Long version, why you get a different value
Model.isValid does in fact accept an (undocumented) options hash as its first argument and it internally forwards this hash to Model._validate via
this._validate({}, _.extend(options || {}, { validate: true }))
trying to set a validate attribute to true. But at this point, options is a string and won't be modified by _.extend. _validate looks like
_validate: function(attrs, options) {
if (!options.validate || !this.validate) return true;
// ...
}
checking if it indeed has to validate the model, options.validate is undefined and your isValid call gets back a true value.
isValid is a backbone API : http://backbonejs.org/#Model-isValid
The reason it is returning true is, the parameter accepted by isValid is an options paramter. It has to be an object.
One of the scenario you use options is :
validate: function(attrs, options) {
if(options.someSpecialCheck) {
// Perform some special checks here
} else {
// Perform some regular checks here
}
}
myModel.isValid({someSpecialCheck: true});
I have a situation where I am creating an unobtrusive validator that must validate that another field is required only if the validated field is not empty (and vice versa). The problem is that there are some edge cases where the other field does not re-validate, and I would like to force it to revalidate itself without causing an infinite loop.
My validation method looks like this:
$.validator.addMethod("jqiprequired", function (value, element, params) {
if (!this.optional(element) || (this.optional(params) && this.optional(element))) {
return true;
}
return false;
});
params is my other field (both are textboxes). If both are empty, it passes, if both have values, it passes. It only fails if only one has a value.
This works fine, except that if one field is empty, and another has a value, then you delete the value from the field with a value, the empty field is not revalidated (because it's value has not changed).
I tried doing this:
if (!this.optional(element) || (this.optional(params) && this.optional(element))) {
$('form').validate().element(params);
return true;
}
But this causes an infinite loop because each time it passes, it calls the other.
How can I cause the other field to validate, without itself calling the original field?
Instead of adding an attribute to each field, try adding a variable jqip_validating in the script where you are adding this validation method. Then, change your validation as follows:
var jqip_calledFromOtherValidator = false;
if (jqip_validating) {
jqip_validating = false;
jqip_calledFromOtherValidator = true;
}
if (!this.optional(element) || (this.optional(params) && this.optional(element))) {
if (!jqip_validating && !jqip_calledFromOtherValidator) {
jqip_validating = true;
$('form').validate().element(params);
}
return true;
}
In order for the other validator to be called, both conditions must be satisfied, and they can only be satisfied when the first validator invokes the second validator.
You can add a is_validating attribute to each fields so that, if it's on you skip the validation and if not, you set it to true, do your validation and then clear it.
I simply need to add a validation class that limits a numerical entry from being greater than 24.
Is this possible with CI's default validation classes or will I have to write a custom validation class?
You can use validation rule "greater_than[24]"
like for Example
$this->form_validation->set_rules('your_number_field', 'Your Number', 'numeric|required|greater_than[24]');
There's no maximum or minimum comparison function in the Form Validation Rule Reference, so you can just write your own validation function.
It's pretty straightforward. Something like this should work:
function maximumCheck($num)
{
if ($num > 24)
{
$this->form_validation->set_message(
'your_number_field',
'The %s field must be less than 24'
);
return FALSE;
}
else
{
return TRUE;
}
}
$this->form_validation->set_rules(
'your_number_field', 'Your Number', 'callback_maximumCheck'
);
Sure you can, just make your own validation function and add it as a callback to validation rule. See http://codeigniter.com/user_guide/libraries/form_validation.html#callbacks
Hence, you will have
...
$this->form_validation->set_rules('mynumber', 'This field', 'callback_numcheck');
....
function numcheck($in) {
if (intval($in) > 24) {
$this->form_validation->set_message('numcheck', 'Larger than 24');
return FALSE;
} else {
return TRUE;
}
}