Quantcast
Channel: Fluent Validation for .NET
Viewing all 1917 articles
Browse latest View live

New Post: Use HttpContext.User in Validator

$
0
0
Hello,

I need the HttpContext.User in a validator. What is the best way to accomplish that?

Use an interceptor?

Inject a service in the constructor?
(Maybe using a Func since the validator is static?)

Thank You,
Miguel

New Post: Use HttpContext.User in Validator

$
0
0
Yep, lazily-loading it with a Func is probably the best way to go.

Jeremy

Created Unassigned: Set Validator with Arguments [7160]

$
0
0
Hello,

I created a validator to specifically validate a User Email within many UserModels:

```
public class UserEmailValidator : AbstractValidator<String> {

public UserEmailValidator(Func<IDispatcher> dispatcher, Func<IWebUser> user) {

RuleFor(x => x)
.EmailAddress().WithMessage("The email is invalid")
.Must(x => dispatcher().Send<ValidateUserEmailReply>(new ValidateUserEmailQuery(new Guid(user().Identity.GetUserId()), x)).Valid).WithMessage("The email is unavailable");

}
}
```

Then in a User Model Validator I have:

```
RuleFor(x => x.Email)..SetValidator(new UserEmailValidator());
```

But this does not compile because the UserEmailValidator does not have an empty constructor.

Is there a way to attach this validator and use it without passing the dispatcher and webuser from the parent validator?

What approach would you use?

Thank You,
Miguel




New Post: Use HttpContext.User in Validator

$
0
0
I will do that.

Thank You,
Miguel

Commented Unassigned: Set Validator with Arguments [7160]

$
0
0
Hello,

I created a validator to specifically validate a User Email within many UserModels:

```
public class UserEmailValidator : AbstractValidator<String> {

public UserEmailValidator(Func<IDispatcher> dispatcher, Func<IWebUser> user) {

RuleFor(x => x)
.EmailAddress().WithMessage("The email is invalid")
.Must(x => dispatcher().Send<ValidateUserEmailReply>(new ValidateUserEmailQuery(new Guid(user().Identity.GetUserId()), x)).Valid).WithMessage("The email is unavailable");

}
}
```

Then in a User Model Validator I have:

```
RuleFor(x => x.Email)..SetValidator(new UserEmailValidator());
```

But this does not compile because the UserEmailValidator does not have an empty constructor.

Is there a way to attach this validator and use it without passing the dispatcher and webuser from the parent validator?

What approach would you use?

Thank You,
Miguel




Comments: Hi No - you'd need to take these as arguments to the parent validator too.

Closed Unassigned: Set Validator with Arguments [7160]

$
0
0
Hello,

I created a validator to specifically validate a User Email within many UserModels:

```
public class UserEmailValidator : AbstractValidator<String> {

public UserEmailValidator(Func<IDispatcher> dispatcher, Func<IWebUser> user) {

RuleFor(x => x)
.EmailAddress().WithMessage("The email is invalid")
.Must(x => dispatcher().Send<ValidateUserEmailReply>(new ValidateUserEmailQuery(new Guid(user().Identity.GetUserId()), x)).Valid).WithMessage("The email is unavailable");

}
}
```

Then in a User Model Validator I have:

```
RuleFor(x => x.Email)..SetValidator(new UserEmailValidator());
```

But this does not compile because the UserEmailValidator does not have an empty constructor.

Is there a way to attach this validator and use it without passing the dispatcher and webuser from the parent validator?

What approach would you use?

Thank You,
Miguel




Created Unassigned: Specify rule set when using ValidateAndThrow [7161]

$
0
0
Is it not possible to specify rule sets to use when using ValidateAndThrow?

Reviewed: 5.0 (Dec 12, 2013)

$
0
0
Rated 5 Stars (out of 5) - Honestly: This makes validation fun. If you have any sort of complicated validation scenario, skip data annotations and use this.

New Post: Help with creating a custom validator

$
0
0
Hi to all,

i would like to create a custom validator and i need some help.

My validator needs four parameters (the object being validated, the parent object being validated, a boolean and the PropertyValidatorContext)

The boolean is a property of the parent object being validated, but because this property can be different each time i call the validator I want to give this as parameter using a lambda expression.

