I want to insert a message which says Type here for example in a textbox in MVC3.
How do I do it?
Can it be done in the Model class on the field declaration?
Please suggest any way of doing?
You could use the HTML5 placeholder. Works in chrome and firefox. U can use modernizr to make it work in IE and other older browsers.
How to set up modernizr for placeholder
#Html.TextBoxFor(model => model.Name,new { placeholder = "Fill in your name" })
#Html.TextBox("Place textbox name here", "Place default value here")
#Html.TextBoxFor(Model field declaration here, new { Value = "Place default value here" }
If you want to use input mask then you may take look here jquery input mask
Related
I have written a Joomla component which is working well. I now want to add a button to an admin list view that when clicked automatically starts a CSV download of only the items selected in the list.
I'm OK with the model logic, the problem I've got is passing the selected cids or presenting raw output without the template.
If I use JToolBar's appendButton function to add a 'Link' type button, then I can send the user to a URL with 'format=raw', but I can't send information about which items were checked.
If I use JToolBarHelper::custom to add a custom list button, then I can send the information about which buttons were checked, but I can't send format=raw
As far as I can see there are two solutions, but I don't know how to implement either of them. Option one would be to force templateless raw output without a URL parameter of format=raw. Option two would be to set a hidden variable with format=raw in the admin form.
Any help would be appreciated
I've solved this as follows:
I added this hidden field to the admin form
<input type="hidden" name="format" value="html" />
Then subclassed JToolbarButtonStandard overriding the _getCommand with
protected function _getCommand($name,$task,$list)
{
JHtml::_('behavior.framework');
$message = JText::_('JLIB_HTML_PLEASE_MAKE_A_SELECTION_FROM_THE_LIST');
$message = addslashes($message);
if ($list)
{
$cmd = "if (document.adminForm.boxchecked.value==0){alert('$message');}else{document.getElementById('adminForm').format.value='raw'; Joomla.submitbutton('$task')}";
}
else
{
$cmd = "document.getElementById('adminForm').format.value='raw'; Joomla.submitbutton('$task')";
}
return $cmd;
}
So that when the button was clicked it changed the format parameter from html to raw.
I'm not going to mark this as solved in case anyone has any better ideas
I tried to create a custom data annotation validation attribute (NameValidationAttribute) in MVC 5 project using VS2013. I was able to successfully add the client side validation and the error message for custom validation is getting displayed as soon as the focus leaves the textbox. However, the standard attributes like [Required] and [Range] validators are now not showing proper error messages, says 'Warning: No message defined for 'field' ' (See below screenshot).
Question:
- Why the standard validation error messages are showing as "Warning: No message defined for UnitsInStock"? What am I missing?
Below is my custom client validation script:
I included following scripts in EditProducts page.
Please note that the error messages for UnitPrice, UnitsInStock and ReorderLevel fields are defined with Range validation attribute (see below).
FYI, I tried to change the order of the scripts in ProductEdit page but still its showing the same message.
Please advise!
I ran into this issue. I had created an MVC attribute called PasswordAttribute, with a client side validator called 'password'.
$.validator.addMethod('password', function (value, element, params) {
...[validation logic]
});
$.validator.unobtrusive.adapters.addBool('password');
As soon as I added this logic to the solution I got the error you saw on my Password and ConfirmPassword fields. Note that this occurred before the attribute was even used. Simply renaming the client side validator to 'passwordcheck' fixed the issue. I initially thought that the name 'password' possibly clashed with one of the pre-defined validators (cf. jQuery Validation Documentation) but this doesn't appear to be the case. I suspect now that it is a clash with the name or value for some input field attribute. Anyway, the solution was simply to rename the validator.
jQuery unobtrusive need data-msg for default validate message.
This is how to apply dynamic error message from your model to Html
$.validator.unobtrusive.adapters.add("requireif", function (options) {
$('#' + options.element.id).attr("data-msg", options.message);
// Add rule ...
});
You can change default warning message.
$.validator.addMethod("requireif", function (value, element, pair) {
// validate logic
return true/false;
}, "YOUR DEFAULT MESSAGE HERE");
Or
#Html.TextBoxFor(m => m.Property1, new { Class = "form-control", data_msg = "YOUR DEFAULT MESSAGE HERE" })
Or if you can put it directly to your Html like this.
<input class="form-control" data-msg="YOUR DEFAULT MESSAGE HERE"/>
I have the following doubt. I am considering the option to have html tags within my resx texts for localization.
When I put the resources directly I can resort to:
#Html.Raw(#Resources.ResourcesFM.Error_Email)
and it works as expected.
The problem is when the resource is being called by a validation message from an htmlhelper:
#Html.ValidationMessageFor(model => model.Email)
Got from an attibute:
[DataType(DataType.EmailAddress,
ErrorMessageResourceType = typeof(ResourcesFM),
ErrorMessageResourceName = "ErrorMailIncorr")]
What I am trying...
#Html.Raw(Html.ValidationMessageFor(model => model.Email))
I do not know how to get the same result as when using #html.Raw as the output from the helper is a MvcHtmlString...
Thanks
Try this:
View:
#Html.Raw(Server.HtmlDecode(#Html.ValidationMessageFor(m => m.UserName).ToString()))
Controller Action:
ModelState.AddModelError("UserName", "This is a link <a href='http://example.com'>Google Home</a>");
Html.ValidationMessageFor html-encodes the message. But you should be able to simply call HttpUtility.HtmlDecode() on the result. Even though the result contains html tags and whatnot, the decode will simply no-op on that part of the string.
So if `Html.ValidationMessageFor(...)' returns
<span><div>This is in a div</div<></span>
Then HttpUtility.HtmlDecode(Html.ValidationMessageFor(...).ToString()) will give you
<span><div>This is in a div</div></span>
It's not pretty, but it works. Your alternative is to recreate your own Validation helper version that never encodes the message.
For Localization, you may use String.Format and choose appropriate placeholder for the link
ModelState.AddModelError("UserName", String.Format("This is a link {0}", "<a href='http://example.com'>Appropriate String From a Resource</a>"));
My views have limited space for validation messages and because of this i want to output a asterisk instead of the validation message. The following blog outlines how to do this by adding a extra parameter to the validation helper:
http://www.erroronlineone.com/2011/09/12/mvc3-display-an-asterisk-for-error-message/
#Html.ValidationMessageFor(model => model.Name, "*")
But I want to output a asterisk or an image and using the HTML title attribute have a pop up displaying the validation message. Would I have to change the way unobtrusive validation works? Or do you have a better idea? Thanks
One way, if you are game, is to edit the unobtrusive validation javascript file.
The error message is displayed in the onError function. Here you can change the message element any way you wish. For example (unminified):
function onError(error, inputElement) { // 'this' is the form element
var container = $(this).find("[data-valmsg-for='" + escapeAttributeValue(inputElement[0].name) + "']"),
replace = $.parseJSON(container.attr("data-valmsg-replace")) !== false;
container.removeClass("field-validation-valid").addClass("field-validation-error");
error.data("unobtrusiveContainer", container);
if (replace) {
container.empty();
// Set the "title" attribute and change the text to an asterisk.
error.prop("title", error.text());
error.text("*");
error.removeClass("input-validation-error").appendTo(container);
}
else {
error.hide();
}
}
When a form with a textarea using CkEditor is submitted using ajax the post is empty on the server side.
If I remove CkEditor, the value is posted. Any ideas?
On submit, run this code:
for (instance in CKEDITOR.instances) {
CKEDITOR.instances[instance].updateElement();
}
.. basically for AJAX, you need to force CKEditor to update your textareas so they contain the data you see in the CKEditor windows. See this question for more details.
You don't really need to update anything with JS. All you have to do is to make sure your textarea (the one you replace with CKEDITOR.replace() on $(document).ready()) has the same name as the property you want to set value of, e.g.:
<textarea id="editor" name="Body">#Model?.Body</textarea>
This works for me:
CKEDITOR.replace( 'content' );
function updateAllMessageForms()
{
for (instance in CKEDITOR.instances) {
CKEDITOR.instances[instance].updateElement();
}
}