Customize error message from Abide in foundation 6 - forms

I have a simple sign up form with one input tag that is set up for an email. I'm using abide to validate the email. That works, but I would like to create my own error message and to style elements on the page of my choosing. Is this possible?
I found this which I know isn't validating an email input ( I can't seem to find the code that abide is using to validate emails)
abide: {
validators: {
myCustomValidator: function (el, required, parent) {
if (el.value.length <= 3) {
document.getElementById('nameError').innerText = "Name must have more than 3 characters";
return false;
} //other rules can go here
return true;
}
}
I would assume if I am able to set up a custom validator that just mimics what abidie already does (email validation) then I could change all the elements on the page that I wanted to when the validation comes back false.

I'm not sure what exactly you're asking for but checkout the docs http://foundation.zurb.com/sites/docs/abide.html
So it is a little different now in Foundation 6. Once you initiate Foundation, you will have to overwrite certain properties in the Foundation.Abide object. Like this:
Foundation.Abide.defaults.patterns['dashes_only'] = /^[0-9-]*$/;
Foundation.Abide.defaults.validators['myCustomValidator'] = function($el,required,parent) {
if ($el.value.length <= 3) {
document.getElementById('nameError').innerText = "Name must have more than 3 characters";
return false;
} //other rules can go here
return true;
};
Then in your markup in your form would something like this:
<input id="phone" type="text" pattern="dashes_only" required ><span class="form-error">Yo, you had better fill this out.</span>
<input id="max" type="number" data-validator="myCustomValidator" required><span class="form-error">Yo, you had better fill this out.</span>
You can customize the error messages in the span tags.

Related

Vue form empty field

I made a form with vuetify,
Name in the form is not required but if the user leave it blank and save the form I want the group to be named Unnamed, maybe if another form is completed, unnamed1, unnamed2 etc...
How can I do it?
In the form I made, it fill the name with unnamed, if I erase , I can named it with the name I like , but if I delete again, it refill with unnamed.
I think there is a better way to do it.
Here is a part of the code I made.
<template>
<v-text-field
v-if="groupName === '' ? groupName = 'Unnamed group' : 'name'"
v-model="groupName"
></v-text-field>
<template>
<script>
export default {
},
data() {
return {
groupName: ''
Try not to have much logic in your template. You can define some method that will handle input change and then you can manipulate how you want with that input:
<v-text-field #change="changeHandler" v-model="groupName">
</v-text-field>
and then in your methods:
methods: {
changeHandler(){
if(this.groupName) {
// do something with this.groupName
} else {
//do something else with this.groupName
}
}
}

Angular 2 - Removing a Validator error

I wrote a function to update Validator rules on an input if a certain option was selected, using this method (the forms are built using FormGroup):
onValueChanged(data : any) {
let appVIP1 = this.vip1TabForm.get('option1');
let appVIP2 = this.vip2TabForm.get('option2');
let appVIP3 = this.vip3TabForm.get('option3');
//Set required validation if data is 'option3'
if(data != 'option3') {
//Due to initialization errors in UI, need to start with the case
//That there are validations, check to remove them
appVIP1.setValidators([]);
appVIP2.setValidators([]);
appVIP3.setValidators([]);
}
else {
appVIP1.setValidators([Validators.required]);
appVIP2.setValidators([Validators.required]);
appVIP3.setValidators([Validators.required]);
}
}
And I bind that function call to a click event on radio buttons (I initially used the guide from this answer, but the onChange function didn't bind correctly).
This works great, and if the user selects option 1 or 2, the validations are empty, and won't be triggered. If they select option 3, the validations are shown and submission is stopped. However, I run into the problem where the user submits, sees the error, and goes back to change to option 1 or 2. While the validator is cleared, my form still reads as invalid. I have multiple input fields I am validating, so I can't just set the form to valid if the validator is removed this way. How would I go about doing this? Can I remove the has-error for one particular field in the formgroup?
If the correct validators are in place, you can manually call AbstractControl#updateValueAndValidity after they select an option:
this.formBuilder.updateValueAndValidity();
(Where, of course, this.formBuilder is your FormBuilder instance.)
You can also call it on FormElements directly.
This is commonly used to trigger validation after a form element's value has been programmatically changed.
Instead of removing and adding validations. It is more simple to enable and disable fields. You need to add the Validators.required for all required fields. And disable the fields which are not required.
onValueChanged(data : any) {
let appVIP1 = this.vip1TabForm.get('option1');
let appVIP2 = this.vip2TabForm.get('option2');
let appVIP3 = this.vip3TabForm.get('option3');
if(data != 'option3') {
appVIP1.disable();
appVIP2.disable();
appVIP3.disable();
}
else {
appVIP1.enable();
appVIP2.enable();
appVIP3.enable();
}
}

JAX-RS, #FormParam display result in the same page

I have this program that I am coding, the main thing is mostly to get familiar with Jax-rs and rest API. I am sending from a form in JSP page one input and with POST I pass it to my java Response class(that I use annotation #FormParam), it does what calculation I need and then I get the result in another link, what can i do to take the result just below my button on the same page? For my button function I use javascript, and when I push it I don't want to be redirected and take the result like this.
So i use this form and a javascript function in my jsp to pass the value in my POST method in java throw #FormParam
<form name="Form1" method="post">
<p>
Name : <input type="text" name="malename" id="malename" autofocus
value="enter your name..." />
</p>
<input type="submit" name="action" id="malename" value="I'm feeling lucky"
onclick="OnButton1();"> </form>
function OnButton1() {
document.Form1.action = "webapi/perfectmatcher/mate"
document.Form1.target = "_self"; // Open in a new window
document.Form1.submit(); // Submit the page
//return;
}
Here after i have the value i use two other classes to do the calculations and return the result i need. In a few words i pass a value in my form and when i push the button i am redirected in another link(the path of POST) and i display the result. I want to display the result below my button. I hope the code is helpful and i hope i am clear with my explanation
#POST
#Path("/mate")
#Produces(MediaType.TEXT_PLAIN)
public Response postMaleName(#FormParam("malename") String malename) {
String femalename = NumMatcher.numMatcher(Numberizer.numberizer(malename));
// FemaleName female = new FemaleName(femalename);
female.setName(femalename);
System.out.println(female.getName());
return Response.status(200).entity("Perfect Match for " + malename + " is " + femalename).build();
}
P.S.:The object female that i am trying to create its for testing purposes, i was trying somehow to get the information that i store but i cannot find how!!I used even jsp expressions but i get null value of course ...
<%
FemaleName female = new FemaleName("john");
String string = female.getName();
out.print(string);
%>

approach for validated form controls in AngularJS

My teammates and I are learning AngularJS, and are currently trying to do some simple form field validation. We realize there are many ways to do this, and we have tried
putting input through validation filters
using a combination of controller and validating service/factory
a validation directive on the input element
a directive comprising the label, input and error output elements
To me, the directive approach seems the most "correct". With #3, we ran into the issue of having to communicate the validation result to the error element (a span sibling). It's simple enough to do some scope juggling, but it seemed "more correct" to put the span in the directive, too, and bundle the whole form control. We ran into a couple of issue, and I would like the StackOverflow community's input on our solution and/or to clarify any misunderstandings.
var PATTERN_NAME = /^[- A-Za-z]{1,30}$/;
module.directive("inputName", [
function () {
return {
restrict: "E",
require: "ngModel",
scope: {
fieldName: "#",
modelName: "=",
labelName: "#",
focus: "#"
},
template: '<div>' +
'<label for="{{fieldName}}">{{labelName}}</label>' +
'<input type="text" ng-model="modelName" id="{{fieldName}}" name="{{fieldName}}" placeholder="{{labelName}}" x-blur="validateName()" ng-change="validateName()" required>' +
'<span class="inputError" ng-show="errorCode">{{ errorCode | errorMsgFltr }}</span>' +
'</div>',
link: function (scope, elem, attrs, ngModel)
{
var errorCode = "";
if (scope.focus == 'yes') {
// set focus
}
scope.validateName = function () {
if (scope.modelName == undefined || scope.modelName == "" || scope.modelName == null) {
scope.errorCode = 10000;
ngModel.$setValidity("name", false);
} else if (! PATTERN_NAME.test(scope.modelName)) {
scope.errorCode = 10001;
ngModel.$setValidity("name", false);
} else {
scope.errorCode = "";
ngModel.$setValidity("name", true);
}
};
}
};
}
]);
used as
<form novalidate name="addUser">
<x-input-name
label-name="First Name"
field-name="firstName"
ng-model="firstName"
focus="yes"
model-name="user.firstName">
</x-input-name>
<x-input-name
label-name="Last Name"
field-name="lastName"
ng-model="lastName"
model-name="user.lastName">
</x-input-name>
...
</form>
First, because both form and input are overridden by AngularJS directives, we needed access to the ngModel API (ngModelController) to allow the now-nested input to be able to communicate validity to the parent FormController. Thus, we had to require: "ngModel", which becomes the ngModel option to the link function.
Secondly, even though fieldName and ngModel are given the same value, we had to use them separately. The one-way-bound (1WB) fieldName is used as an attribute value. We found that we couldn't use the curly braces in an ngModel directive. Further, we couldn't use a 1WB input with ngModel and we couldn't use a two-way-bound (2WB) input with values that should be static. If we use a single, 2WB input, the model works, but attributes like id and name become the values given to the form control.
Finally, because we are sometimes reusing the directive in the same form (e.g., first name and last name), we had to make attributes like focus parameters to be passed in.
Personally, I would also like to see the onblur and onchange events bound using JavaScript in the link function, but I'm not sure how to access the template markup from within link, especially outside/ignorant of the larger DOM.

Asp.Net JQuery Validation Custom Rule

i try to customize the rule of the JQuery validation plugin and I'm almost done. The only problem I got is that the function $("<%=txtUserl.UniqueID %>").val() return undefined. How can I get the value of that textbox ?
$("#aspnetForm").validate
(
{
rules:
{
<%=txtUser.UniqueID %>:
{
required: true,
remote: "CheckUser.aspx?User=" + $("#<%=txtUser.ClientID %>").val()
}
},
messages:
{
<%=txtUser.UniqueID %>:
{
remote: "Invalid user"
}
}
}
);
And in my webform
<asp:TextBox ID="txtUser" runat="server" TabIndex="1" />
UPDATE
I change to use ClientID instead of UniqueID. Also, I put my javascript code at the end of my file instead of in the beginning of the file.
Now, the problem I got is txtUser.val() return an empty string "". in fact, I notice that it's return the old value of the textbox if I change the value. It's doesn't return the current value, which is want I need...
try txtUser.ClientID instead of UniqueID
$("#<%=txtUser.ClientID %>").val()
Note the # that's missing as well.
Yes use the txtUser.ClientID but also you have to use the (#) to get the value of the text box your statement should be
$("#"+"<%=txtUser.UniqueID %>").val()
if you don't use (#) sign then it will give error
hope this will help