Rectangle 27 452

Option 3: Do not validate

Paperclip version 4.0
Paperclip::Errors::MissingRequiredValidatorError

In your case, you can add any of the following line to your Post model, after specifying has_attached_file :image

validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
validates_attachment :image, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] }

For example: To validate all image formats, regex expression can be specified as shown in

validates_attachment_file_name :image, :matches => [/png\Z/, /jpe?g\Z/, /gif\Z/]

If for some crazy reason (can be valid but I cannot think of one right now), you do not wish to add any content_type validation and allow people to spoof Content-Types and receive data you weren't expecting onto your server then add the following:

do_not_validate_attachment_file_type :image

Specify the MIME types as per your requirement within content_type/ matches options above.

You might also have to deal with the spoofing validation explained here https://stackoverflow.com/a/23846121

validates_attachment :image, presence: true, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png"] }

@rawonstack +1 Thanks for suggesting the alternative. :) I have included that in answer after a little tweak. presence: true validation is not mandatory so I have excluded that part.

Paperclip will also throw "Missing Required Validator Error" in case of PDF file upload. The workaround for that is: First install the "GhostScript" and then add "application/pdf" to content-type.

I would really not recommend do_not_validate_attachment_file_type . As Rdocs puts it: Thanks to a report from Egor Homakov we have taken steps to prevent people from spoofing Content-Types and getting data you weren't expecting onto your server.

My crazy reason for not doing content validation is because the attachment is not created by the users but by a system process. Paperclip is the convenience layer for S3 storage.

ruby - Paperclip::Errors::MissingRequiredValidatorError with Rails 4 -...

ruby-on-rails ruby ruby-on-rails-4 paperclip
Rectangle 27 452

Option 3: Do not validate

Paperclip version 4.0
Paperclip::Errors::MissingRequiredValidatorError

In your case, you can add any of the following line to your Post model, after specifying has_attached_file :image

validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
validates_attachment :image, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] }

For example: To validate all image formats, regex expression can be specified as shown in

validates_attachment_file_name :image, :matches => [/png\Z/, /jpe?g\Z/, /gif\Z/]

If for some crazy reason (can be valid but I cannot think of one right now), you do not wish to add any content_type validation and allow people to spoof Content-Types and receive data you weren't expecting onto your server then add the following:

do_not_validate_attachment_file_type :image

Specify the MIME types as per your requirement within content_type/ matches options above.

You might also have to deal with the spoofing validation explained here https://stackoverflow.com/a/23846121

validates_attachment :image, presence: true, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png"] }

@rawonstack +1 Thanks for suggesting the alternative. :) I have included that in answer after a little tweak. presence: true validation is not mandatory so I have excluded that part.

Paperclip will also throw "Missing Required Validator Error" in case of PDF file upload. The workaround for that is: First install the "GhostScript" and then add "application/pdf" to content-type.

I would really not recommend do_not_validate_attachment_file_type . As Rdocs puts it: Thanks to a report from Egor Homakov we have taken steps to prevent people from spoofing Content-Types and getting data you weren't expecting onto your server.

My crazy reason for not doing content validation is because the attachment is not created by the users but by a system process. Paperclip is the convenience layer for S3 storage.

ruby - Paperclip::Errors::MissingRequiredValidatorError with Rails 4 -...

ruby-on-rails ruby ruby-on-rails-4 paperclip
Rectangle 27 1

Option 3: Do not validate

Paperclip version 4.0
Paperclip::Errors::MissingRequiredValidatorError

In your case, you can add any of the following line to your Post model, after specifying has_attached_file :image

validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
validates_attachment :image, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] }
validates_attachment_file_name :avatar, :matches => [/png\Z/, /jpe?g\Z/, /gif\Z/]

If for some crazy reason (can be valid but I cannot think of one right now), you do not wish to add any content_type validation and allow people to spoof Content-Types and receive data you weren't expecting onto your server then add the following:

do_not_validate_attachment_file_type :image

Specify the MIME types as per your requirement within content_type/ matches options above.

added the link in the answer. thanks for suggestion :)

