I have made a review ui where the user clicks on a star to give a rating, this populates a hidden field with the rating, i have a range validator 1 to 5, if i use a textboxfor instead of the hiddenfor i get validation, but with the hiddenfor i dont.
How can i get validation when using a hidden for, is there a dataanotation for this?
How can i get validation when using a hidden for, is there a dataanotation for this?
A simple and quick way is hidden textbox,
Html.TextBoxFor(a => a.FirstName, new { style="display:none;"})
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
i already did somthing with javascript, I did consider the solution you purpose also. I was wondering if there was a inbuilt solution rather then a workaround, but alas i had to settle for a workaround.
The code below forces validation of hidden fields:
$.validator.setDefaults({
ignore: ""
});
If you substitute the empty string with a selector....you can specify what to ignore. The default is :hidden that cause all hidden fileds or all fields with display:none to be ignored. Thus also putting a textbox with display:none doesn't help.
ThatsIT
Participant
1026 Points
388 Posts
Valdating a hidden field
Mar 29, 2012 04:03 PM|LINK
I have made a review ui where the user clicks on a star to give a rating, this populates a hidden field with the rating, i have a range validator 1 to 5, if i use a textboxfor instead of the hiddenfor i get validation, but with the hiddenfor i dont.
How can i get validation when using a hidden for, is there a dataanotation for this?
Thanks
snajp3er
Member
97 Points
26 Posts
Re: Valdating a hidden field
Mar 29, 2012 04:22 PM|LINK
Maybe it helps http://www.gregshackles.com/2010/02/validating-hidden-fields-in-asp-net-mvc-2/
ThatsIT
Participant
1026 Points
388 Posts
Re: Valdating a hidden field
Mar 29, 2012 04:25 PM|LINK
Thanks, i did see that article, but since that was mvc2, i hopped there was a simpler way to do it now
imran_ku07
All-Star
45785 Points
7698 Posts
MVP
Re: Valdating a hidden field
Apr 01, 2012 06:01 PM|LINK
A simple and quick way is hidden textbox,
Html.TextBoxFor(a => a.FirstName, new { style="display:none;"})
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
ThatsIT
Participant
1026 Points
388 Posts
Re: Valdating a hidden field
Apr 01, 2012 06:05 PM|LINK
Thanks,
i already did somthing with javascript, I did consider the solution you purpose also. I was wondering if there was a inbuilt solution rather then a workaround, but alas i had to settle for a workaround.
Maybe in mvc 5
Thanks again
francesco ab...
All-Star
20888 Points
3277 Posts
Re: Valdating a hidden field
Apr 01, 2012 06:30 PM|LINK
The code below forces validation of hidden fields:
$.validator.setDefaults({ ignore: "" });If you substitute the empty string with a selector....you can specify what to ignore. The default is :hidden that cause all hidden fileds or all fields with display:none to be ignored. Thus also putting a textbox with display:none doesn't help.
Mvc Controls Toolkit | Data Moving Plug-in Videos
ThatsIT
Participant
1026 Points
388 Posts
Re: Valdating a hidden field
Apr 01, 2012 06:42 PM|LINK
Thanks francesco, work a treat.