Create a custom validator for number field - dart

I need a validator for a form field <input type='number> to limit the range.
I tried to build one similar to the existing (like NgModelMaxLengthValidator) but it extends the private class _NgModelValidator, so this approach seems not to work.
Is it possible to add custom validators to the built in validation system?
What is a good approach?

Related

Conditional required data annotation with EditorForModel

We have a ViewModel for "create" and one for "edit." The edit inherits from create so that we're sharing common fields. We are then using an #Html.EditorForModel("User") that uses the "create" as its model.
This is for editing users, so I need the password field to be required on create, but not required on delete. Thus, the create VM has Password with [Required] decorating it, while the edit VM has password property with no decoration, and is also declared with new (public new string Password {get;set;}).
This is almost exactly similar to this question:
MVC4 Conditional Required Annotation
For my situation, it's still requiring the field I believe due to the EditorTemplate using the create object as its model instead of the edit. I tried changing it from editorformodel to:
#Html.EditorFor(model=>model, "User")
in hopes that because edit is actually a "create" due to inheritance that it would work, but it'still barking at me for not providing the required field when I post.
Is there a way to accomplish what I'm attempting, or are my options to either remove the required and handle it server-side, or split the editor template into two templates?
You can create a custom attribute to drive your check on whether the password is required or not. When the submission happens to the server your custom attribute can check to see if the you are dealing with an Update or and Insert and then invalidate the model if it needs to.
Here are some resources on creating custom attributes and custom attributes with unobstrusive jquery validation
Happy Coding!!

How to make a RequiredAttribute for create not for edit of model

I have a model containing a property of HttpPostedFileBase type, and I have created a custom validation attribute which implements RequiredAttribute to make some validations. It works perfectly when you create the model. However I don't want to make any validation when you edit the model (optional HttpPostedFileBase property when you edit the model). How do I make it?
Your attribute is simple metadata added to a property of a class. This attribute has no information about what you do with the class, i.e. if you're using this class to edit or to create a new entity.
So, the answer is that you cannot do that directly in the attribute. So the only solution is to "bypass" the attribute where you're using it, if you're editing the value. There are several alternatives:
use two different models, one for editing, the other for creating. And decorate the property only in the model for creating
remove the validation, or the error, using some code to do it: for example, you can remove the property error from the ModelState in a post action for the Edit action.
Obviously the easiest is 1. You can use a base class with all the fields, but those which have different treatement, and then inherit it for creation or edition. It's not strange to represent the same data with different models when you use MVC: a model for viewing, a model for showing the editor, a model for receiving the result of an edition... so, using several different models is not a problem at all. You can use AutoMapper or ValueInjecter to simplify moving data between entities and models.

How to create a generic validation.xml in struts 2 application?

For instance, I have a field to enter telephone number and i want it to be validated. I.e. check if the value entered is numeric and check if the entered value is having 10 digits.
I achieved this by creating a validation.xml.
But my question is How can i use the same set of validation rules(the same file) for another field? Is that possible in Struts 2?
Not sure if this is what you are looking for:
Create a new file called validators.xml in WEB-INF/classes and define the new validator.
<validator name="newvalidator" class="FQCN"/>
Create a class which extends FieldValidatorSupport to implement your own validation logic.
Use the new validator in validation xml files
<field-validator type="newvalidator">

How can I pre-populate a hidden form field in grails?

I have just gotten started with Grails and I have a very basic application running. I want to pre-populate a hidden form field with a random string.
What is the best way to do this? I have looked at the taglib but I am not sure what the best practice is for this sort of thing. Should I create a class in the src/java or src/groovy folder or is there a better way to get this done?
Lastly, and I know this is a very basic question, but if I do create a class or taglib, how exactly is that called from within the .gsp page?
Thanks!
If your action looks like this
def create() { [orgInstance: new Org(params)] }
it means that a new Org object is passed to your view which can be referenced as orgInstance
Since the model [orgInstance: new Org(params)] is a map, you can simply add another parameter:
def create() { [orgInstance: new Org(params), hiddenValue: 'something random'] }
This can be used in your .gsp in the following way:
<input type="hidden" name="test" value="${hiddenValue}" />
Regarding your other question: a custom taglib is used in the same way as the other Grails-Tags: <g:myTag ...>...</g:myTag> . You can change the namespacegto whatever you like -g` is the default. See the documentation for more details: http://grails.org/doc/latest/ref/Tag%20Libraries/Usage.html

How to dynamically add/remove validators in ASP.NET MVC at runtime?

We are relying heavily on client-side validation using MicrosoftMvcValidation.debug.js in the our current application implementation.
We have form elements and form validators being defined in the database and loaded from the database at runtime. We have viewmodel properties Answer1, Answer2, Answer3, etc., and up until now all fields were required, so we had the [Required] attribute on each of them, but now we need to apply this required annotation at runtime based on database settings since some of the questions are optional.
I don't want to do any reimplementation of validators themselves, I just want to either dynamically remove the [Required] attributes and/or their effects at runtime, or else dynamically add them at runtime.
Using ASP.NET MVC 2.
Add the [Required] attribute to any fields that could be required. As long as you don't bind a control client-side, you will bypass client validation without problems. On the server side post-back action, loop through the ModelState (which implements IDictionary) and clear the errors on the ModelState for the validators that you want to bypass.
foreach( var validator in ModelState){
if( validator.Key == "Validator_To_Bypass")
validator.Value.Errors.Clear();
}
Seems one can create a custom class that inherits ValidationAttribute that can determine at runtime how or whether validation is done. This is one way of accomplishing this requirement.

Resources