Rails paperclip not showing image - Stack Overflow

ruby-on-rails paperclip
Rectangle 27 1

Option 3: Do not validate

Paperclip version 4.0
Paperclip::Errors::MissingRequiredValidatorError

In your case, you can add any of the following line to your Post model, after specifying has_attached_file :image

validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
validates_attachment :image, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] }
validates_attachment_file_name :avatar, :matches => [/png\Z/, /jpe?g\Z/, /gif\Z/]

If for some crazy reason (can be valid but I cannot think of one right now), you do not wish to add any content_type validation and allow people to spoof Content-Types and receive data you weren't expecting onto your server then add the following:

do_not_validate_attachment_file_type :image

Specify the MIME types as per your requirement within content_type/ matches options above.

added the link in the answer. thanks for suggestion :)

Rails paperclip not showing image - Stack Overflow

ruby-on-rails paperclip
Rectangle 27 15

To enable eager validation on the jquery validation plugin, add the following option to the validate function:

$('#myform').validate({
    onfocusout: function(element) {
        $(element).valid();
    }
});

Jquery Validation Plugin - can you enable "eager" validation from the ...

jquery validation jquery-plugins
Rectangle 27 28

Seems like you are going to have limited options since you want the check to occur before the upload. I think the best you are going to get is to use javascript to validate the extension of the file. You could build a hash of valid extensions and then look to see if the extension of the file being uploaded existed in the hash.

<input type="file" name="FILENAME"  size="20" onchange="check_extension(this.value,"upload");"/>
<input type="submit" id="upload" name="upload" value="Attach" disabled="disabled" />
var hash = {
  'xls'  : 1,
  'xlsx' : 1,
};

function check_extension(filename,submitId) {
      var re = /\..+$/;
      var ext = filename.match(re);
      var submitEl = document.getElementById(submitId);
      if (hash[ext]) {
        submitEl.disabled = false;
        return true;
      } else {
        alert("Invalid filename, please select another file");
        submitEl.disabled = true;

        return false;
      }
}

don't forget, this is only Client Side validation. I can still 'manually' HTTP-POST to the form url to bypass this check - you'll need to do a server side validation check also.

.doc
.docx

this will return false when the filename contains extra .'s eg my.file.doc

I used above but got the extension of the file using :'var ext = value.slice(value.lastIndexOf(".")).toLowerCase();'

+ 1

asp.net - How do I Validate the File Type of a File Upload? - Stack Ov...

asp.net javascript validation file upload
Rectangle 27 6

Angular2 FORM with controls and validation.

Basically we can ngControl for both to get values of forms as well as for validations but there are some probelms using this methods so best solution is according to me use ngModel for getting values of the form into your class and use ngControl for the validation purpose. there are default validators provide by angular to check validation we can create our custom one too as per need and can use in the validation (ngControl). if we are going to create model driven form i.e we have to create new control for every input by using new Control(). for the Control, control group and validation refer this best artical

here is the basic example of using controls for the form:

this.CreateGroup = fb.group({
            'name': new Control(this.demoInfo.name, Validators.required),
            'password': new Control(this.demoInfo.password, Validators.required),
            'select': new Control(this.demoInfo.select, Validators.required)
        })

Here i have three inputs named name, password,select respectively. and corresponding i have mentioned their values and validators (default validation).

<input type="text" [(ngModel)]='demoInfo.name' ngControl='name'>

After a lot searching i concluded that using ngModel is best to get values from form. by using same it is easier to clear to controls of the forms. and validations gets easy. and used ngControl for checking validations.