Searching on the documentation i found how to create custom validators, but i'm not able/don't know if it's possible to create this kind of validator.

Can someone please help me?

New Post: Custom Validator With Arguments

$
0
0
Hi, I want to implement a validator which would check if the property value is within a certain collection (list, params, array, etc.).

It's a simple validator and I realize that I can use 'Must' to achieve this, but can this be done using a custom [Property] validator? I think the main blocker for me is that I don't know how to include arguments in the custom validator.

Thanks!

New Post: Help with creating a custom validator

New Post: Custom Validator With Arguments

$
0
0
Yep, anything like this can be done within a custom property validator. Custom property validators have access to both the property being validated and its parent instance via the context parameter - you should be able to access any other properties of the object through this.

Jeremy

New Post: Custom Validator With Arguments

$
0
0
Hi Jeremy, thanks for your answer. Sorry, perhaps it wasn't clear what I wanted to do in my original post - what I want to access is not a property of the parent instance, but rather a supplied argument. Something similar (in syntax) to the 'Equal' or 'Length' validators, where an argument is supplied. Is this possible?

OJ

New Post: Custom Validator With Arguments

$
0
0
Yes, this is perfectly possible. All the built-in validators (such as equal/length etc) are built as custom property validators under the covers. Essentially, you just take parameters as constructor arguments for your custom validator. Here's a snippet from the FluentValidation code for Length:


This is the extension method that gives the syntax:
public static class Extensions {
  public static IRuleBuilderOptions<T, string> Length<T>(this IRuleBuilder<T, string> ruleBuilder, int min, int max) {
    return ruleBuilder.SetValidator(new LengthValidator(min, max));
  }
}
...and the underlying validator:
public class LengthValidator : PropertyValidator, ILengthValidator {
        public int Min { get; private set; }
        public int Max { get; private set; }

        public LengthValidator(int min, int max) : this(min, max, () => Messages.length_error) {
        }

        public LengthValidator(int min, int max, Expression<Func<string>> errorMessageResourceSelector) : base(errorMessageResourceSelector) {
            Max = max;
            Min = min;

            if (max != -1 && max < min) {
                throw new ArgumentOutOfRangeException("max", "Max should be larger than min.");
            }
        }

        protected override bool IsValid(PropertyValidatorContext context) {
            if (context.PropertyValue == null) return true;

            int length = context.PropertyValue.ToString().Length;

            if (length < Min || (length > Max && Max != -1)) {
                context.MessageFormatter
                    .AppendArgument("MinLength", Min)
                    .AppendArgument("MaxLength", Max)
                    .AppendArgument("TotalLength", length);

                return false;
            }

            return true;
        }
    }
...so essentially the custom arguments are passed to the custom validator through its constructor. If you'd like to see more examples, check out the implementation in the FV code:

https://github.com/JeremySkinner/FluentValidation/tree/master/src/FluentValidation/Validators

Jeremy

New Post: Custom Validator With Arguments


New Post: Custom Validator With Arguments

New Post: Help with creating a custom validator

$
0
0
Hi Jeremy,

thanks for the reply, after some research i got it.

I just had to add a Func as constructor parameter to my custom validator and add a Func parameter for the extension method for using my validator as chain.

New Post: Till what extend fluent validation can be used in ASP.NET Webform (not MVC) patterns?

$
0
0
I am planning to use this in my ASP.NET application, which is in web form patterns. I know we can use Fluent Validations just for server side validations in web forms, but I want to know if there are some ways to configure it so that it can also be also used for client side validations (by keeping html input name=propertyName or something...)

New Post: Till what extend fluent validation can be used in ASP.NET Webform (not MVC) patterns?

$
0
0
Hi

Client side validation only works with MVC, sorry.

Jeremy

Created Unassigned: Add release notes to NuGet package [7162]

$
0
0
It would be very handy if the changelogs/release notes were added to the NuGet package, so you can view them directly when updating the package.

The .nuspec provides a <releaseNotes> tag for this, which could either contain the notes directly, or at least link to http://fluentvalidation.codeplex.com/SourceControl/latest#Changelog.txt .
Viewing all 1917 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>