Get value of editbox of type date/time - validation

I try to get the value of an editbox of type Date/Time. If I test it with
getComponent("dateField").value
or
getComponent("dateField").getSubmittedValue();
and print the output to the console. It always returns "null" if the field is empty or
the field does not contain a valide date. Because of this I can't differ between invalide input and empty input.
Is there a way to get the information if the field is empty?

It depends on the refresh phase you're testing.
getValue() will always return blank, because only content that can be converted to the underlying data type will be passed to it. Even if you disable validation, converter checks still run, because serious errors will occur if you try to put "this is not a date" into a Date/Time.
getSubmittedValue() will always be null if you're checking in Invoke Application or Render Response phases. That's because during the Update Model Values phase, the submittedValue property is passed to the value property and the submittedValue property nulled.
If you're checking in a validator, the text value entered by the user has not yet been checked against validation rules (validation) or that it can be converted to the right data type (conversion), so getValue() will return the value stored last time round and getSubmittedValue() will give the string value (e.g. "this is not a date").
So the answer is you should be able tell whether the field is empty in a validator, but bear in mind custom validators only run if you also have a required validator.

Related

Optional Input parameters in CRM action always NULL in Code Activity even if not passed?

If I ommit an optional input parameter in a CRM action call, will this parameter always be null in the Code Activity?
We have a customer which calls a particular CRM action, lets say an update action. The customer wants to be able to pass input parameters as null if the value in the corresponding field in dynamics must be deleted.
the problem i an facing now is that I cannot detect wether the input variable was effectively passed like "parameter_1 = null" or if the parameter itself was not even passed in the action call. the issue is that I cannot delete the value in crm if the input parameter was just not passed. only if the parameter was passed with the value null, I am allowed to delete the field value in crm.
Am I correct assuming that the value of a optional action input parameter is also null if the parameter is not passed at all?
Is there maybe a workaround which enables me to detect wether the value of the input parameter was passed as null instead of beeing ommitted?
something like "undefined" or similar?
You are correct. Input parameters are always present in the IPluginExecutionContext.InputParameters collection passed to the plugin handling your action.
You would need an extra "MyParameterNameSpecified" so to speak to signal if the parameter null value was actually passed.
Another option could be to use a string parameter holding the value passed in JSON-serialized form.

Sitecore Item Validators

I created an item validator with the validation rule template. I'm using it to check if one field for a date is after another field for a date following this tutorial: https://sitecorejohn.wordpress.com/2010/03/17/validate-that-the-value-of-one-datetime-field-follows-another-with-sitecore. For some reason the item that is pulled with GetItem() in my validator does not have the change that the content editor has made until the item is saved. I thought Sitecore.Data.Validators.BaseValidator.UpdateItem would take care of this but it seems that my control to validate is null. That makes sense since it is an item validator instead of a field validator but that means that if this fires off on blur content editors may see false error messages or not see error messages when they should. I'm also running into an issue where my Evaluate method is firing twice on save; once before the item is actually saved so GetItem() returns with the non-updated values and once after save which has the expected values. If anyone has any insight as to why this might be happening I'd like to know. I have a feeling that the validator executing twice on save might be a config issue but I didn't see anything very obvious in the pipeline.
To get the new value of the field that's being validated you can use BaseValidator's
GetControlValidationValue();

Unique Validator - add error (warning) and return true

What would be the best way to create validator that checks if model value is unique or not, but it does not return false - it only shows message "the value already exists" (I can still save the model)?
Validators usually don't return boolean values, they add errors for given model attribute(s).
One of the ways (with minimal completions) will be using built-in UniqueValidator and saving without running validation.
At first call $model->validate() to fill model with errors.
You can use $model->validate('fieldName') to validate only needed field.
Then call $model->save(false) or $model->save('fieldName') (for just one field).
This will prevent validation before saving and model values will be saved "as is".
Another way for just saving one attribute without triggering events, etc. will be using updateAttributes after calling validate():
$model->updateAttributes(['fieldName' => 'fieldValue']);

Spring MVC: Set property to null when bind fails

Is it possible to configure Spring to set the target property to null when binding fails?
For example, my bean has a date property which can have a pre-existing value when the form is displayed. If the user enters an invalid date, I want the property to be set to null; currently it retains its previous value.
I still want the binding error of course, so I can display an error message.
I'm hoping there's a general configuration solution for this (ie. I don't want to just write code to deal with each field manually!).
Background: Retaining the old value causes odd behaviour in subsequent custom cross-field validation. Setting the property values to null would tell this validation not to execute.

Backbone.js: run validations and fire error events on set, but don't abort set if validations fail

I've got a Backbone model with a custom validate method that validates the format of one of a the model's attributes. My model is hooked up to a view that exposes said attribute via a text field. The view has a 'save' button that the user must explicitly press to save the model changes back to the server.
When the user types an invalid attribute value, I want to visually mark the field as being in an invalid state. So far, easy - I can bind the change event of the input field to a function that calls myModel.set({ attribute: value }), and listen for the "error" event on the model to tell when the validation has failed and I should mark the input as invalid.
The problem comes when I want to handle the save button click. Because Backbone.Model.set aborts actually setting the attributes on the model if validation fails, my model will not accurately reflect the value the user has entered unless the value is valid. When the user clicks save after typing in an invalid value, I check whether the model is valid, find that it is (because the invalid attribute was never actually set), and save the old (valid) attribute value to the server.
What it seems like I want is a version of set that always makes the requested changes, but also still triggers validations and events. set(..., { silent: true }) will allow the change to go through, but will not run validations or trigger events.
In short - I want my model to sometimes exist in an invalid state (if the user has entered invalid attribute values), and I want to be able to get events when it transitions between valid and invalid. Is there a graceful way to do this with backbone, or am I thinking about this completely wrong?
I suggest you use this backbone validation plugin https://github.com/thedersen/backbone.validation
Extremely helpful.
For your usecase (to allow values to be set even if they are invalid),
you just need to do override the set function of your model.
set: function (key, value, options) {
options || (options = {});
options = _.extend(options, { forceUpdate: true });
return Backbone.Model.prototype.set.call(this, key, value, options);
}
The 'forceUpdate' property of backbone.validation allows the validate method to return true, while still calling for validations.
After the save click, you can call,
var errors = model.validate(model.validate.attributes);
This will return all the errors that your model has and you can appropriately display them.
You also have callbacks for valid and invalid which you can use freely
What I've done with this sort of validation is to reset the models attributes from the inputs before save on the save click (only doing the save if the set doesn't fail)
This way the save button click re triggers the validation - triggering the error.
It means the model is always valid and you cant progress to the next page until the input is all valid.

Resources