onSubmit() call multiple time service - angular2-forms

I am write a form with some fields, when all have valid then i am enable the submit button, do a service call on onSubmit() method.My issue is when i click on the submit button twice it do service call twice. Is there any directive for check form submit.

When you click on the submit button twice it will call the service two times just use one variable 'submit' boolean by default false.
reference: https://angular.io/guide/forms#submit-the-form-with-ngsubmit
onSubmit() {
if(this.submit) {
return;
}
this.submit = true;
this.serviceCall(); // got error set submit to false.
}

Related

Swift - How to use integrate SwiftValidator into my project

I've got a login form which takes 2 alternate forms dependent on how a user taps a segmented control .... either as a register form with email, password, and repeat password; or as a login form with just email and password.
When a user fills out the register or login fields and taps the register/login button ... I log the user in using code along the lines of ...
#IBAction func loginRegisterTapped(sender: AnyObject) {
//check whether the sender tag is registerTag (in which case a new user is registering or alternately returning user logging in
//do various checks and if all good submit the form
}
Ive recently copied over a validation library (SwiftValidator) to my project, and configured it to also validate fields when the login/register button is tapped ie i insert the validation code above my own code in the loginRegisterTapped ibaction method. My code after integrating SwiftValidator essentially becomes
#IBAction func loginRegisterTapped(sender: AnyObject) {
//validation
self.clearErrors()
validator.validate(self)
//check whether the sender tag is registerTag (in which case a new user is registering or alternately returning user logging in
//do various checks and if all good submit the form
}
The SwiftValidator library ends us calling 2 delegate methods (in my LoginViewController) on validation success or failure as follows
// SwiftValidator library Delegate methods
func validationSuccessful() {
// submit the form
}
func validationFailed(errors:[UITextField:ValidationError]) {
// turn the fields to red
for (field, error) in validator.errors {
field.layer.borderColor = UIColor.redColor().CGColor
field.layer.borderWidth = 1.0
error.errorLabel?.text = error.errorMessage // works if you added labels
error.errorLabel?.hidden = false
}
}
Given validation is successful, how can i get code to return to my #IBAction func loginRegisterTapped(sender: AnyObject) method and continue with my own form submission code (... I need a reference to the sender to check it's tag to determine whether the button was tapped as a "Register" button or a "Login" button) rather than conducting the form submission as suggested by the SwiftValidator library within it's validationSuccessful delegate method where i dont have access to the sender var. ie what options do i have here or what would be considered best practice? ... I thought of customizing the validationSuccessful method to return perhaps a bool to the calling function but doubt if that's the best way?
IMHO, I'd do the successful form submission in the validationSuccessful() method, not in your own action method. I'd avoid using tags to differentiate the login and register buttons, and instead have each one call a separate action method. You're not saving any time, code, or logical clarity by handling them both with the same handler.

Remote Validation OnBlur and Empty String

I have a view model with remote validation enabled. In my view I also disabled OnKeyUp
$(function() {
$.validator.setDefaults({ onkeyup: false });
})
But if I focus on the text box, and move the focus to another control the remote validation is not fired at all.
Is there a way I can ask remote validation to fire when I configured it to onBlur and empty string?
In order to do this you would need to modify the jquery-validate.js file.
The code associated with remote validation:
remote: function(value, element, param) {
if ( this.optional(element) )
return "dependency-mismatch";
var previous = this.previousValue(element);
....
// ajax call
....
}
The first if block tests if the element is optional, which it will be unless a [Required] attribute has been applied to the property, or the property is a value type (I assume you property is typeof string) so the function returns and the code to make the ajax call is never reached. Note that even if you added the [Required] attribute, this code block would never called because required: is validated first and you would just get the "The XXX filed is required" message.
If you comment out the first if block, and focus out with an empty string, the remote validation will be fired.

How do I prevent an on.submit event from changing/reloading the page?

In Javascript when I provide an onSubmit function, and I return 'false' from the function, it prevents the page from changing/reloading.
However in Dart the onSubmit.listen(...) function does not take a return type.
How do I stop the submit from sending the form data and changing/reloading the page?
Within the on.submit.add(...) callback, you will receive the Event as an argument. The Event object provides a preventDefault() method to prevent or cancel the default action.
It can be used as follows:
querySelector('#myForm').onSubmit.listen((Event e) {
// ... your code here
e.preventDefault();
});

Trying to join two independent forms

i'm trying to join two independent forms (login and register) in the
same page.
My idea is (just looking at the signin form):
Create an action that shows both forms (partials):
public function executeLoginAndRegister(sfWebRequest $request){
$this->form_signin = $this->getUser()->getAttribute('form_signin');
}
Each partial calls to its action:
form action="php?> echo url_for('#sf_guard_signin') ?>" method="post">
In the actions i write this code
public function executeSignin($request)
{
//...
$this->form = new $MyFormclass();
if ($this->form->isValid())
{
//...
}else{
// save the form to show the error messages.
$this->getUser()->setAttribute('form_signin', $this->form);
return $this->forward('sfGuardAuth', 'loginAndRegister');
}
}
It works, but, for example, if i execute LoginAndRegister and submit
incorrectly the signin form and I go to another page and then return to
LoginAndRegister, i will find the submiting error messages...
If i execute LoginAndRegister and submit incorrectly the signin form and
open another browser tab, i will find the submiting error messages in
the signin form of the second tab...
Any idea? any better approach?
I would just use sfDoctrineApplyPlugin if i were you :)
I have it, just writing in the if "request->isMethod('post')":
public function executeLoginAndRegister(sfWebRequest $request){
if($request->isMethod('post')){
$this->form_signin = $this->getUser()->getAttribute('form_signin');
}
}
Anyway if my approach has any big error or is not safety i would
thank anyone who tell me.
Javi

Getting ASP.NET ImageButton OnClientClick changes to the server

I use an ASP.NET ImageButton on my website. When a user clicks on the ImageButton, a javascript is fired via the OnClientClick event. The script changes the ImageUrl of the ImageButton.
On the same page, I have a submit button.
Clicking the button causes a post back to appear.
Is there a way of knowing the correct ImageUrl of the ImageButton from serverside now when processing the serverside OnClick event of the submit button?
can you store that changed inage url in a hidden text box and at postback you can get the text box.
or you could just use this in your codebehind:
protected void imageclicked(object sender, eventargs e)
{
ImageButton ClickedButton = sender as ImageButton;
}
then you can use ClickedButton.ImageUrl
You can add a hidden field to record what's the image name each time when clien side script fired. When postback, you just check the value of this field.

Resources