<form class="form-horizontal" id='myForm' role="form" [ngFormModel]="CreateGroup">

  <div class="col-md-7">
    Name: <input type="text" [(ngModel)]='demoInfo.name' class="form-control" ngControl='name'>
  </div>

  <div class="col-md-7">
    Password:   <input type="password" [(ngModel)]='demoInfo.password' class="form-control" ngControl='password'>
  </div>

  <div class="col-md-7">
    <input type="radio" name='type' (click)='demoInfo.radio="Btech"' [checked]="'Btech' === demoInfo.radio">Btech
    <input type="radio" name='type' (click)='demoInfo.radio="Mtech"' [checked]="'Mtech' === demoInfo.radio">Mtech
  </div>

  <div class="col-md-7">
    <select #selectOption (change)='demoInfo.select=selectOption.value' class='form-control' ngControl='select'>
      <option> select</option>
      <option value='One' [selected]="demoInfo.select==='One'">One Value</option>
      <option value='Two' [selected]="demoInfo.select==='Two'">two Value</option>
      <option value='Three' [selected]="demoInfo.select==='Three'">Three Value</option>
    </select>
  </div>
</form>
<br>
<div class='text-center'>
  <button type="button" [disabled]='!CreateGroup.valid' (click)="addNewGroup(demoInfo)" class="btn btn-primary">Create</button>
</div>

and code for the class side is here...

import {Component} from 'angular2/core';
import {CORE_DIRECTIVES, NgClass, FORM_DIRECTIVES, Control, ControlGroup, FormBuilder, Validators} from 'angular2/common';

class DemoInfo{
  name:string;
  password: string;
  radio: any;
  select: any;
}
@Component({
    selector: 'my-app',
    templateUrl: 'mytemplate.html',
    directives: [CORE_DIRECTIVES, FORM_DIRECTIVES] 
})
export class AppComponent { 
  CreateGroup: FormBuilder;
  demoInfo: DemoInfo;
  constructor(fb: FormBuilder){
    this.demoInfo= new DemoInfo(); 

    this.CreateGroup = fb.group({
            'name': new Control(this.demoInfo.name, Validators.required),
            'password': new Control(this.demoInfo.password, Validators.required),
            'select': new Control(this.demoInfo.select, Validators.required)
        })
  }
  addNewGroup(demoInfo:demoInfo) {
    console.log(demoInfo, 'whole object');
    this.demoInfo= new DemoInfo();
  }
}

angular - Forms In angular2 - Stack Overflow

angular angular2-forms
Rectangle 27 6

Angular2 FORM with controls and validation.

Basically we can ngControl for both to get values of forms as well as for validations but there are some probelms using this methods so best solution is according to me use ngModel for getting values of the form into your class and use ngControl for the validation purpose. there are default validators provide by angular to check validation we can create our custom one too as per need and can use in the validation (ngControl). if we are going to create model driven form i.e we have to create new control for every input by using new Control(). for the Control, control group and validation refer this best artical

here is the basic example of using controls for the form:

this.CreateGroup = fb.group({
            'name': new Control(this.demoInfo.name, Validators.required),
            'password': new Control(this.demoInfo.password, Validators.required),
            'select': new Control(this.demoInfo.select, Validators.required)
        })

Here i have three inputs named name, password,select respectively. and corresponding i have mentioned their values and validators (default validation).

<input type="text" [(ngModel)]='demoInfo.name' ngControl='name'>

After a lot searching i concluded that using ngModel is best to get values from form. by using same it is easier to clear to controls of the forms. and validations gets easy. and used ngControl for checking validations.

<form class="form-horizontal" id='myForm' role="form" [ngFormModel]="CreateGroup">

  <div class="col-md-7">
    Name: <input type="text" [(ngModel)]='demoInfo.name' class="form-control" ngControl='name'>
  </div>

  <div class="col-md-7">
    Password:   <input type="password" [(ngModel)]='demoInfo.password' class="form-control" ngControl='password'>
  </div>

  <div class="col-md-7">
    <input type="radio" name='type' (click)='demoInfo.radio="Btech"' [checked]="'Btech' === demoInfo.radio">Btech
    <input type="radio" name='type' (click)='demoInfo.radio="Mtech"' [checked]="'Mtech' === demoInfo.radio">Mtech
  </div>

  <div class="col-md-7">
    <select #selectOption (change)='demoInfo.select=selectOption.value' class='form-control' ngControl='select'>
      <option> select</option>
      <option value='One' [selected]="demoInfo.select==='One'">One Value</option>
      <option value='Two' [selected]="demoInfo.select==='Two'">two Value</option>
      <option value='Three' [selected]="demoInfo.select==='Three'">Three Value</option>
    </select>
  </div>
