is there any possibility to get error message without ts file, I used this i could validate my input
<form #form="ngForm" (ngSubmit)="logForm(form)" novalidate>
<ion-item>
<ion-label style="color: black;" fixed>User Name</ion-label>
<ion-input type="text" name="username" placeholder="valid user name" [(ngModel)]="username" pattern="[A-Za-z0-9]{3}" required></ion-input>
</ion-item>
<ion-item>
<ion-label style="color: black;" fixed>Email Id</ion-label>
<ion-input type="email" name="email" placeholder="Examples#gmail.com" [(ngModel)]="email"
pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,3}$" required></ion-input>
</ion-item>
<button ion-button type="submit" value="Submit" block>Login</button>
</form>
<p *ngIf=username.valid> The following problems have been found with the username: </p>
what i need is to display error message if the input is not valid, and i should not submit empty form
import { Component } from '#angular/core';
import { NavController, NavParams } from 'ionic-angular';
#Component({
selector: 'page-login',
templateUrl: 'login.html'
})
export class LoginPage {
constructor(public navCtrl: NavController
) {}
ionViewDidLoad() {
console.log('ionViewDidLoad LoginPage');
}
logForm(form) {
console.log(form.value)
if(form.valid) {
console.log(form.value);
/*here i get user entered values as object*/
}
}
You need to add to your form attributes something that will identify what fields we're going to validate, so for username, let's say this:
#userName="ngModel"
You can loose the [(ngModel)] in this case.
and then your validation:
<div *ngIf="userName.errors?.required && userName.touched">
Name is required
</div>
<div *ngIf="userName.errors?.pattern && userName.touched">
Not valid
</div>
So you just use the different validations you have set for input and check whether they match the validations, e.g required, pattern with the prefix of the name of the form control and errors?
So here's how your username validation would look like:
<ion-item>
<ion-label style="color: black;" fixed>User Name</ion-label>
<ion-input type="text" name="username" ngModel #userName="ngModel"
required pattern="[A-Za-z0-9]{3}"></ion-input>
</ion-item>
<div *ngIf="userName.errors?.required && userName.touched">
Name is required
</div>
<div *ngIf="userName.errors?.pattern && userName.touched">
Not valid
</div>
Here's a plunker.
Related
I'm using Spring+Thymeleaf to see and modify the users in a database. I would like to set the input fields to the actual values of an original user but I've tried with different styles and it doesn't work.
With the present configuration I can update information of users and see the id of original user (it's not in a input field) but I can't show the actual configuration in input field as default.
CONTROLLER:
#GetMapping(value = {"/", ""})
public String subusersPage(HttpSession session, Model model) {
String idUser = BaseController.getLoggedUser(session);
UserDTO userDTO = userService.getUserById(idUser);
model.addAttribute("subusersDTO", userService.getSubusersDTO(userDTO.getSubusers()));
model.addAttribute("populations", userDTO.getPopulations());
model.addAttribute("configurations", userDTO.getConfigurations());
model.addAttribute("attributes", userDTO.getAttributes());
model.addAttribute("subuserDTO", new SubuserDTO());
return "subusers";
}
HTML:
<th:block th:each="subuserDTO_original : ${subusersDTO}">
<hr>
<form action="#" th:action="#{/subusers/__${subuserDTO_original.id}__}" th:object="${subuserDTO}" method="post">
<div>
<p th:text="${'Id: ' + subuserDTO_original.id}"></p>
<p>Name: <input type="text" th:field="*{name}" th:name="name" th:value="${subuserDTO_original.name}"/></p>
<p>Population: <input type="text" th:field="*{population}" th:name="population" th:value="${subuserDTO_original.population}"/></p>
<p>Configuration: <input type="text" th:field="*{configuration}" th:name="configuration" th:value="${subuserDTO_original.configuration}"/></p>
<p>Attributes: <input type="text" th:field="*{attributes}" th:name="attributes" th:value="${subuserDTO_original.attributes}"/></p>
<p>
<button type="submit" th:name="action" th:value="update">Update</button>
<button type="submit" th:name="action" th:value="delete">Delete</button>
<button type="reset" th:name="action" th:value="clear">Clear</button>
</p>
</div>
</form>
<form action="#" th:action="#{/subusers/__${subuserDTO_original.id}__}" method="get">
<button type="submit">Default</button>
</form>
</th:block>
Any help will be very appreciated, thank you!
If you want to edit an existing user, then your th:object (which is ${subuserDTO} in this case) needs to be populated with the values of the original user. This is because when you use the attribute th:field="*{name}", it actually overwrites the name, id, and value of the html tag (which is why th:value="${subuserDTO_original.name}" isn't working.
Two other options you could do:
You could also set name="name" and use th:value instead.
Or another option, you could use ${subuserDTO_original} as your th:object.
My form in NOT asynchronised. I want file input to be required. If I add "required" attribute in that input it show popup required message even if file is uploaded. If I ommit "required" attribute and define validation in kendoUpload configuration to "minFileSize: 1" it only react after file is uploaded, but ignoring that validation if form is submited.
<form method="post" action="foo" enctype="multipart/form-data" id="document-form">
<div class="modal-body">
<input id="files" type="file" name="files" required/>
<input name="description" required/>
<div class="modal-footer">
<button type="submit" class="k-button">Dodaj</button>
</div>
</form>
$('#files').kendoUpload(
{
multiple: false,
validation: {
minFileSize: 1
}
}
)
do not use "required" attribute, use other attribute such as validationMessage
you can use this rule:
rules: {
upload: function(input) {
if (input[0].type == "file" && input.is("[validationMessage]")) {
var len = input.closest(".k-upload").find(".k-file").length;
return len > 0;
}
return true;
}
I have following Angular 2 form:
<register>
<form [ngFormModel] = "registrationForm">
<div class = "form-group">
<label class = "control-label" for="email">Email</label>
<input class = "form-control" type="email" id="email" ngControl="email" #email="ngForm">
</div>
<div *ngIf = "email.touched && email.errors">
<div *ngIf = "!email.errors.required && email.errors.underscoreNotFound" class = "alert alert-danger">
<span>Underscore is required</span>
</div>
<div *ngIf = "email.errors.required" class = "alert alert-danger">
<span>Email is required</span>
</div>
</div>
<div class = "form-group">
<label class = "control-label" for="password">Password</label>
<input class = "form-control" type="password" id="password" ngControl="password" #password="ngForm">
</div>
<div *ngIf = "password.touched && password.errors">
<div *ngIf = "password.errors.minLength && !password.errors.required" class = "alert alert-danger">
<span>Password should contain 6 characters</span>
</div>
<div *ngIf = "password.errors.required" class = "alert alert-danger">
<span>Password is required</span>
</div>
</div>
</form>
</register>
This is my Component where I have implemented validators:
import {Component} from '#angular/core';
import {Control, ControlGroup, FormBuilder, Validators} from '#angular/common';
import {CustomValidator} from './CustomValidator';
#Component({
selector: 'register',
templateUrl: './app/authentication/register_validation/register.html',
})
export class RegisterComponent{
registrationForm: ControlGroup;
constructor(formBuilder:FormBuilder)
{
this.registrationForm = formBuilder.group({
email: ['',Validators.compose([Validators.required, CustomValidator.underscore])],
password: ['',Validators.compose([Validators.required,Validators.minLength(6)])]
});
}
}
In this form, email field is working fine for both validators i.e. when I do not type anything , it gives "Email is required" message, when I start typing something, it gives "Underscore is required" message and when I type "_" all error messages disappears. However, when I try to apply such 2 validators on password field, it's not working. When I do not type password it gives message as "Password is required". But when I type something less than 6 characters, minLength message doesn't appear at all. What is wrong in this code?
The error key is minlength and not minLength:
<div *ngIf = "password.hasError('minlength') && !password.hasError('required')" class = "alert alert-danger">
<span>Password should contain 6 characters</span>
</div>
This really caught me out as well as I matched the key in my markup to what I had in code which is incorrect.
Sample Code
password1: ['', [Validators.required, Validators.minLength(8)]],
Sample Markup
*ngIf="registrationRequest.get('password1').hasError('minlength')"
Note in the code it's minlength entirely in lower case.
I was facing the same issue but after so many research I got a solution with this:
Use minlength instead of minLength
Here is the example:
<div *ngIf = "password.errors.minlength && !password.errors.required" class = "alert alert-danger">
<span>Password should contain 6 characters</span>
</div>
Instead of:
<div *ngIf = "password.errors.minLength && !password.errors.required" class = "alert alert-danger">
<span>Password should contain 6 characters</span>
</div>
I had the same problem where I wanted to validate a number field that contained the year, so I used Validators.maxLength(4) to make sure someone does not accidently type 5 numbers for a year. Turns out when you set the input type to number:
<input type="number" formControlName='year'>
then maxLength does not "work" anymore. Which actually makes sense if you think about it for half a second.
So I changed my date input to:
year: [null, [Validators.required, Validators.min(1990), Validators.max(2050)]],
Which is the right approach when working with a number field.
This worked for me for password validation.
<input type="password" class="form-control" name="password" placeholder="Password" [(ngModel)]="register.password" #password="ngModel" minlength="6" required>
field: ['', Validators.compose([Validators.required, Validators.minLength(8)])]
This only works with multiple validators.
<div *ngIf = "password.errors.minLength && !password.errors.required" class = "alert alert-danger">
<span>Password should contain 6 characters</span>
</div>
minLength must be lowercase minlength
This might not really be related to the question asked here. But in my case, it was happening due to the input type defined in the HTML.
Validator was initiated like this in my case:
this.OTPForm = this.fb.group({
otp: [null, [Validators.required, Validators.minLength(6), Validators.maxLength(6)]]
})
Validators.minLength and Validators.maxLength won't work for below as the input type is number/non-string hence the length cannot be really checked here.
<input type="number" nz-input formControlName="otp" placeholder="Enter OTP" />
While for below where the input type is stated as "text", the comparison can be done. And so the Validators.minLength and Validators.maxLength worked here.
<input type="text" nz-input formControlName="otp" placeholder="Enter OTP" />
I been trying to click on a link with class "selectable" without success.
The html code is as display below:
<div class="form-group">
<label for="txtEmail">Email</label>
<label class="selectable col-xs-offset-2">
<input type="checkbox" id="chkNoEmail" data-bind="checked: noEmail, attr: { 'disabled': baseController.readonly() }">
</label>
<a class="selectable" data-toggle="popover" data-bind="click: baseController.noEmailClicked, popover: { title: '', customClass: 'popover-lrg popover-alert', contentHtmlId: 'noEmailAlert', onlyIf: function () { return noEmail() } }" data-original-title="" title="">no email</a>
<input type="text" class="form-control" id="txtEmail" placeholder="Email" data-bind="value: email, attr: { 'disabled': noEmail() || baseController.readonly() }"><span class="validationMessage" style="display: none;"></span>
</div>
I have try using the parent div and also using
browser.selectable(:text, /no email/).click
and
browser.selectable(:text, /no email/).fire_event("onclick") but not success.
I would try something like this.
#browser.div(:class => "forum-group").label(:class => "selectable").check
However, I am not sure you can do a ".check" on a label element. You could also try
#browser.div(:class => "forum-group").checkbox(:id => "chkNoEmail").check
Or, since I dont think it is an actual check box, you could try ".click" instead of ".check" on either one of them, I still do not think you can do either action on a label element though.
When i use yui3`s io-form module to post a form, i found the field value that server reseived is null...
Any help is welcome.
<form name='testajax' id="testajax1" >
<input type="text" name="test1" id="test1" ></input>
<input type="text" name="test2" >
<input type="text" name="test3" id="result" >
<input type="submit" value="submit" id="submit">
</form>
Y.io('/ajax/test',{
method:'POST',
form: {
id:Y.one('#testajax1'),
useDisabled: true,
},
on:{
complete:function(id,response){
Y.log(Y.one('#test1').get('value'));
},
start:function(id,response){
Y.log(Y.one('#test1').value);
}
}
});
You are passing a Y.Node to form.id and the docs indicate that it takes either a string or a "formObject" which I'm assuming means a "form element". I don't believe a Y.Node is a valid (which is an unfortunate API choice if true). Try switching your code to:
form: {
id: "#testajax1"
}
http://yuilibrary.com/yui/docs/io/#serializing-html-form-as-data