I have a required input which is connected to a model.
Now i want to show an error message if the required field is empty. But only if the input has been changed or the form gets submitted. So initially there should be no error even if the input is empty.
My current solution is a property named "hasChanged" which gets set by an observer on init.
http://emberjs.jsbin.com/tovawezide/2/edit?html,js,output
Is there a shorter solution with less boilerplate in ember? like an build in "hasChanged"?
I think my way gets confusing with more inputs.
Note: im not talking about ember-data or its isDirty property nor do i ask for validation libraries.
We have been using ember validations together with easyForms quite succesfully for this sort of task.
export default Ember.ObjectController.extend({
validations: {
requiredField: {
presence: true,
}
}
});
-
{{#form-for model}}
{{input requiredField}}
{{submit value="Submit" class="button"}}
{{/form-for}}
Related
I'm trying to manually set an error on a computed observable using Knockout Validation but the validation message isn't displaying. I need to be able to set the error after apply bindings has been called and the group set.
var viewModel = {
computedTest: ko.computed(function(){
return 'Test'
})
};
viewModel.errors = ko.validation.group(viewModel);
ko.applyBindings(viewModel);
viewModel.computedTest.extend({ validatable: true });
viewModel.computedTest.setError('oops');
viewModel.errors.showAllMessages(true);
Using this example a validationMessage doesn't get displayed for the computedTest observable.
I believe the reason is because the validation group hasn't doesn't know that computedTest is now extended. But I'm not sure how to refresh the group so that the error message is displayed.
Here's a better example: https://jsfiddle.net/onbyc67h/.
As you can see if you set the .extend({ validatable: true }) before applyBindings is run a message is displayed, but if you do it after one isn't.
Thanks
What is going on is completely logical: when you apply bindings, the different bound properties are subscribed to changes of existing observables. So, if you create a new observable after binding, there is no way for ko to discover and subscribe to it. Take into account that what the validation extenders do is creating new observables, which can be subscribed. But, if you create them after binding, as explained, they can not be subscribed by the binders.
The only thing that you could do would be to unbind and rebind, but this is not advisable at all.
I'm having a problem while trying to validate and save domain object.
First I'm making the validation, when the validation is wrong, I'm putting error inside my future to be saved object, like this:
myDomain.errors.reject("An Error")
myDomain.discard()
Then, when I'm trying to save this object, I can see that error list has one error but 'validate()' returns 'true', also, when the function is finished the object is being saved automatically.
I must say that all the called functions are in the same class which is a controller.
I need to know how to code my save function (in the controller class) which shows only the error without saving the object, and when the validation is good, to save the object.
Thanks!
myDomain.validate() will overwrite myDomain.errors clearing the reject(). You could do something like this:
// bind etc.
...
// do grails validation
if (!myDomain.validate()) {
myDomain.discard()
}
// do custom validation
if (custom validation has errors) {
myDomain.errors.reject ()
myDomain.discard()
}
If you can move you custom validation into a validator on myDomain you do not need the custom validation.
You don't need to call save() explicitly.
The fix was to use:
#Transactional(readOnly = true)
For the function which located in the service file.
It makes the function to avoid from saving transactions in the end of it, which caused the problem in the first place.
I've setup a handsontable with a csv data source. After adding some validation rules everything seems to work fine.
I used the 'afterValidate' event to inform the user if the cell they just edited failed validation. However, I want to run validation on all cells so if there are any errors the user is notified.
afterValidate: function (isValid,value,row,prop,source) {
if(isValid) document.getElementById('validated_label').innerHTML = 'Working';
else document.getElementById('validated_label').innerHTML = 'Not Working';
},
EDIT: Managed to solve by using the validateCells method available in the API :)
Gah.. I have spent way to long on this, but I believe I have found the problem.
Essentially I have a hidden field which is populated when a user clicks on an image.
It is required that the user has clicked the image but I do not want the generic form error message for a 'required' check with the CI form validation class.
As such I quickly made a image_required function in my extended form validation class, and set a rule such that this rule was applied to the hidden field.
function image_required($str)
{
$CI =& get_instance();
$CI->form_validation->set_message('image_required','Please click the image above.');
if($str != '')
{
return TRUE;
}
else
{
return FALSE;
}
}
If the hidden field was blank no error was being called.
I am led to believe now that this is because CI says this field is empty yet it is not 'required', therefore we will ignore all the other validation rules for the field. Is this correct?
If so how can i go about requiring this field be set but having a custom error message?
bangs head
Thanks
If you look at the source code (v2.1.3) for the '_execute' routine (system/libraries/Form_validation.php) you will see on line 486
// If the field is blank, but NOT required, no further tests are necessary
So you are correct, it needs to be required and then it will process your rule.
In order to fix it so you can have a non-required blank field that still processes rules, you should override the '_execute' method by creating a file called 'MY_Form_validation.php' in the application/libraries folder (I think, you might need to check exactly how you extend an existing library) and then copy the '_execute' method and alter the code to continue on a non-required but blank entry.
I do love CI, but I have to say this does not allow the flexibility required. It is perfectly reasonable to have a field that cannot be empty, but is NOT required. As in, you wouldn't enforce "user MUST enter a value", but they cannot submit a blank. I think someone got confused between EMPTY and REQUIRED.
1) REQUIRED: User MUST put a value in the field and it cannot be empty (i.e. '')
2) EMPTY: User does not HAVE to enter a value, BUT, if they do, it's cannot be empty. This not the same as REQUIRED... Looks like I'll be using a callback again.
REQUIRED incorporates two logical steps (1->Must enter a value, and 2->Cannot be empty) these two steps should be separated logically to allow either / or.
In constraint terms it would be either, REQUIRED, NOT NULL. Or NOT REQUIRED, NOT NULL.
I would like use data annotations to handle validation in my Silverlight app. The built-in validation attributes (primarily StringLength and Required) are great, and make life very easy. However, they seem to have one critical flaw. If my locale is set to fr-CA, for example, the validation exceptions are still in English - 'The Name field is required', 'The field Name must be a string with a maximum length of 20', etc.
This is a major problem. It means that if I want localized error messages for the built-in validation attributes, I have to manually add ErrorMessage/ErrorMessageResourceType to every validation attribute on every validatable property in my business layer, and manually add translated strings for every error message.
So... am I missing something here? Is there a way to automatically have the built-in validation attributes localized? Or some other easier way of doing this? Or am I just completely out of luck, and stuck with the manual route?
Any comments or thoughts would be appreciated.
Ok, I got around this by simply subclassing the built-in validation attributes. Problem solved!
internal class LocalizedStringLengthAttribute : StringLengthAttribute
{
public LocalizedStringLengthAttribute(int maximumLength)
: base(maximumLength)
{
}
public override string FormatErrorMessage(string name)
{
return String.Format(CultureInfo.CurrentUICulture, LanguageResources.Resource.Error_StringLength, name, MaximumLength);
}
}