Parsley.UI.addError doesn't get messages disabled - javascript

I am using Parsley to validate some forms and the server is doing same validations in his side. When a constraint fail on the server, i call this function do add the error on parsley:
var invalidField = $('[name="' + param.target + '"]').parsley();
window.ParsleyUI.addError(invalidField, "remoteError_"+param.target, param.message);
Param.target is the name of the input field to invalidate. The error shown as li under the field, but for the parent form messages are disabled
<form id="ricarica_telefonica_form"
data-parsley-errors-messages-disabled>
<script>
$(function(){
$('#ricarica_telefonica_form').parsley();
});
</script>
Additionally i am using a custom message visualization to show errors trought bootstrap tooltip
window.Parsley.on('field:error', function() { some code });
but my code is not being executed (it works when a field is invalidated trought parsley). What am I doing wrong? Is the method .addError managed correctly by parsley?

I found the solution, the right way is
var instance = $('#field').parsley();
instance.trigger('field-error',instance,{message:errorMessage});
In this way the event is catch by the function that manage tooltip, where i use message attribute. The form is still valid for parsley, but at least i reached my scope.

Related

How to reformat an auto formatted number using JQuery

Hello Im fairly new to Javascript/JQuery. I wanted to autoformat a number while it was being inputted.
I used a Jquery plugin for the same -
<script src="simple.money.format.js"></script>
And calling it using
$('.money').simpleMoneyFormat();
I want it be reformatted when I hit a button (Save) so that the value can be saved into the database.
Any help would be much appreciated.
You can use the submit event handler, the code will be called when the user hits the submit button, and before the data sent to the server
$('#your-form-id').submit(function () {
$('.money').simpleMoneyFormat();
});
See the following fiddle: https://jsfiddle.net/EliteSystemer/wv3sxo3j/
$('button').on("click", function() { //Submit
var m = $('.money').val(); //Get input value
m = m.replace(/,/g, ""); //Remove commas
$('.money').val(m); //Update input value
...send to server
});

Tampermonkey: Trigger event does not work for element

