I am using Angular2 and when users go to my page using IE11 on Windows 7 (which appears to be the only combination that has issues) they cannot enter information into the textboxes/textareas. The drop-downs work just fine as do the checkboxes (which will show/hide additional fields as needed although you still cannot enter information into the new text fields).
If the user clicks the submit button all the required fields show as needing input and then the user can enter the information.
If it matters, the users are redirected to this page after enter RECAPTCHA information. If I take out the "canActivate" from my app routing module it works.
Here is the captcha code
import { Component } from '#angular/core';
import {
Router,
NavigationExtras
} from '#angular/router';
import { CaptchaService } from '../../utils/captchaguard/captcha.service';
import { ReCaptchaComponent } from 'angular2-recaptcha/lib/captcha.component';
#Component({
moduleId: module.id,
templateUrl: 'captcha.component.html'
})
export class CaptchaComponent {
message: string;
constructor(
public captchaService: CaptchaService,
public router: Router) {
//this.captchaService.isLoggedIn = true; //Uncomment out for testing
this.captchaService.isLoggedIn = false; //Uncomment out for live
}
handleCorrectCaptcha(userResponseKey: any) {
let redirect = this.captchaService.redirectUrl ? this.captchaService.redirectUrl : '/admin';
let redirectParts = redirect.split('/');
let appendID = redirect[redirect.length - 1];
//let appendID = 0;
this.captchaService.isLoggedIn = true;
//this.router.navigate(['/studyfd' + appendID]);
this.router.navigateByUrl('/studyfd' + appendID);
//this.router.navigate(['/studytest/0']);
}
}
Here is a portion of the HTML that generates the page in question:
<!-- First and Last Name -->
<div class="row">
<div class="col-sm-6">
First Name, Last Name
</div>
<div class="col-sm-3">
<input type="text" name="FirstName" id="firstname" required="required"
placeholder="First Name" class="required "
[(ngModel)]="user.FirstName" *ngIf="!readonly" />
<label *ngIf="readonly">{{user.FirstName}}</label>
</div>
<div class="col-sm-3">
<input type="text" name="LastName" id="lastName" required="required"
placeholder="Last Name" [(ngModel)]="user.LastName" *ngIf="!readonly"
class="required " />
<label *ngIf="readonly">{{user.LastName}}</label>
</div>
</div>
Turns out the issue was due to the RECAPTCHA window not closing (probably because of the redirect I was doing in handleCorrectCaptcha). Resolved by adding a hidden button and in the handleCorrectCaptcha method I made the button visible. Clicking on the button performed the redirect that was originally done in the handleCorrectCaptcha method.
Related
In my Laravel+Vue.js SPA ( Single Page Application) I am using the phone number input validation package from here, BootstrapVue from here and Veevalidate from here .
I think I need to show only the code inside my component file. The vue-phone-number-input component shows blue border and HTML5 error message tooltip (?) upon invalid data but no error message appears in red as it happens with name field. My code follows in EditProfile.vue:
<ValidationObserver ref="form" v-slot="{ passes }">
<div id="registration_form">
<b-form #submit.prevent="passes(onSubmit)" #reset="resetForm">
<ValidationProvider vid="name" rules="required|min:2" name="name" v-slot="{ valid, errors }">
<b-form-group
label="User Name:"
label-for="exampleInput1"
>
<b-form-input
type="text"
v-model="name"
:state="errors[0] ? false : (valid ? true : null)"
placeholder="Enter your name"
></b-form-input>
<b-form-invalid-feedback id="inputLiveFeedback">{{ errors[0] }}</b-form-invalid-feedback>
</b-form-group>
</ValidationProvider>
<ValidationProvider vid="mobile" rules="required" name="mobile" v-slot="{ valid, errors }">
<b-form-group
label="Mobile:"
label-for="exampleInput1"
>
<vue-phone-number-input
v-model="mobile"
default-country-code="BD"
required
disable-leading-trailing-space
:state="errors[0] ? false : (valid ? true : null)"
#update="updatePhoneNumber"
placeholder="Enter Mobile Number"
/>
<b-form-invalid-feedback id="inputLiveFeedback">{{ errors[0] }}</b-form-invalid-feedback>
</b-form-group>
</ValidationProvider>
<b-button type="submit" variant="primary">Submit</b-button>
<b-button type="reset" variant="danger">Reset</b-button>
</b-form>
</div><!-- end of id registration_form-->
</ValidationObserver>
JS part inside EditProfile.vue is:
import Datepicker from 'vuejs-datepicker';
import { ValidationObserver, ValidationProvider } from "vee-validate";
export default {
name: "EditProfile",
components: {
ValidationObserver,
ValidationProvider,
Datepicker
},
data: () => ({
name: "",
mobile:""
}),
methods: {
onSubmit() {
console.log("Form submitted yay!");
},
resetForm() {
this.name = "";
this.mobile = "";
requestAnimationFrame(() => {
this.$refs.form.reset();
});
}
}
};
When the submit button is pressed then validation works for name field nad for vue-phone-number-input it also happens but no error message shows up in red color.
My Vue.js version is 2.6.10 and I used the latest versions of BootstrapVue and Veevalidate (3) as well.
How can I make the error message appear in red color for vue-phone-number-input element ?
In bootstrap, invalid-feedback is only shown when it has a sibling input (or select etc) that is set to the state is-invalid. Chances are that vue-phone-number-input does not meet that criteria. So, instead you should make your error message like this:
<span class="text-danger" v-show="errors[0]">{{ errors[0] }}</span>
I have a input box and user have to give their facebook user id on that so i make input box with default value https://www.facebook.com and then user give their user-profile-link then it will update data but i got this error when anything gonna type on input box
<div class="column">
<label class="label">Facebook Id::</label>
<input class="input is-medium" type="text" v-model="'https://www.facebook.com/'+data.fblink">
</div>
<script>
import contactInfo from './ContactInfo'
export default {
components: {contactInfo},
props: ['data'],
binding is for variables only, use this it works
<div class="column">
<label class="label">Facebook Id::</label>
<input class="input is-medium" type="text" v-model="facebook_link">
</div>
in your data variable add facebook_link as string. then if you want to update use this
'https://www.facebook.com/'+facebook_variable
v-model needs a variable to bind to, not a fixed string.
...
<input v-model="fbProfileLink"/>
...
export default {
components: {contactInfo},
props: ['data'],
data() {
return {
fbProfileLink: 'https://www.facebook.com/'
}
}
}
If you're making any requests to that link though, you probably shouldn't let the user manipulate it freely but put guards in place so you won't be making requests to a user provided URL blindly.
I'm working with a template form component with angular2 and I cannot get to set the focus in my firstName input element after submit the form. The form is reseted fine but no way of setting focus.
This is my component code:
export class TemplateFormComponent {
#ViewChild('f') form: any;
onSubmit() {
if (this.form.valid) {
console.log("Form Submitted!");
this.form.reset();
this.form.controls.firstName.focus();
}
}
}
and my template code:
<form novalidate autocomplete="off" #f="ngForm" (ngSubmit)="onSubmit()">
<div class="form-group">
<label>First Name</label>
<input type="text"
class="form-control"
name="firstName"
[(ngModel)]="model.firstName"
required
#firstName="ngModel">
</div>
</form>
On your view, set a reference to the input field you want to focus (you already have that with #firstName).
Then, on your component code, create an access to it with #ViewChild:
#ViewChild('firstName') firstNameRef: ElementRef;
And finally, right after reseting the form:
this.firstNameRef.nativeElement.focus()
ps.: I would expect the FormControl api to expose a focus method, but this issue on gitHub suggests it may never happen.
For a more generic solution see
Is it possible to get native element for formControl?
`
#Directive({
selector: '[ngModel]',
})
export class NativeElementInjectorDirective {
constructor(private el: ElementRef, private control : NgControl) {
(<any>control.control).nativeElement = el.nativeElement;
}
}
`
This will add the nativeElement to EACH form control once you add the directive to your module.
UPDATE: a much simpler solution for this usecase
To set focus on the first invalid element on a form create a directive like this:
import { Directive, HostListener, ElementRef} from '#angular/core';
#Directive({
selector: '[focusFirstInvalidField]'
})
export class FocusFirstInvalidFieldDirective {
constructor(private el: ElementRef) { }
#HostListener('submit')
onFormSubmit() {
const invalidElements = this.el.nativeElement.querySelectorAll('.ng-invalid');
if (invalidElements.length > 0) {
invalidElements[0].focus();
}
}
}
Then simply add it to your form element tag
Simple solution: add "autofocus" to your input element properties.
<input type="text" class="form-control" name="firstName" autofocus required>
I do not understand how I can validate a form with jquery validate to display the Submit button or not.
My javascript is as follows:
ko.bindingHandlers.jqValidate = {
init: function (element, valueAccessor, option) {
var element = element;
var validateOptions = {
ignore: [":hidden:not(required)"],
focusCleanup: true,
onsubmit: true
};
function displaySubmitButton() { // Remove/Add class to submit button
if ($('form').valid()) $('.toSave').show();
else $('.toSave').hide();
}
$('form').bind('onchange keyup onpaste', function (event, element) {
if ($('form').valid() === true && $(element).not('.resetForm')) $('.toSave').show();
else if ($(element).not('.resetForm')) $('.toSave').hide();
});
}
};
My form :
<ol class="MutColor13 mouseOver" data-bind="jqValidate: {}">
<li class="rightCol">
<strong>
<label for="iEmailReply"><asp:Literal runat="server" ID="lEmailReply" /></label>
</strong>
<input id="iEmailReply" name="EmailReply" type="text" tabindex="2"
class="MutColor13 email"
data-bind="value: communication.Message.ReplyEmail, valueUpdate: 'afterkeydown'"
required="required" />
</li>
<li class="leftCol">
<strong>
<label for="iEmailFrom"><asp:Literal runat="server" ID="lEmailFrom" /></label>
</strong>
<input id="iEmailFrom" name="EmailFrom" type="text" tabindex="1"
class="MutColor13 email"
data-bind="value: communication.Message.SenderEmail, valueUpdate: 'afterkeydown'"
required="required" />
</li>
<!-- and more form input -->
</ol>
My submit button :
<div class="buttonBlock rightButton" data-bind="fadeVisible: validateMessage()">
<a class="MutButton3 toSave" data-bind="click: saveMessage"><span class="MutButton3">submit</span></a>
</div>
when I type "$ ('form'). valid ()" in the firebug console, all error messages appear. I do not think the submit button is the problem because I have not clicked at this point
How do I enable the display of the error message from the input short change while allowing the display of the submit button if fields (and any other form fields in the page) if all fields are valid?
I was inspired by this question: jquery validate: IF form valid then show submit button
a working demo : http://jquery.bassistance.de/validate/demo/
but the button is displayed continuously
ok I think this could work:
http://jsfiddle.net/Ay972/10/
what I did :
$('form').bind('change oninput', function (event, element) {
console.log('formLive :: ', $(event));
if ($('form').valid() === true && $(element).not('.resetForm')) $('.toSave').show();
else if ($(element).not('.resetForm')) $('.toSave').hide();
});
the 'change oninput' seems to work.
I have a "Create an account" form that is handled by spring. I am using jquery to auto-update some of the fields.
<script type="text/javascript">
$().ready(
function() {
$("#item1").keyup(function() {
var text= (this).value;
//modify text
$("#item4").val(text);
});
});
</script>
<form:form action="${form_url}" method="POST" modelAttribute="object" enctype="${enctype}">
<label for="Item1">Item1:</label>
<input type="text" id="item1" name="item_1"/>
<br></br>
<br></br>
<label for="Item2">Item2:</label>
<input type="text" id="item2" name="item_2"/>
<br></br>
<br></br>
<label for="Item3">Item3:</label>
<input type="text" id="item3" name="item_3"/>
<br></br>
<br></br>
<label for="Item4">Item4:</label>
<input type="text" id="item4" name="item_4"/>
</form:create>
<input id="proceed" type="submit" value="Save"/>
Upon pressing the submit button I need to make sure the first field "item1" is not empty. I haven't found a solution to this uses spring forms. Thanks in advance!
There are many ways to implement such validation. It is recommended you do both client and server side.
For client side you can simple create a click listener to the submit button
$('.submitbutton').click(function(e) {
e.preventDefault();
if($('#item1').val().trim() != '') $('.myform').submit()
else { $('.item1_error').show();
}
Or (even better) use 3rd party validation library
For the server side you can use hibernate-validator and add #NotEmpty annotation to your form bean
public class MyForm {
#NotEmpty private String item1;
// ...
}
Then on your controller handler method just decorate the parameter with #Valid. Any errors can be checked through the BindingResult object
#RequestMapping(..)
public String myhandler(#Valid #ModelAttribute("object") MyForm myForm, BindingResult binding) {
if(binding.hasErrors()) {
// ...
}
}