DCSIMG
March 2010 - Posts - Avi Pinto

Avi Pinto

על הבלוג

Follow uberPinto on Twitter

View Avi Pinto's profile on LinkedIn

Subscribe in a reader Subscribe by Email

 


Two new sculptures

View my Air Brush Work at Avipinto.com

Helping a friend, great tool for fixing file names

JavaScript Tutorial


Disclaimer All postings/content on this blog are licensed under a Creative Commons Attribution By license and provided "AS IS" with no warranties, and confer no rights.
All entries in this blog are my opinion and don't necessarily reflect the opinion of my employer or sponsors.

March 2010 - Posts

Alt.Net Israel #3 - the programming languages talk(mostly on F#)

מאוד נהנתי בכנס האלט נט ביום שישי שעבר, גדי תאר אותו בצורה מצויינת.

צילמתי את הקבוצה שדנה בנושא שפות תכנות, אבל תכלס בעיקר דיברנו שם על F# ותכנות פונקציונאלי בכלל, והאם זה טוב לנו.
מצטער מראש על האיכות(למרות שהופתעתי לטובה) והחלוקה לחמישה חלקים - צילמתי במצלמת סטילס (canonPowerShot s3),
שמסוגלת לצלם עד 10-15 דקות בכל פעם(אני יודע שאני יכול לאחד אל הסרטונים, אבל זו התעסקות שלוקחת זמן).
 
למעט הדגמה קצרצרה על הלוח, היו בעיקר דיבורים, כך שאם יותר נוח לכם, תמירו ל MP3 ותהנו משיחה מעניינת.
תהנו:
חלק 1:
 
חלק 2:

 
חלק 3:

 
חלק 4:

 
חלק 5:

Simple RTFEM OR why asp.net MVC [ValidateInput(false)] doesn't disable request validation on asp.net 4

Have been doing a lot of asp.new MVC the past three months, learning it as we build the project(hope to share more in the near future).
We are using visual studio 2010, so now we're stuck with MVC2 RC1 since RTM wasn't shipped for the the RC version of 2010 (and yes this is a huge rant!!, we are stuck with 2010 since we also chose to work with entity framework 4 which only work on framework 4.0).
This is why i suspect every feature that doesn't work well as a problem of an early release.

Before i get to the point, just wanted to state that i really like the MVC framework, and i don't even consider moving back to WebForms.
it is much more natural to write code this way, and the thing i like the most is that it is extensible and Microsoft have released all it's code, so if something doesn't behave well - ditch it and replace it, using the supplied code as a reference.

Now to the point - RTFEM == Read The Fucking Error Message.
well.. nobody hardly does, and if the error is too long - nobody reads it.
At one of my forms i use a Rich Text Editor (TinyMCE if you must know), so i have to disable request validation or else i'll get a server error for posting potentially....

In WebForms you just set the validateRequest="false" at the page level, and you're OK.
in MVC
disabling this feature is a bit different,
setting validateRequest="false" on the aspx or at the web.config won't cut it since these are only views, the controller handles the request first and just use the view as template.
So the MVC team gave us the [ValidateInput(false)] attribute to disable this annoying feature.
But just setting it on an action will also fail, you still have to set one more setting at the web.config for this to work(if you are working with asp.net 4):
<system.web>
...
<httpRuntime requestValidationMode="2.0" />
...
</system.web>

you can read at the ASP.NET 4 Release Candidate (RC) Breaking Changes that now all requests to the server "enjoy" this validation,
and not just aspx pages, which is OK and more secure, but MS should have gave us a better way to disable it for specific requests, instead of just reverting to the 2.0 mode.
as the above paper states,request validation is enabled before the beginRequest stage, so i don't believe that at the RTM it will be fixed.

As for the time it took me to realize that and add the web.config entry, the answer was in front of me all the time in the ugly YSOD(yes i feel stupid now),
I just ignored it because it had all this WebForms specific error data and instructions that i know are not the answer.

I would expect a change at the error message from MS for the MVC people(or better - that putting [ValidateInput(false)]will just work) , but that's just me dreaming.

The error message(the yellow part is the relevant part for me): 

A potentially dangerous Request.Form value was detected from the client (SomeLie="<b>Bold Lie</b>").

Description: Request Validation has detected a potentially dangerous client input value, and processing of the request has been aborted. This value may indicate an attempt to compromise the security of your application, such as a cross-site scripting attack. To allow pages to override application request validation settings, set the requestValidationMode attribute in the httpRuntime configuration section to requestValidationMode="2.0". Example: <httpRuntime requestValidationMode="2.0" />. After setting this value, you can then disable request validation by setting validateRequest="false" in the Page directive or in the <pages> configuration section. However, it is strongly recommended that your application explicitly check all inputs in this case. For more information, see http://go.microsoft.com/fwlink/?LinkId=153133.

Exception Details: System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client (SomeLie="<b>Bold Lie</b>").

 

