why does Razor put validation attributes in #Html.Hiddenfor() helper? - asp.net-mvc-3

it doesn't make any sense, is there anyway to make this not to act this way?
for
#Html.HiddenFor(model=>model.Id)
I get
<input type="hidden" value="e62fceab-588c-4777-bfe9-8516425a5028" name="Id" id="Id" data-val-required="The Id field is required." data-val="true">

MVC is automatically adding required validation to all non null-able fields. If you don't like this then you can make your id null-able.

It's just an additional layer of server side protection. It's trivial to change an outgoing hidden input with a man in the middle tool like Fiddler.
As for making it optional, there's almost surely a data attribute for that. Alternatively, adding a question mark after the property name in your model should do it.

Related

How to populate form after validation fails using custom formRequest class in laravel

I am using Custom ForRequest class to validate my form, it works fine. But when it redirects back on form page, the form is not populated.
I don't want to use
old('form_field_name')
My code will be weird if I use old('form_field_name') because I use same form to store and to update the model
What am I missing?
thanks
if you use the same form for store and update, use old with optional. just like
<input name="name" id="name" placeholder="Name" type="text" class="form-control" value="{{ old('name', optional($object)->name) }}">
this is the way you can get the old form values as well as the object values. if there is old value it will put that as value otherwise it will put the object value as value.
make sure to send the object as null from the create function.

How to hide parameter from url, when need to pass data from one page to another?

Instead of showing ../product/1
$breadcrumbs->push($name,url('/product/'.$id));
I want it to be displayed as ../product
Any method can be used to do this ?
Use hidden input field like:
<input name="id" value="{{$id}}" hidden>
Not a too good practice, But you can hide id parameter from url

Fighting Spam in Laravel 4

What are the most effective and/or easiest to implement methods for reducing spam entries on a comment form in Laravel?
I have tried https://github.com/msurguy/Honeypot, but the time field doesn't pass validation I think it is because I'm using ardent?
We implement this method at work and it stops almost all spam. You need to hide a text field (Using css "display:none" on a parent element. Don't use a hidden field, spambots know better) and when you validate the form, make sure that field has no content. If there is content, you know it is spam. Spam bots like to fill in as many fields as possible. Here is an example:
.special-field {
display:none;
}
<div class="special-field">
<label for="birthday">Birthday</label>
<input type="text" name="birthday" id="birthday" value="" />
</div>
Applying a name to the field may help to confuse spam bots as well, further encouraging them to fill in a value.

How to recognaize which ajax form it is in Django?

I have view which takes care of all the Ajax submits from the client side. And to differentiate them by I uses different submit button names such as this one
<input type="submit" value="Send" name="send_message">
Suggested from this question.
The only problem is that from the view side it doesn't seems to carry the name to the server side so I cannot use the following if-statement
if 'send_message' in request.POST:
It works if I send it normally with page fresh. But I want to use it with Ajax.
I came up with a hack that you can add this name with jQuery. Simply by after serializing() your data you then concatenate the name attribute by data += "&send_message"
Then the if statement will work. But it doesn't seems so clean. So I wonder if there's a better way to handle this? Or should I make different views to handle the different Ajax calls I have?
You really should post each form to a different URL.
If not, you could add a hidden input with the name of the form as the value.
<input name="form_name" type="hidden" value="form_1" />
views.py:
form_name = request.POST['form_name']

How to make a field required without data annotation

I am using the MvcContrib Grid to display a table on the page. I am using a custom column to produce a checkbox on the table so that the user can select multiple rows and submit the form.
I don't want the form to submit unless at least one checkbox has been selected. I could easily write this Javascript myself to enforce the validation, but I wanted to know how I could fit it in with the unobtrusive library supplied with MVC3.
I imagine I just need to set my inputs with the proper classes and attributes and then the scripts (validate and validate.unobtrusive) on the page should pick them up and mark them as needing validation, but I haven't been able to get the right combination thus far.
Here is the input that I am currently generating:
<input type="checkbox"
name="foo"
value="#item.foo"
class="input-validation-error"
data-val-required="Please select an option."
data-val="true" />
Try setting the data-val attributes on the item, then you have to tell jQuery you have new content to re-parse the form via something like:
$.validator.unobtrusive.parse($('#yourForm'));
where form is of course a reference to your form element.
There is also this great posting and jQuery has a few internal adapters you can call:
from http://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-2
jQuery.validator.unobtrusive.adapters.addSingleVal("notequalto", "otherproperty", "mynotequaltofunction")
From my experience, a commonly overlooked mistake with displaying client-side validation is putting a Html.ValidationMessageFor(lambda) on the page.
Without that, no client-side validation will fire to prevent the form submit and/or display the message that is generated using annotations on the client-side.
Hope this helps.
<div class="editor-field">
<input class="text-box single-line"
data-val="true" data-val-number="The field Qty Available must be a number."
data-val-range="The field Qty Available must be between 0 and 120."
data-val-range-max="120" data-val-range-min="0"
data-val-required="The Qty Available field is required."
id="QtyOnHand" name="QtyOnHand" type="text" value="12" />
<span class="field-validation-valid" data-valmsg-for="QtyOnHand"
data-valmsg-replace="true"></span>
</div>
The tie-in between the data model annotations and the data-val-* attributes should be clear after reading the above code, but it's where the client side validation ties in might not be so obvious. Open the \Scripts\jquery.validate.unobtrusive.js file and search for "data-val". Right away you'll see that the JavaScript uses the data-val-, input- and field-* CSS classes to display/hide validation messages on the client.
The above is taken from this great article, which you might want to read in full.

Resources