I have a text area which I need to validate.
I can not use paper-textarea, because "only required and maxlength validation is supported."
So, my only choice seems to be iron-autogrow-textarea, which does have a validate(). But I don't know how to make use of the validate(). Can someone please let me know?
My textarea field currently looks like this.
<iron-autogrow-textarea
id='{{name}}'
class="textareas"
cols='60'
bind-value= "{{value}}">
</iron-autogrow-textarea>
Where did you get your information for paper-textarea
"only required and maxlength validation is supported."
The below documentation indicates that it supports almost all the validation:
https://elements.polymer-project.org/elements/paper-input?active=paper-textarea
If you need to implement your own validator, then you need to create two elements
Validator that implements 'Polymer.IronValidatorBehavior'
Element that implements 'Polymer.IronValidatableBehavior'
Check the ssn validators in the paper-input demo for an e.g. on how to implement those.
https://github.com/PolymerElements/paper-input/tree/master/demo
Related
I'm wondering if it's possible to describe a format, that an interface property should have. For example:
interface User {
age?: number,
name: string,
birthdate: string // should have format 'YYYY-MM-DD'
}
I read about decorators but it seems to only apply to classes, not interfaces.
I'm building an API with node/express and want to have input validation. So I'm considering Celebrate which can take joi type Schema to validate input. But I would like to use TypeScript instead to define my Schema / view model... As you see I try to use an Interface to define how the input of a given end point should look like:
age: number, optional
name: string
birthdate: string in format "YYYY-MM-DD"
Any hints and help much appreciated :)
Any hints and help much appreciated :)
First and foremost : you will have to write code for the validation. It will not happen magically.
Two approaches:
Out of band validation
You use validate(obj) => {errors?}. You create a validate function that takes and object and tells you any errors if any. You can write such a function yourself quite easily.
In band validation
Instead of {birthdate:string} you have something like {birthdate:FieldState<string>} where FieldState maintains validations and errors for a particular field. This is approach taken by https://formstate.github.io/#/ but you can easily create something similar yourself.
A note on validators
I like validators as simple (value) => error? (value to optional error) as they can be framework agnostic and used / reused to death. This is the validator used by formstate as well. Of course this is just my opinion and you can experiment with what suits your needs 🌹
I want to use custom error messages for validation constraints on dozens of fields in my project.
I do not want to set the message on every one of these, because that would be a blatant violation of DRY. Repeating the same string in every declaration like: #NotNull(message="custom msg") would mean that if I decide to change the message in the future I'd have to hunt them all down replace them, but even worse I might use the wrong string on some of them and be inconsistent.
How do you deal with this?
Is the best option really to extend every symfony stock constraint and set my default there, and use my own custom annotation class?
Please note that using the translator is not an option for me, so I am looking for a solution that does not include the symfony translation component.
Thanks a lot for the help in advance.
Assuming you are working with English as the app's language, and you've configured translator: { fallback: en }, you can override these constraint messages universally. Start by creating this file: app/Resources/translations/validators.en.yml
And use the following translation format:
This value should not be blank.: Your custom message here
Whatever the standard message is.: Another custom message
This also works for any other language setting, assuming you've made a validators.lang.yml for it!
You can also place this file in your bundle directory under Resources/translations, and a few other places.
You can read more about this here!
I had a text field in my form like
$form->textField($model,'test',array('required'=>'true'))
The validation works for required field. Is any way to give validation for an integer like this . I tried Like
$form->textField($model,'test',array('required'=>'true','integer'=>'true'));
But it doesn't work. Is any other way to do it
Thanks in advance
in your model under rules() function add the rule like below
array('test', 'numerical', 'integerOnly'=>true),
This has to be done in the rules. Your code as provided was:
$model = "someString";
$form->textField($model,'test',array('required'=>'true'));
This code itslef will not cause the field to be required, all it will do is add a required attribute with a value of true to the generated HTML, e.g.:
<input name='someString' required='true'>test</input>
That's not going to cause validation to run. Validation will run because of rules defined in the model, and can not be added during the view stage.
We can also use like this -
array('number_value', 'match', 'pattern'=>'/^[0-9]+$/', 'message'=>"{attribute} no. Invalid."),
My question refers to validation in a Model class.
I know how to basically do validation, but I've though got a question about it.
I'd like to now if it is possible to use a alias in validation? Because it should display a german message, and mixed up with my English database field name it looks really strange.
So, without an alias my code in the model class would be
#Equals("password")
#Required
public String passwordConfirm;
So password has to equal with passwordConfirm (passwordConfirm = "Bestätigung des Passworts" in German, password="Passwort" :)
Which would print, if a error occurs: "Bestätigung des Passworts muss mit password übereinstimmen." (should be "Passwort", not "password")
So do I need to define an alias or something, or how could this work?
greetings
You should be able to use the "message" variable for that, to override the output of the validation.
#Equals({value="password", message="key.to.messages.i18n")
This will let you define the validation messages in I18N files.
Disclaimer: don't have the code here, but it's something like this, beware of typos :P
I would like use data annotations to handle validation in my Silverlight app. The built-in validation attributes (primarily StringLength and Required) are great, and make life very easy. However, they seem to have one critical flaw. If my locale is set to fr-CA, for example, the validation exceptions are still in English - 'The Name field is required', 'The field Name must be a string with a maximum length of 20', etc.
This is a major problem. It means that if I want localized error messages for the built-in validation attributes, I have to manually add ErrorMessage/ErrorMessageResourceType to every validation attribute on every validatable property in my business layer, and manually add translated strings for every error message.
So... am I missing something here? Is there a way to automatically have the built-in validation attributes localized? Or some other easier way of doing this? Or am I just completely out of luck, and stuck with the manual route?
Any comments or thoughts would be appreciated.
Ok, I got around this by simply subclassing the built-in validation attributes. Problem solved!
internal class LocalizedStringLengthAttribute : StringLengthAttribute
{
public LocalizedStringLengthAttribute(int maximumLength)
: base(maximumLength)
{
}
public override string FormatErrorMessage(string name)
{
return String.Format(CultureInfo.CurrentUICulture, LanguageResources.Resource.Error_StringLength, name, MaximumLength);
}
}