</form>
<br>
<div class='text-center'>
  <button type="button" [disabled]='!CreateGroup.valid' (click)="addNewGroup(demoInfo)" class="btn btn-primary">Create</button>
</div>

and code for the class side is here...

import {Component} from 'angular2/core';
import {CORE_DIRECTIVES, NgClass, FORM_DIRECTIVES, Control, ControlGroup, FormBuilder, Validators} from 'angular2/common';

class DemoInfo{
  name:string;
  password: string;
  radio: any;
  select: any;
}
@Component({
    selector: 'my-app',
    templateUrl: 'mytemplate.html',
    directives: [CORE_DIRECTIVES, FORM_DIRECTIVES] 
})
export class AppComponent { 
  CreateGroup: FormBuilder;
  demoInfo: DemoInfo;
  constructor(fb: FormBuilder){
    this.demoInfo= new DemoInfo(); 

    this.CreateGroup = fb.group({
            'name': new Control(this.demoInfo.name, Validators.required),
            'password': new Control(this.demoInfo.password, Validators.required),
            'select': new Control(this.demoInfo.select, Validators.required)
        })
  }
  addNewGroup(demoInfo:demoInfo) {
    console.log(demoInfo, 'whole object');
    this.demoInfo= new DemoInfo();
  }
}

angular - Forms In angular2 - Stack Overflow

angular angular2-forms
Rectangle 27 6

Angular2 FORM with controls and validation.

Basically we can ngControl for both to get values of forms as well as for validations but there are some probelms using this methods so best solution is according to me use ngModel for getting values of the form into your class and use ngControl for the validation purpose. there are default validators provide by angular to check validation we can create our custom one too as per need and can use in the validation (ngControl). if we are going to create model driven form i.e we have to create new control for every input by using new Control(). for the Control, control group and validation refer this best artical

here is the basic example of using controls for the form:

this.CreateGroup = fb.group({
            'name': new Control(this.demoInfo.name, Validators.required),
            'password': new Control(this.demoInfo.password, Validators.required),
            'select': new Control(this.demoInfo.select, Validators.required)
        })

Here i have three inputs named name, password,select respectively. and corresponding i have mentioned their values and validators (default validation).

<input type="text" [(ngModel)]='demoInfo.name' ngControl='name'>

After a lot searching i concluded that using ngModel is best to get values from form. by using same it is easier to clear to controls of the forms. and validations gets easy. and used ngControl for checking validations.

<form class="form-horizontal" id='myForm' role="form" [ngFormModel]="CreateGroup">

  <div class="col-md-7">
    Name: <input type="text" [(ngModel)]='demoInfo.name' class="form-control" ngControl='name'>
  </div>

  <div class="col-md-7">
    Password:   <input type="password" [(ngModel)]='demoInfo.password' class="form-control" ngControl='password'>
  </div>

  <div class="col-md-7">
    <input type="radio" name='type' (click)='demoInfo.radio="Btech"' [checked]="'Btech' === demoInfo.radio">Btech
    <input type="radio" name='type' (click)='demoInfo.radio="Mtech"' [checked]="'Mtech' === demoInfo.radio">Mtech
  </div>

  <div class="col-md-7">
    <select #selectOption (change)='demoInfo.select=selectOption.value' class='form-control' ngControl='select'>
      <option> select</option>
      <option value='One' [selected]="demoInfo.select==='One'">One Value</option>
      <option value='Two' [selected]="demoInfo.select==='Two'">two Value</option>
      <option value='Three' [selected]="demoInfo.select==='Three'">Three Value</option>
    </select>
  </div>
</form>
<br>
<div class='text-center'>
  <button type="button" [disabled]='!CreateGroup.valid' (click)="addNewGroup(demoInfo)" class="btn btn-primary">Create</button>
</div>

and code for the class side is here...