P.S.
when looking for the solution, encountered this http://stackoverflow.com/questions/1461330/validateinput-attribute-doesnt-seem-to-work-in-asp-net-mvc
This shows another thing to look for - "If there are any calls to other actions in the request context (e.g. from the Master Page or child partials) these actions also need to have [ValidateInput(false)] set appropriately"

hope this helps.

והסבר קצר בעברית: יתווסף פה בהמשך.. עייף מידיי כרגע, מצטער.
רק תקפידו לקרוא את הודעות השגיאה, גם אם נראה שהן לא לגמריי קשורות.

 
 

jQuery Gotcha: val() might select the wrong option at a select element

Let's imagine you have the following select element:

<select id="someSelect">
  <option value="">-Select-</option>
  <option value="12">21</option>
  <option value="11">20</option>
  <option value="10">19</option>
  <option value="9">18</option>
  <option value="8">17</option>
  <option value="7">16</option>
  <option value="6">15</option>
  <option value="5">14</option>
  <option value="4">13</option>
  <option value="3">12</option>
  <option value="2">11</option>
  <option value="1">10</option>
</select>

Notice that the text in some options appears as value in other options.
you can get a select like this when composing an age drop down for example.

Now for the Gotcha: which option will be chosen as a result of the following expression:
$("#someSelect").val("12");
???
Answer: the selected option will be <option value="3">12</option>
and not the option with the value of 12 as we intended.

Why this happened?
Let's look at the part of the jQuery(varsion 1.3.2) val() function that handles the Select:

...
else if ( jQuery.nodeName( this, "select" ) )
{
  var values = jQuery.makeArray(value);
  jQuery( "option", this ).each(function()
  {
    this.selected = (jQuery.inArray( this.value, values ) >= 0 || jQuery.inArray( this.text, values ) >= 0);
  });
  if ( !values.length )
    this.selectedIndex = -1;
}
...

it loops on all the items of the select and tries to match either the value or the text against the input value,
it was designed to help us select options by text, but i find it a bit annoying...

How to avoid this Gotcha?
for select elements of this nature you can use:
$("#someSelect option[value=" + val + "]").attr("selected", "selected");
or
jqThePrefoundSelect.find("option[value=" + val + "]").attr("selected", "selected");
or... well you get the point - just make sure you find the option by it's value and not it's text.

hope this helps.

והסבר קצר בעברית:
שימו לב ל select שהצגתי לעיל,
בחלק מה options שלו יש ערכים שמופיעים כטקסט ב options אחרים.
זהו תרחיש אפשרי למשל במתן אפשרות לבחירת גיל של משתמש.

במידה ונרצה לבחור ערך באמצעות jQuery בצורה הבאה:
$("#someSelect").val("12");

במקום שתיבחר ה option בעלת הערך 12(כמו שרצינו), תיבחר ה option בעלת הערך 3, כיוון שהטקסט שלה מכיל 12
את הסיבה לכך הצגתי בקטע הקוד מהפונקציה val לעיל(קטע הקוד הבעייתי מסומן בצהוב):
רצים בלולואה על כל ה options ומנסים למצוא התאמה לערך ולטקסט,
זה נועד לאפשר בחירה קלה של ערך לפי הטקסט שלו, אבל במקרה שלנו זה משבש את העניינים.

אז מה עושים?
מוודאים שבוחרים את ה option הנכון לפי הערך שלו, ולא לפי הטקסט.
הצגתי לעיל שתי אפשרויות(כמובן שאפשר בעוד דרכים, גם לולאה פשוטה ב JS תעשה את העבודה)
אחת מהצורות שהצגתי:
$("#someSelect option[value=" + val + "]").attr("selected", "selected");
 
 

ALT.NET Israel #3 - Spring 2010

שריינו את יום שישי ה 26.3.10 (ואת הערב של יום חמישי שלפניו).
בשעה טובה כנס alt.net israel נוסף יוצא לדרך.
 
כל הפרטים נוספים בקבוצה בגוגל

ולמי שאין פייסבוק, הנה הפרטים המינימלים שצריך לדעת ע"מ להגיע:
תאריך התחלה: Thursday, March 25, 2010 at 6:00pm
תאריך סיום:  Friday, March 26, 2010 at 4:00pm
מיקום: משרדי חברת סירס ישראל (SHC) - המנופים 9 הרצליה פיתוח (מפה)
         בניין אקרשטיין A, קומה 3, על הדלת כתוב SHC ISRAE
 

הייתי בשני הכנסים הקודמים והיה ממש מעניין, וכמובן, אחלה אנשים.
קצת הרצאות מהכנס הקודם בפוסט ישן שלי
ועוד לינקים להרצאות מהכנס הראשון בדלישס שלי.
 
וכמובן תודה לחן אגוזי (AKA ken) על היוזמה והארגון,
לחברת סירס ישראל על האירוח
ולחברת טייפמוק על הכיבוד
 
נתראה שם.