My viewmodel has a ko.observable members that store dialog state objects. Each dialog object has all sorts of members corresponding to input fields in a dialog. I would like to add validation to the dialogs using the KnockoutJS validation plugin.
I don't, however, want to add validation to my entire view model, but rather just to my dialogs. When I tried extending the dialogs like this:
this.dialog = ko.observable(new RegistrationDialog(self)).extend({validatable: true});
things didn't work right: the isValid() and errors() methods were not defined, and validation wasn't working properly. I've created a jsfiddle to illustrate this. When I press the start button, the dialog opens (pardon the lack of CSS), but pressing enter doesn't generate any error messages. email validation also fails to work, showing the message 'true is not a proper email address.'
I would go with the documentation:
this.dialog = ko.validatedObservable(new RegistrationDialog(this));
Then fix some bugs in your fiddle:
data-blind
click handler on the form (instead of submit, I assume)
on the register button you have enable: isValid which should be enable: isValid() (I also removed the click binding because of fixing the form submit binding)
I think that was it. Updated fiddle here
Here is a working version:
http://jsfiddle.net/t9zLH/8/
I added an errors group inside and check it before allowing your register method to succeed, otherwise I show the errors.
Related
The text in ckeditor field is not sent when submitting forms for the first time (only on the second time, third time, etc).
For example, If try to create an article's post and submit the form I'll get a validation error: 'The field body is required'. If try to submit again (for the second time or third time), It will work well.
The real problem is when editing! For example, when editing a form the field 'body', among others fields, is filled out with the data from the database. In other words, there are already text in the ckeditor field.
If I try to submit the form for the first time it will not update the body because the text in the ckeditor is not sent; what is sent is the default value (the old article's body, which was filled out with data from the DB).
Therefore, it won't edit unless I get a validation error in other field (if I get a validation error, I'll have to submit again, and that will work).
How to solve this problem? Is this an known bug in CKEDITOR 4? If I don't solve it the users will feel frustrated if they have to submit the form at least twice to edit or to create an article.
Here is a list of plugins I'm using (may be useful to solve the problem):
a11yhelp, about, api, autocomplete, autocorrect, browser, clipboard, colordialog, copyformatting, crossereference, dialog, div, docprops, find, googlesearch, image, link, liststyle, magicline, mathjax, openlink, pastecode, pastefromword, preview, quicktable, scayt, section, showblocks, sourcedialog, specialchar, table, tableselection, tabletools, tabletoolstoolbar, texttransform, widget, wsc
By the way, I downloaded ckeditor using ckeditor builder in their official website.
I opened this issue in GitHub and a guy figured out the problem. His proposed solution worked wel!! Here is what he said:
Workaround
As the issue is more tricky to fix than it seems, for now I propose a
simple workaround: invoke ajaxRequest not on $( document ).ready,
but rather on editor's loaded event:
CKEDITOR.replace( 'editor', {
on: {
loaded: function() {ajaxRequest();}
}
});
Explanation of the issue
The issue is connected with how DOM listeners are registered for given
element:
The order of event listeners for a particular event type will always
be:
The event listeners registered with addEventListener() before the first time the event handler's value was set to non-null
Then the callback to which it is currently set, if any
Finally, the event listeners registered with addEventListener() after the first time the event handler's value was set to non-null.
In case of CKEditor 4, the value of the form's element is modified by
editor._attachToForm private method, which adds event listener to
form's submit event:
ckeditor-dev/core/editor.js form.on( 'submit', onSubmit );
However this listener is added on loaded event, which is fired
asynchronously when editor is loaded – so after registering
synchronous onsubmit handler with the validation logic. This way
editor's field is updated after validating.
Proposed solutions
Update editor's element on formdata event. This way we would have
total control over data being submitted and we would be sure that
correct data is set before submit event. The problem with this
solution is the fact that browsers' support is non-existent; the event
will appear in Chrome 77, however it is still not known if and when
the support will appear in Firefox or Safari.
Update editor's element on every change in the editor's content thanks
to change event. This solution will also fix cases, where some other
scripts are using value not from the editor, but directly from the
replaced textarea – they would get fresh data more often then only
after submitting the form. However this solution requires #1364, which
connects with a pretty big refactoring.
NOTE: AjaxRequest is the function I was using to submit the form togehter with Jquery.
In OOB asset form save button is not there, but it is present in my instance.I want to hide this save button.As you can see there are many UI actions, how I can determine which one works an asset table?(all ui actions are on global table)
Please help me out....
As you have correctly pointed out, there are 4 global UI Actions with the name "Save". They all have different combinations of the true/false fields: "Form Button", "Form Context Menu", and "Show Insert".
You want to de-activate the one that has "Form Button" value of "True" and "Show Insert" value "true".
The only reason it is being displayed is because of the condition isAdvancedUI() is returning as true. This is because the system property 'glide.ui.advanced' has been set to true. By setting this to true, a number of useful options in the context menus, become visible as form buttons (Save, Insert, Insert and Stay). Unfortunately one of the save buttons that appears, is not very desirable, because there is already a "submit" for inserting new records.
This is why it is perfectly OK to disable this button globally.
Gordon's answer here (https://community.servicenow.com/thread/261454) is exactly what you're looking for. Identify and override the save buttons (ones with "form button" checked) and you'll be good to go.
Side note: Generally it's a good idea to avoid changing the out-of-box UI actions, especially for something simple like hiding the button. Using a method like adding a condition can result in you 'owning' the button and make upgrades more time consuming. That said, there is a mechanism called 'UI Action Visiblity' that can control visibility for views without modifying (and owning) the UI Action itself. I've used this in the past and it works well.
https://docs.servicenow.com/bundle/istanbul-servicenow-platform/page/administer/list-administration/concept/c_ControllingVisibilityWithRoles.html
You can accomplish this with a Client Script. Keep in mind that hiding the button is obfuscating the Save button rather than restricting the access.
I tested this on a personal instance and it worked.
Name: Hide save button
Table: Asset [alm_asset]
UI Type: Both
Type: onLoad
Active: Checked
Inherited: Checked
Global: Checked
Script
function onLoad() {
var items = $$('BUTTON').each(function(item){
if(item.innerHTML.indexOf('Save') > -1){
item.hide();
}
});
}
This will load when any form view loads that extends the Asset [alm_asset] table because the Inherited box is checked. This is important because you can have hardware assets, license assets, and so on.
When this runs, it will search each button for Save and then hide it if matched.
I noticed that on a log in view, when a user enters in their email address, and quickly tabs to the password field, the MVC3 required attribute validation fires. The code I have is quite large, but I have been able to reproduce it with a very simple page. It is similar to what follows:
I have a very basic view with a model attached. The model has 2 properties, both of which have the [Required] attribute. If you focus one, and hit any key that does not input any characters(eg, Shift, Esc, etc.), the required validation fires, and I get my error message saying that the field is required, even though I have not entered anything yet.
Does anyone know how to prevent this early validation from firing?
Concur with gdordon - disable/fix any jquery validation. Also, you can disable this behavior by disabling Unobtrusive Validation (in web.config) - this is typically enabled and uses jquery for client-side validation.
So what I did was I disabled the keyup event for these input fields, using the following code:
<script type="text/javascript">
$(document).ready( function() {
$("input[data-val-required]").keyup(function () {
return false;
});
});
</script>
This allows for the client side validation to only go off when the user hits the submit button
Even using the Post/Redirect/Get method, and including javascript to disable a button after it has been clicked, I am having a problem with users being able to just rapidly hammer a submit button and get multiple form posts in before server side validation can stop it.
Is there any way to stop this? I've even tried this method : how to implment click-once submit button in asp.net mvc 2?
And I've tried outright blocking the UI with jquery blockUI. I have BOTH client side and server side validation in place, and they work perfectly - but a user smashing the submit button twenty times in under a second just seems to keep breaking it.
Use javascript to wire the onclick event to disable the button.
If you are already doing that and you can still get multiple form posts, then the problem is a delay between the clicking of the button and the button being disabled, and you must be submitting the form multiple times during this delay.
To fix this, make the onclick event first make a call to stopPropagation() to stop the submit event. Then validate that the form is not in submission-blocked state. You can do this by creating a page-scoped javascript variable with a boolean value like can_submit. Test for can_submit being true before submitting the form. Set the can_submit = false when the button is disabled, so even if the button is not disabled fast enough, the form will not submit if the value has already been set to false.
In most cases I'd say that this isn't worth fixing - if a user is going to do something as silly as clicking submit 20 times they should expect to get an error.
The only real fix for this is to set up your action to only accept the same form once - add a hidden field that is set to a random value when the form is loaded. When the form is posted, save that value somewhere temporarily and if it is already there you have a duplicate request that shouldn't do anything.
Currently, a spring application I am working on has several wizards that it is using with Spring's AbstractWizardFormController. During the early stages of development(pre-design phase), the type of "next" button did not matter.
Just to refresh, the Next and Back button are submit buttons with target attributes. So a next button on the first page of a wizard would look like the following.
<input type="submit" name="_target1" value="Next"/>
This is the standard way Spring does wizards on the view. This works fine, given that you want your Next button to be a standard HTML submit button. Otherwise, in my case, If I want a custom button, I am not sure how to do this. I know it is possible, but haven't found any documentation.
I imagine I will need to do a javascript submit, but I am not sure how to set the name of the button, of if something else needs to be done.
I just need to know how I can still extend AbstractWizardFormController, and use custom buttons.
When clicked, HTML submit button submits a form with additional parameter {name}={value}, that is _target1=Next. I guess the value doesn't matter here, controller looks at the name. So, if you want to emulate this with Javascript, you may, for example, dynamically add a hidden field with name = "_target1" before submit.