import {Component} from 'angular2/core';
import {CORE_DIRECTIVES, NgClass, FORM_DIRECTIVES, Control, ControlGroup, FormBuilder, Validators} from 'angular2/common';

class DemoInfo{
  name:string;
  password: string;
  radio: any;
  select: any;
}
@Component({
    selector: 'my-app',
    templateUrl: 'mytemplate.html',
    directives: [CORE_DIRECTIVES, FORM_DIRECTIVES] 
})
export class AppComponent { 
  CreateGroup: FormBuilder;
  demoInfo: DemoInfo;
  constructor(fb: FormBuilder){
    this.demoInfo= new DemoInfo(); 

    this.CreateGroup = fb.group({
            'name': new Control(this.demoInfo.name, Validators.required),
            'password': new Control(this.demoInfo.password, Validators.required),
            'select': new Control(this.demoInfo.select, Validators.required)
        })
  }
  addNewGroup(demoInfo:demoInfo) {
    console.log(demoInfo, 'whole object');
    this.demoInfo= new DemoInfo();
  }
}

angular - Forms In angular2 - Stack Overflow

angular angular2-forms
Rectangle 27 5

I would just add that for the error placement, it will append the element right next to hidden, therefore you may want to change the placement by using the following code on your validate function as an option argument

errorPlacement: function(error,element) {
        if (element.is(":hidden")) {
            //console.log(element.next().parent());
            element.next().parent().append(error);
        }
        else {
            error.insertAfter(element);
        }

    }

javascript - Chosen.js and validate jquery - Stack Overflow

javascript jquery validation jquery-chosen
Rectangle 27 6

If you validate , option 1 comes up as an error in html 5, so option 2 is correct.

Proper way to make HTML nested list? - Stack Overflow

html html-lists nested-lists
Rectangle 27 239

You can use the filter_var() function, which gives you a lot of handy validation and sanitization options.

filter_var($email, FILTER_VALIDATE_EMAIL)
function isValidEmail($email){ 
    return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}

Note: For other uses (where you need Regex), the deprecated ereg function family (POSIX Regex Functions) should be replaced by the preg family (PCRE Regex Functions). There are a small amount of differences, reading the Manual should suffice.

Update 2: This method will of course validate bazmega@kapa as a valid email address, because in fact it is a valid email address. But most of the time on the Internet, you also want the email address to have a TLD: bazmega@kapa.com. As suggested in this blog post (link posted by @Istiaque Ahmed), you can augment filter_var() with a regex that will check for the existence of a dot in the domain part (will not check for a valid TLD though):

function isValidEmail($email) {
    return filter_var($email, FILTER_VALIDATE_EMAIL) 
        && preg_match('/@.+\./', $email);
}

As @Eliseo Ocampos pointed out, this problem only exists before PHP 5.3, in that version they changed the regex and now it does this check, so you do not have to.

+1 That said, you might want to mention that this is only available in PHP 5.2.x and above. :-)

@middaparka: As the OP gets a deprecated message for eregi, it seems he is using PHP 5.3. But yes, it is important to mention it (for others)).

PHP 5.3.3 and 5.2.14 had a bug (bugs.php.net/52929) related to FILTER_VALIDATE_EMAIL, which resulted in segfault when validating large values. Simple and safe workaround for this is using strlen() before filter_val(). I'm not sure about 5.3.4 final, but it is written that some 5.3.4-snapshot versions also were affected.

filter_val
filter_var

regex - How to validate an Email in PHP? - Stack Overflow

php regex validation email-validation
Rectangle 27 239

You can use the filter_var() function, which gives you a lot of handy validation and sanitization options.

filter_var($email, FILTER_VALIDATE_EMAIL)
function isValidEmail($email){ 
    return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}

Note: For other uses (where you need Regex), the deprecated ereg function family (POSIX Regex Functions) should be replaced by the preg family (PCRE Regex Functions). There are a small amount of differences, reading the Manual should suffice.

Update 2: This method will of course validate bazmega@kapa as a valid email address, because in fact it is a valid email address. But most of the time on the Internet, you also want the email address to have a TLD: bazmega@kapa.com. As suggested in this blog post (link posted by @Istiaque Ahmed), you can augment filter_var() with a regex that will check for the existence of a dot in the domain part (will not check for a valid TLD though):