I'm trying to add some functionality using Tampermonkey on top of a providers angular application but I'm stuck at this simple thing. I can't replicate the issue using CodePen so we're going to have to go for theories and suggestions. I'll try to be as specific as I can.
Adding this interval when the page loads to check when an input with the id serialNumberInput is available. Then I'm adding a dropdown to the form, and attach an onChange event to it to update the serial input field with the value of the selected option. However, the trigger parts just never happens. It does work when I enter them manually, but not with the script.
var populateSerialNumbersTimer = setInterval(function(){
var serial = $("input#serialNumberInput");
if($(serial).length >= 1){
$(serial).css("display", "inline").css("width", "50%");
$(serial).after(deviceToSerialSelectionHTML);
$("select#deviceToSerial").on("change", function(){
$(serial).val($("select#deviceToSerial").val());
$(serial).trigger("change");
$(serial).trigger("blur");
});
clearInterval(populateSerialNumbersTimer);
}
}, 200);
I've thought about it and considering how the serial number ends up in the text field the field must be accessible. Maybe it's that the events that I'm trying to trigger has not been declared at the time of the function declaration?
Suggestions much appreciated.
It looks like jQuery tries to cache the event somehow. This is how I solved it with native javascript in case someone else is interested:
function triggerEvent(e, s){
"use strict";
var event = document.createEvent('HTMLEvents');
event.initEvent(e, true, true);
document.querySelector(s).dispatchEvent(event);
}
$("select#deviceToSerial").on("change", function(){
serialNumberInput.val($("select#deviceToSerial").val());
triggerEvent("change", "input#serialNumberInput");
triggerEvent("blur", "input#serialNumberInput");
}

How to disable button in javascript

Onclick of button,(In clientclick event)I show confrmation box having ok and cancel button.Onclick of ok button,buttonclick event fire.I want to disable button and show to message(Pls wait) to user.I am trying but not working.Unable to disable the button and set text to label.
function validate()
{
var response = confirm(Proceed)
if(response)
{
document.getElementById('btnSave').disabled = true;
document.getElementById('lblMessage').innerText = 'Please Wait';
return true;
}
else
{
return false;
}
}
I am getting error.Error is
JavaScript runtime error: Unable to get property 'innerText' of undefined or null reference in asp.net
First of all there's maybe a problem in your HTML. Javascript cannot find the 'lblMessage' element. Fix that first and you're good to go.
Maybe you're using ASP.NET Web Forms. ASP.NET changes the client ID's of server controls depending on the ID of their container. You can change this behavior easily by reading this article:
http://weblog.west-wind.com/posts/2009/Nov/07/ClientIDMode-in-ASPNET-40
Or, find your elements by class name. That way it won't cause conflict with any configuration you're now using.
I think you need to add your html code. I'm guessing your html looks like this
<button onclick="validate()">
you need to add a return statement into the html code so it looks like this
<button onclick="return validate()">

how to place a button on crm 2011 form.

I want to place a button on CRM 2011 form.
function create_button(fldName, btnLabel, btnWidth, evt)
{
try{
fldName = "inmate_button_submit";
btnLable="Click Me";
// btnWidth="200px";
var btn = '<button class="ms-crm-Button" onclick="evt()" style="width:50px" id="' + fldName + '" onmouseover="Mscrm.ButtonUtils.hoverOn(this);" onmouseout="Mscrm.ButtonUtils.hoverOff(this);" >Click Me</button>';
var ctrl = null;
try {
ctrl = Xrm.Page.ui.controls.get(fldName).get_chromeElement()[0];
} catch (e) {
ctrl = Xrm.Page.ui.controls.get(fldName)._control.get_element();
}
// Add the new button
ctrl.innerHTML += btn;
// Hide the textbox
ctrl.children[0].style.display = 'none';
ctrl.children[1].style.display = 'none';
// Hide the label
Xrm.Page.ui.controls.get(fldName).setLabel(btnLable);
// Add Event to the newly created button
ctrl.children[0].attachEvent('onclick', evt);
}
catch(e){alert(e.description);}
}
function evt()
{
alert("You have clicked me!!");
}
The above is my following code which places the button on the form. When i click on the button, after showing the alert crm gives me the following error.
ERROR
An error has occurred.
Try this action again. If the problem continues, check the Microsoft Dynamics CRM Community or you for solutions or contact....
I have no idea why this is happening. Any help guys?
Keep in mind that using getElementById is going to be unsupported and may not work (does not work in Outlook 2007) in some browsers.
I would recommend placing this is in a web resource, or utilizing the ribbon for this functionality. Would either of those meet your requirements?
If you use your js as a webresource then you shouldnt have any problems.
Or you can even trigger the function OnLoad
Please see following :
http://www.mscrmconsultant.com/2012/07/insert-custom-button-on-crm-form-using.html
i will recommend add custom webresource ( HTML page which contanis javascript functionality on in onload event ) if you need access to form, use window.parent.document.Xrm.Page ....
2 things might cause this issue:
evt function is not accessible to your button
Resolution: try registering the evt function in global scope i.e.
evt = function() { alert("…"); }
or
window.evt = function() {}
2. Your button is using the same field name as the placeholder field.
This might cause internal issues.
Resolution: Try giving your button another (bogus) id instead i.e. fieldname + "_button"
Have you tried the 3rd party applications...
http://crmvisualribbonedit.codeplex.com/
Call the function from a web resource.
NOTE: Remember to add $ to reference the web resource location
Example $webresource:ButtonScript.js

"Submit is not a function" error in Firefox in dynamically CREATED form without other submit inputs

In Firefox 5's error console (but not in IE 9) I get the error "myForm.submit is not a function" when I call the following javascript function (in an external script file):
function go(url_go, arr_POST_vars, str_method) {
var str_method = str_method || "POST"; // by default uses POST
var myForm = document.createElement("form_redir");
myForm.setAttribute("method", str_method);
myForm.setAttribute("action", url_go);
for (var key in arr_POST_vars) {
var myInput = document.createElement("input");
myInput.setAttribute("name", key);
myInput.setAttribute("type", "hidden");
myInput.setAttribute("value", arr_POST_vars[key]);
myForm.appendChild(myInput);
}
document.body.appendChild(myForm);
myForm.submit();
}
The only HTML on my page (between the HTML body tags) is the following line:
<button type='button' onClick='javascript:go("http://www.testsite.com/login.php", {userCode:"2487",password:"jon123"}, "POST");'>Go!</button>
I have Googled extensively, and I know that this error occurs when there is a form input which is also named "submit", which then clashes with the "submit()" function name of the form. However this is not the case here.
The idea here is to navigate and submit POST values to a specific url when the button is clicked, by creating a form dynamically. It works fine in IE but not Firefox.
I am at a loss and a solution will be deeply appreciated.
Its caused in the type of element you've created. "form_redir" is no valid html tag so it created an element but it has only the default dom methods. Change it to "form" and it will work.

Categories