0

I need following attributes:

1.For example: i have 2 field. first is checkbox, second is textbox. If first control checked. second field must be Required attribute. first control unchecked. second control not required.

[Required]
public boolean showHeader{get;set;}

[IFRequired("showHeader",true)]
public string HeaderText{get;set;}

2.For example: i have 2 field. new password, confirmation password. Attribute must check this 2 field are equal.

[Required]
public string newPassword{get;set;}

[Expression("newPassword",ExpressionAttributeEnum.Equils)]
public string confirmPassword{get;set;}

How to create above attributes?

NakedBrunch
  • 48,713
  • 13
  • 73
  • 98
ebattulga
  • 10,774
  • 20
  • 78
  • 116

2 Answers2

0

This is not possible using attributes.

however you can do it quite easy in your action.

public ActionResult Create(FormCollection collection) {
    ///do your checks here which you cant do using attributes
    ModelState.AddModelError("fieldname", "yourErrorMessage");

    if (ModelState.IsValid) {
        ////.........
    }
    return View();
}
Stefanvds
  • 5,868
  • 5
  • 48
  • 72
0

For point 2 if you're using MVC3 use the Compare attribute

[Required] public string newPassword{get;set;}

[Compare(] public string confirmPassword{get;set;}

Have a look at this SO question and answer for the first part

Community
  • 1
  • 1
heads5150
  • 7,263
  • 3
  • 26
  • 34