function isValidEmail($email) {
    return filter_var($email, FILTER_VALIDATE_EMAIL) 
        && preg_match('/@.+\./', $email);
}

As @Eliseo Ocampos pointed out, this problem only exists before PHP 5.3, in that version they changed the regex and now it does this check, so you do not have to.

+1 That said, you might want to mention that this is only available in PHP 5.2.x and above. :-)

@middaparka: As the OP gets a deprecated message for eregi, it seems he is using PHP 5.3. But yes, it is important to mention it (for others)).

PHP 5.3.3 and 5.2.14 had a bug (bugs.php.net/52929) related to FILTER_VALIDATE_EMAIL, which resulted in segfault when validating large values. Simple and safe workaround for this is using strlen() before filter_val(). I'm not sure about 5.3.4 final, but it is written that some 5.3.4-snapshot versions also were affected.

filter_val
filter_var

regex - How to validate an Email in PHP? - Stack Overflow

php regex validation email-validation
Rectangle 27 1

You can use the submitHandler from the validate options:

$(".selector").validate({
   submitHandler: function(form) {
    if (value > BIG_VALUE) {
        if (confirm("...")) {
           form.submit();
        }
    }
    else {
      form.submit();
    }
   }
})

Thanks. Sorry should have mentioned that I would like the message to appear as all the other validation error messages at the side of the element when they leave focus. I didn't know whether it was possible to still have a rule that fires after all the others are valid but at the same time mark the field as valid or something?

JQuery Validator Plugin - Stack Overflow

jquery jquery-plugins validation jquery-validate
Rectangle 27 39

I had the same issue, but realized that the Unobtrusive plugin was actually casuing the issue, it will override any options you set! The trick is to have your code written/included in the following order:

Why is this not the accepted answer?! I've battled with this for months and months on various legacy projects and came across this by chance. It makes so much sense now. Thank you.

This unobtrusive will wipe your options out, so use the accepted solution. Don't know if this is a change in the last 5 years, so may have worked previously

jquery - How can I customize the unobtrusive validation in ASP.NET MVC...

jquery asp.net asp.net-mvc-3 jquery-validate
Rectangle 27 18

A better option would be to use ViewModels.

how silly of me! Of course this is a nice way :) Silly me :) blond moment.

If the viewmodel fields have validation dataannotations on them, you will still get validation errors when navigating to the next step.

@awirgley the problem here isn't getting validation errors. It's getting the WRONG validation errors. you create a ViewModel for EACH part of the wizard, as they're just different views.

c# - Is there any way to ignore some properties (on a POCO) when valid...

c# asp.net-mvc validation asp.net-mvc-3
Rectangle 27 3

One option would be to validate page state in the SideBarButtonClick event, setting Cancel to true if it fails validation. Then your users should never reach the summary page with invalid data.

Wouldn't that trigger the same "bug"? The controls that aren't present in the current WizardStep when this event occurs would have the same state (stating that they are valid) when they are in fact not. The validation for the current step already runs when a link in the SideBar is clicked.

@Erik If the problem is the user missing out steps in thw wizard, I was thinking you could use the CurrentStepIndex property and the NextStepIndex properties of the WizardNavigationEventArgs to prohibit this?

c# - Validate all wizard steps on FinishButtonClick - Stack Overflow

c# asp.net validation aspwizard
Rectangle 27 14

Option 1 doesn't fit. The only way it would work would be to pull in the dependency via the service locator anti-pattern.

Option 2 doesn't work. Although I couldn't see how this was possible because of the C# attribute requirements, it is possible. See the following for references:

Option 3: I didn't know about this earlier, but what appears to be a very powerful way to write validators is to use the ModelValidator class and a corresponding ModelValidatorProvider.

public class CustomModelValidatorProvider : ModelValidatorProvider
{
    public CustomModelValidatorProvider(/* Your dependencies */) {}

