creating a salesforce validation rule for one of the object - salesforce

I am creating a salesforce validation rule for one of the object which is related to Date field.
OR((Delivery_Date_From__c < TODAY()+1), (Placement_Date_From__c < TODAY()+1), true, false)
This gives me a validation, but when i remove the true, false from the validation formula, it doesn't give me any validation.
Thanks in advance.

By adding true to the OR function you are forcing it to always return true. Validation Rules cause an error when they return true, thus users will never be able to save a record. Instead try this:
OR(
AND(ISCHANGED(Delivery_Date_From__c), Delivery_Date_From__c <= TODAY()),
AND(ISCHANGED(Placement_Date_From__c), Placement_Date_From__c <= TODAY())
)
This way the comparison only happens if the date is actually being edited, and if the date is before or equal to today it will return true, causing validation to throw an error.

Related

yup validation hierarchy issue (formik vs react-hook-form with yupResolver)

I am working on converting forms from Formik over to React Hook Form. yup is the validator in both cases. However I've noticed that despite leaving the validation schema in tact, there seems to be some sort of hierarchy mismatch between Formik and RHF.
I noticed for instance that for min, max, required, originally in that order, validation was working correctly with Formik, but I had to invoke required before min & max in order for the correct validation message to trigger on an input. That fix was self-explanatory enough.
But some of these are tricky. For instance:
dob: yup.date().when([], (schema: yup.DateSchema) => {
return boolCheck1 && boolCheck2
? yup.string()
: schema
.min(
moment.utc().add(1, 'day').subtract(126, 'years').toDate(),
'an error message'
)
.max(
moment.utc().subtract(15, 'years').toDate(),
'another error message'
)
.transform((_, originalValue) => {
const date = moment.utc(originalValue, 'MM/DD/YYYY');
return date.isValid()
? date.startOf('day').toDate()
: new Date('');
})
.typeError('A valid date is required (MM/DD/YYYY)')
.required('Please enter a valid date');
})
It seems that regardless of where I am placing the required check, the one that is triggered is typeError ("A valid date is required (MM/DD/YYYY)"). Basically, if a user touches and blurs this input field without having typed anything, it should trigger the required message ("Please enter a valid date".) Only when a user types something that does not pass the typeError check should the typeError message appear.
This was working previously with Formik. However, now, I'm using React Hook Form as such:
const rhfMethods = useForm({
defaultValues,
resolver: yupResolver(validationSchema),
mode: 'onTouched',
});
The only difference really is that now the yupResolver is being used (without which I get a typescript error.)
If anyone could shed any light on this that would be greatly appreciated!
Nvm, looks like a similar question had already been asked and answered.
While that definitely helped shed light on the issue ('' is still a string type, not simply no data), what helped me solve this was this addendum.
I still needed the initial '' to be a valid input type, so instead of returning an Invalid Date (new Date('')) in the else case, I just needed to return undefined.
Final fix:
dob: yup.date().when([], (schema: yup.DateSchema) => {
return boolCheck1 && boolCheck2
? yup.string()
: schema
.min(
moment.utc().add(1, 'day').subtract(126, 'years').toDate(),
'an error message'
)
.max(
moment.utc().subtract(15, 'years').toDate(),
'another error message'
)
.transform((_, originalValue) => {
const date: moment.Moment = moment.utc(
originalValue,
'MM/DD/YYYY'
);
return date.isValid() ? date.startOf('day').toDate() : undefined;
})
.typeError('A valid date is required (MM/DD/YYYY)')
.required('Please enter a valid date');
})
(To be fair... I tried testing on the UI and there was no instance where this .typeError was ever triggered (even with Formik) since every allowable input instance (only numbers can be typed and they're auto-formatted to dates) was already being checked by the other tests... I think I could safely remove that typeError check altogether lol.)

Warnings in AngularJs

Angularjs has great infrastructure for form validation and showing error messages. But, I am in a situation that I have to show a warning message to a user in a specific scenario. Here is the diagram of my simple form
The form has required and pattern validation applied on both fields. In addition to this validation I want a warning message to be displayed to the user if VatAmount is not 20 percent of the InvoiceAmount. The warning will differ from validation in following aspects
It will not prevent the form submission
It will only appear if both fields (InvoiceAmount and VATAmount) are
valid
The warning should have a button or link that would read "Change and
proceed". When user presses that button the warning message will
hide and focus will be set to VATAmount field.
I believe this is a prefect use case for creating a directive. Actually, I have given it a try and put my effort in the form of a plunker. But my directive does not handle following cases
It appears even if the fields involved in warning are invalid
The show and hide functionality is not implemented (have no idea how
to target it)
Here is the link to the plunker
Your plunkr demo was on the right track; really you just needed to check for the special cases of when one of the values was empty.
I'd suggest calculating the fraction and storing it in the scope, and then watching that to see whether you should display your tax rate warning. Here's how to calculate the fraction. If either invoice.Amount or invoice.VAT is empty, the fraction will be set to null.
if (amt == null || vat == null) {
$scope.warning.fraction = null;
return;
}
$scope.warning.fraction = vat / amt;
This works because those properties will be set to undefined if the user doesn't enter a valid number due to your use of ng-pattern.
However, while it's nice to encapsulate this in a directive, you don't need to compile the template yourself. Just use the built-in ng-transclude directive. Then you can include a button that references the current scope like this:
<vat-warning>
Vat Amount is not 20%.
<button ng-click="invoice.VAT = invoice.Amount / 5">change</button>
</vat-warning>
Your directive would contain this declaration:
transclude: true,
template: '<span class="alert-warning" ng-show="warning.show" ng-transclude></span>'
Plus a controller to update the directive's local scope to manipulate the warning object. Here's an updated demo.
You need to calculate visibility of vat-warning tag in controller on basis of $error.required and $error.pattern of invoiceAmount and vatAmount and then use it as below:
$scope.isInvoiceAmountInvalid = function () {
var error = $scope.invoiceForm.invoiceAmount.$error;
var required = error.hasOwnProperty("required") && error.required;
var pattern = error.hasOwnProperty("pattern") && error.pattern;
console.log("Inside isInvoiceAmountInvalid", error, required, pattern);
return (required || pattern);
};
$scope.isVatAmountInvalid = function () {
var error = $scope.invoiceForm.vatAmount.$error;
var required = error.hasOwnProperty("required") && error.required;
var pattern = error.hasOwnProperty("pattern") && error.pattern;
console.log("Inside isVatAmountInvalid", error, required, pattern);
return (required || pattern);
};
Here is an updated plunker for the same

CakePHP v2.4.3 record update and validation rules

I'm trying to update one record with
$this->Model->id = 1;
$this->Model->save($this->request->data);
and to use validation rule 'on'=>'update'.
Is that possible and how :) ?
Solution to my problem was to remove validation rule for the field that i don't want to validate when i update record.
$this->Model->validator()->remove('fieldName');
$this->Model->id = 1;
$this->Model->save($this->request->data);

ExtJS findExact() and custom validator bug

I'm using a custom validator on my combobox's:
function(v) {
console.log(v === 'some value I know for SURE is in the store'); // (1)
var index = this.getStore().findExact(this.displayField, v);
return (index!==-1) ? true : 'Invalid selection';
}
Basically admits the same set as forceSelection but allows the user to type arbitrary text to attempt to auto-complete.
However; I'm having really odd results with findExact(). For example, if the combobox's value is currently valid, and a user does a space + backspace, the validator will fail, even though the output of (1) is true.
Any ideas what is causing the problem? The end-experience is currently very buggy-feeling..
When you type additional space, store is filtered. After you press backspace, and validator is fired, store is still empty.
If you have local store, then you could validate combo with some delay after each change. Example:
listeners: {
change: function() {
this.validate();
},
delay: 100
}
That should be enough.
On the other hand if you have remote store, try something like this:
validator: function(v) {
var store = this.getStore(),
index = store.findExact(this.displayField, v);
if (index === -1 && store.isLoading()) {
store.on('load', function() {
this.validate();
}, this, { single: true, delay: 100 });
}
return (index !== -1) ? true : 'Invalid selection';
}
I had a similar issue and found this entry. My problem was, that I reused the same store instance across multiple ComboBoxes. After giving each ComboBox an own store with cloned data, everything was fine.
See also:
https://www.sencha.com/forum/showthread.php?305182-ComboBox-forceSelection-clears-input&p=1127218&viewfull=1#post1127218
I just spent a few days on this issue and found a really great solution (by accident, really). You can - as the accepted answer suggests - utilize the provided validator function; however, from my understanding, there is a much simpler solution than the accepted answer provides: evaluating whether or not the user-provided input equates to a value in the store (which is the underlying question in the original post).
The advantage of thinking about the input in this way is that it enables us to handle the use case of an invalid value entered by the user (validated; no value) and - after the field loses focus - Ext JS sets the field back to its previous value (which remembers its store value).
This is an entirely different route than your thinking, but it should work, especially as .validate() runs regardless of whether you provide an implementation of the validator procedure:
validator : function(someParam) {
if(this.value === null) {
return "error message"; //falsy
} else {
return true;
}
}
If you enable forceSelection, the above works, very well, and gets rid of the buggy feeling. This allows you to rely on the .validate to do its magic elsewhere (notice I don't even call it; read the doc. to figure out when its called in relationship to validator) and not have to worry about what the user correctly explains in the accepted answer.
We were having trouble with forceSelection clearing user text before they were finished typing. We seemed to get what we needed by setting forceSelection false and just checking that they selected something.
validator: function(v) {
if (this.getSelection() === null) {
return 'invalid text';
}else{
return true;
}
}

ExtJS: default validation

95% of my fields for a form I have use a custom validator -> hence I include it as apart of my defaults config of the container.
However, for the remaining 5% of fields, I want to use the built-in validation. Is there a way to manually specify using the default validator for a given field type?
Currently I try,
{
name: 'normalNumberField',
xtype: 'numberfield',
validator: Ext.form.field.Number.validate,
...
}
but I'm getting an error that Ext.form is undefined (why? I clearly have a reference to Ext ... )
The validator is not specified by default. Number field validation occurs in multiple steps (this is taken from the ExtJS docs):
Field specific validator
A validator offers a way to customize and reuse a validation specification. If a field is configured with a validator function,
it will be passed the current field value. The validator function
is expected to return either:
Boolean true if the value is valid (validation continues).
a String to represent the invalid message if invalid (validation halts).
Basic Validation
If the validator has not halted validation, basic validation proceeds as follows:
allowBlank : (Invalid message = blankText)
Depending on the configuration of allowBlank, a
blank field will cause validation to halt at this step and return
Boolean true or false accordingly.
minLength : (Invalid message = minLengthText)
If the passed value does not satisfy the minLength
specified, validation halts.
maxLength : (Invalid message = maxLengthText)
If the passed value does not satisfy the maxLength
specified, validation halts.
Preconfigured Validation Types (VTypes)
If none of the prior validation steps halts validation, a field
configured with a vtype will utilize the corresponding
Ext.form.field.VTypes validation function. If invalid, either the
field's vtypeText or the VTypes vtype Text property will be used
for the invalid message. Keystrokes on the field will be filtered
according to the VTypes vtype Mask property.
Field specific regex test
If none of the prior validation steps halts validation, a field's configured regex test will be processed. The
invalid message for this test is configured with regexText
Number field specific validation
The additional validations run test that the value is a number, and
that it is within the configured min and max values.
So, long story short, to override your validator that you specified in defaults, you can simply use a validator which always returns true.
{
name: 'normalNumberField',
xtype: 'numberfield',
validator: function(){ return true; },
...
}

Resources