Currently if using Parsley, it will auto validate when we click submit button
What I want to do is
When user submit button, handle with jQuery
Depends on field value, for example STATUS is DRAFT, do not trigger the Parsley validation
If STATUS is PUBLISH, trigger the Parsley validation
Question is, how do I prevent Parsley from auto validate when I click submit button so I can achieve the scenario above?
$(".review_form").submit(function (event) {
// get status value
var status = $("#status").val();
// continue the form submission without validating
if (status === 'DRAFT') {
return true;
}
else {
// let Parsley validate the form
}
});
If you don't want validation feedback in draft mode, you should listen to your #status changing and call destroy() on the Parsley form or re-init it accordingly.
If you'd potentially like validation feedback but still allow the form to submit, that is way more difficult than it should be, sorry. I opened an issue.
Related
I added a code injection in Squarespace to block specific email. If user inputs email, submit button will be disabled. I added recaptcha before and didn't work so I decided to block the specific email. But I still get same spam email from same email used in my contact form. Is there any other way to fix this?
I get the same experience posted here: https://www.signal-arnaques.com/en/scam/view/260546
One possible explanation is that the submit button is not being used to submit the form (it could be submitted programmatically or by other means). You could use JavaScript to add an event listener to the 'submit' event and, within that, prevent the form from being submitted based on your comparison logic. It'd look something like:
var myForm = /* Get the form element. */;
myForm.addEventListener("submit", function(e) {
var emailAddress = /* Get the value of the field */;
if (emailAddress == "info#domainworld.com") {
e.preventDefault();
}
});
Of course, because it's client-side JavaScript, it's entirely possible that the bot could work around this as well.
I am using angular-schema-form, and ran into a problem that when I load a schema and a form from a server using REST, the validation sometimes did not kick in. I could post a schema even though some fields were required.
How can I always be sure that the required fields in the form has to be filled in by the user before posting?
I found that using $scope.$broadcast('schemaFormValidate'); before submitting the form works (from the docs).
$scope.onSubmit = function(form) {
// First we broadcast an event so all fields validate themselves
$scope.$broadcast('schemaFormValidate');
// Then we check if the form is valid
if (form.$valid) {
// ... do whatever you need to do with your data.
}
}
However, we can not disable any buttons beforehand.
#John you can set a value in your model that is part of a display condition. That allows you to hide the buttons on submit and then re-enable them when you are ready for the user to submit the form again for any reason.
After submitting the form, the pristine state of the input is still "false". I don't know how to reset the pristine state to be true. In Angular 1, I would use the $setPristine function.
I looked at the API and developer guide. There is no API to reset input to pristine.
Instead developer guide on forms (section "Add a hero and reset the form"), shows a mechanism to reset form to pristine by adding a active flag on the component. And then binding it on form tag with ngIf
active = true;
newHero() {
this.model = new Hero(42, '', '');
this.active = false;
setTimeout(()=> this.active=true, 0);
}
and
<form *ngIf="active">
This regenerates the form, setting its controls back to pristine.
There is a pull request waiting to be added https://github.com/angular/angular/pull/6679
See also this related issue https://github.com/angular/angular/issues/4933
The usually used workaround is to recreate the form.
I have a login form that has a username and password. Both are required to trigger other form elements.
However, in chrome if the password is saved, form.$invalid returns true and the digest doesn't re-run when the saved information gets added. Is there any way to require fields being saved and set by the browser and have angular re-check form.$valid?
Surprisingly, I was facing a similar problem some time back. The trick is to get the browser to fire input events for things that may have been filled in by chrome autofill (but haven't updated).
If you have a submit or click handler for the form submission, you can trigger the input event on all your inputs so that angular will pick up the changes by autofill.
You may do something like this
var app = angular.module('app',[]);
app.run(function() {
// Trigger input event on change to fix auto-complete
$('input, select').on('change',function() { $(this).trigger('input'); });
});
to fire input event on all inputs.
Here's the original github issue related to your problem.
There's one field in the form which has strict format (validator is already in place).
How to make form submit automatically in AngularJS as soon as it becomes valid?
I am assuming you are talking about ajax post of form data (model data).
You can do a $watch on the $valid property of a form field and submit the form as soon as it becomes true.
See my fiddle here
Something like
$scope.$watch('myForm.userName.$valid',function(newValue,oldvalue) {
if(newValue) {
alert('Model is valid');
//Can do a ajax model submit.
}
});
You can get mode details from the form directive documentation