    public override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context)
    {
        if (metadata.ModelType == typeof(YourModel))
        {
            yield return new YourModelValidator(...);
        }
    }
}

ASP.NET MVC's IDependencyResolver will attempt to resolve the above provider, so as long as it's registered with your IoC container you won't need to do anything else. And then the ModelValidator:

public class EntryRatingViewModelValidatorMvcAdapter : ModelValidator
{
    public EntryRatingViewModelValidatorMvcAdapter(
            ModelMetadata argMetadata,
            ControllerContext argContext)
                : base(argMetadata, argContext)
    {
        _validator = validator;
    }


    public override IEnumerable<ModelValidationResult> Validate(object container)
    {
        if (/* error condition */)
        {
            yield return new ModelValidationResult
              {
                MemberName = "Model.Member",
                Message = "Rating is required."
              };
        }
    }
}

As the provider is retrieved through the IDependencyResolver and the provider has full control over the returned ModelValidators I was easily able to inject the dependencies and perform necessary validation.

I am using MVC5 but I think the above it still relevant. You've said that "MVC's IDependencyResolver will attempt to resolve the provider". But how would you register the provider with the IoC container? (Unity in my case).

validation - ASP.NET MVC 3: Validating model when information external...

asp.net-mvc-3 validation
Rectangle 27 14

Option 1 doesn't fit. The only way it would work would be to pull in the dependency via the service locator anti-pattern.

Option 2 doesn't work. Although I couldn't see how this was possible because of the C# attribute requirements, it is possible. See the following for references:

Option 3: I didn't know about this earlier, but what appears to be a very powerful way to write validators is to use the ModelValidator class and a corresponding ModelValidatorProvider.

public class CustomModelValidatorProvider : ModelValidatorProvider
{
    public CustomModelValidatorProvider(/* Your dependencies */) {}

    public override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context)
    {
        if (metadata.ModelType == typeof(YourModel))
        {
            yield return new YourModelValidator(...);
        }
    }
}

ASP.NET MVC's IDependencyResolver will attempt to resolve the above provider, so as long as it's registered with your IoC container you won't need to do anything else. And then the ModelValidator:

public class EntryRatingViewModelValidatorMvcAdapter : ModelValidator
{
    public EntryRatingViewModelValidatorMvcAdapter(
            ModelMetadata argMetadata,
            ControllerContext argContext)
                : base(argMetadata, argContext)
    {
        _validator = validator;
    }


    public override IEnumerable<ModelValidationResult> Validate(object container)
    {
        if (/* error condition */)
        {
            yield return new ModelValidationResult
              {
                MemberName = "Model.Member",
                Message = "Rating is required."
              };
        }
    }
}

As the provider is retrieved through the IDependencyResolver and the provider has full control over the returned ModelValidators I was easily able to inject the dependencies and perform necessary validation.

I am using MVC5 but I think the above it still relevant. You've said that "MVC's IDependencyResolver will attempt to resolve the provider". But how would you register the provider with the IoC container? (Unity in my case).

validation - ASP.NET MVC 3: Validating model when information external...

asp.net-mvc-3 validation
Rectangle 27 4

Answering my own question. I deploy my site to S3 using s3cmd tool and there is an option you could provide to invalidate CloudFront cache of all the files changed (diff between your dist folder and S3 bucket). This invalidates cache of all the files changed including index file. It usually takes around 15-20 mins to reflect the new changes on production.

Here is the command

s3cmd sync --acl-public --reduced-redundancy --delete-removed --cf-invalidate [your-distribution-folder]/* s3://[your-s3-bucket]
brew install s3cmd
[your-distribution-folder]/*
ERROR: Parameter problem: Invalid source: 'WHATEVER_I_TRIED' is not an existing file or directory

Also is the /* required? Does it signify that everything in the distribution / bucket gets invalidated?

It should be the name of the bucket/folder you've configured as the root of the website.

And no. It doesn't invalidate everything. It checks for the difference in files using sync command and only invalidates files that have changed.

amazon web services - How do you invalidate cache of index.html for a ...

html amazon-web-services amazon-s3 cdn amazon-cloudfront