I have a login page that has 2 matInputs(username and password). I added the mat-error element to those to matInputs, so the mat-form-field displays an error message when entering invalid Username. Also, both inputs are part of a reactive form. So they both have the "formControlName" attribute.
The problem is that when I unfocus from one of the input fields(with out typing username or password), the warn color from Angular material triggers as part of the reactive forms validator(username/password should not be empty).
I provided images and I can provide code.
This is regular(Good):
This when entering password(Good):
This when Unfocues/Blur(Bad):
And this when entering invalid inputs(good):
I know that the reactive forms are triggering a validator when left empty(onUnfocus). I am trying to find a way to control that or control the Angular material warning color, so it does not trigger with the left empty validator.
There are different types of field validation and there is also form (submit/login) validation. At a practical level, you want form validation not field validation for not empty/required. By default, field validation (validators used in form controls or the 'required' attribute directive) is activated as soon as the field is 'touched'. So if you make a field 'required' an error will be shown as soon as the user applies and removes focus even without entering a value. Form validation however only takes place when the form is submitted.
You have two options - don't make those fields required and instead check them as part of your submit function and then set errors on the form controls if needed. You'll also need to take care of clearing those errors when the user enters a value or focuses the field.
Or, with reactive forms, you can implement a custom ErrorStateMatcher for those fields so that the 'required' validator will only throw an error if the form is submitted rather than when the field is touched. Turning off 'touched' validation this way is fairly common for this kind of thing - you can just modify the Angular Material example shown here: https://material.angular.io/components/input/overview#changing-when-error-messages-are-shown.
Related
I'm sure this has come up many, many times before but I am at my wit's end with this problem. I'll try and be as descriptive as possible without bloating the problem statement.
I am using the jQuery validate plugin for form validation. All form rules and messages are retrieved via ajax and then initialised. All form validation works like a charm until I start cloning fieldsets inside the form to give the user the ability to add additional entries.
The input fields react exactly as they should to changing values (error highlights and displaying messages etc), but when I call the .valid() method on the form it ignores the additional fieldset cloned from the first.
I have added test rules after cloning the fieldset, and again, the input field responds correctly to the newly assigned rule but gets ignored when calling .valid() on the form element.
I will add code snippets should anyone be willing to help me find the problem.
Thanks in advance!
Fred
Edit:
I have found that the .valid() method only uses the first fieldset in the form. Take note that all input names of cloned fieldsets are the same as the original. For example, say i have a field called productName in my original fieldset and i clone it, I will now have two inputs in the form with the same name attribute.
Could it be that jQuery validate only cares about the first instance it reaches and validates on that? I have seen posts about jQuery validate not playing nice with duplicate input names on forms but i believe it was resolved.
Have an ASP.NET MVC3 app with model validation using FluentValidation. User enters some html in a regular text field, and is presented with a user-friendly error on submit (because of the "A potentially dangerous Request.Form value" error).
What I'd really like to do is show a validation message right on the form itself ("Oops, no html allowed") which shows up right next to the field - similar to model validation errors. That way, the user (malicious or not), knows about it before the entire form is actually filled out and submitted.
Would appreciate any ideas on how to go about implementing this apart from what's shown here (i.e. without having to add [AllowHtml] + a regex to almost every field in almost every viewmodel)
Thanks!
You will need to write a custom ProertyValidator for server-side FluentValidation with client-side support. I think you can follow this article, here, to create client-side jquery validation rule and the adapter.
I have a form that has a field (Field A) that needs to be empty if Option 1 is selected on a dropdown, and is required if Option 2 is selected. I have some javascript that clears and disables the Field A when Option 1 is selected.
I have Simon Ince's RequiredIf attribute applied to Field A in the model, dependant on Option 1, and it works well. That's not the problem.
Here's the sequence of events that does cause a problem:
User has Option 2 selected (so that field A is required) and Field A empty.
User clicks on Save. Validation message appears by field A and in the validation summary. All is well.
User changes to Option 1. Field A becomes disabled.
User clicks on Save. Validation message remains by Field A but does not appear in the Validation Summary.
I conclude from this that the RequiredIf validation is working (and Field A is passing validation), but the old validation message is sticking around. Which I do not want.
All this is client-side, by the way.
If the field isn't disabled, everything works as expected, but I'd like it disabled rather than editable but "required to be empty".
Other than clear out the message SPAN tag through js and jQuery, is there a way to fix this?
Because the validation requirements are changing, you need to cause validation to occur, and then update all of the error messages. Since the issue relates to the change of the selected item on the dropdownlist, add the following:
$("#myDropDown").live("change", function() { // assumes dropdownlist has id of 'myDropDown'
$("form").validate().form(); // form() causes error messages to update
});
I have a view which is containing 5 forms submitting separately but to the same model. The aim of this is allow the user to fill all the forms in the same page and be able to quickly compare the values he puts in for each form. Each form has most of the fields in common with another one. If you want a idea of the stuff, imagine that you are a soccer trainer and you want to be able on the same page to set up your team tactics for each half time. So I'll have a HalfTime model with two forms on the same page, one for each half time
So when I submit Halftime 1 form and that the validation fails, the validation errors of HalfTime1 is displayed in HalfTime2 fields too. I would like to be able to disable the validation of one form according if it wasn't submited.
My idea was to send a variable from the controller to the view containing the name/id of the current half time and from this variable, displaying validation errors only in the ccorrect form.
Do you have any idea on how I can disable the display of the validation errors in one field?
Thank you a lot!
Set the error option to false when you use form input. ie:
echo $this->input('Halftime2.title', array('error' => false));
I am working on Microsoft office infopath 2007.I have inserted the controls as text box and drop down. when i get right click to control there are 2 option links as Data validation and Rules. what are the main difference in between them? I am new in infopath so need guidance.
when to use rules and when to use validations. can we customize validation and rules? and can we define custom rules and validation in infopath ?
Data validation can be used to display error alerts when users enter incorrect values into a form. Rather than checking for errors after a form is completed, data validation verifies values as the form is being filled out.
You can use rules to automatically display a dialog box, set a field's value, query or submit to a data connection, switch views, or open or close a form in response to certain events and conditions.
I'm not sure that you can create your own custom rules and data validation. But infopath forms can be customized by writing programming code to respond to form and data validation events, to access and manipulate a form's underlying XML document, to implement custom data submission and merges, and to implement access to external data sources.