Knockout js + Laravel localization - laravel

Knockout JS includes some build-in validation messages such as
"This field is required"
"Please enter at least {0} characters."
and so on.
How can I translate these messages ? If I just replace them with "#lang(..) or "trans(..)" it does not works..

Laravel does not have build in functions for translations.
If you want to use messages in other languages you will have to add them to the language files.

Here is how I overrided and translated the knockout validation messages:
var = ko.observable().extend({required: {
params: true,
message: "#lang('errors.required_field')"
}})

Related

MVC Custom Attribute - Client-side Validation - Warning: No message defined for 'field'

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"/>

knock out observable for telerik radcaptcha

I am trying out with knockout validation.
I understand that i am able to use observable for inputs, i.e
<asp:TextBox ID="TxtName" runat="server" data-bind="value:Name"></asp:TextBox>
var self = this;
self.Name = ko.observable().extend({ required: { message: "Please enter your name." } });
but what if i am going to do a validation for a telerik radcaptcha
<telerik:RadCaptcha ID="RadCaptcha" runat="server"></telerik:RadCaptcha>
Am i able to do an observable to check if the radcaptcha is valid and display a message if its not?
For security reasons it is not possible to perform a client-side check on the Telerik RadCaptcha control. Think about it, if client side validation was enabled it would be possible to create a program to bombard the input with responses until the correct one was found.
See Telerik Forum Posts:
http://www.telerik.com/community/forums/aspnet-ajax/captcha/rad-captcha-client-side-validation.aspx
http://www.telerik.com/community/forums/aspnet-ajax/captcha/client-side-support-needed-for-radcaptcha.aspx

How can we show error message on form validation on joomla 2.5 in the same way as email validation with a tooltip like alert?

How can we show error message on form validation on joomla 2.5 in the same way as email validation etc? with the tool tip?
I have included
JHtml::_('behavior.formvalidation');
JHtml::_('behavior.tooltip');
and I have a script
$(document).ready(function () {
var msg = new Array();
document.formvalidator.setHandler('phone', function (value) { return (value != '123'); } );
});
and My input filed is like
<input name="jform[contact_phone]" type="text" id="jform_contact_phone" class='validate-phone'/>
I am getting the tooltip for email field validation and required filed validation, but I can not include numeric validation and I could not implement a tooltip message for my custom phone number field.
I am using joomla 2.5
Please check here for custom validation
http://docs.joomla.org/Client-side_form_validation
You have to add like below:
window.addEvent('domready', function(){
document.formvalidator.setHandler('birth', function(value) {
regex=/^\d{4}-\d{2}-\d{2}$/;
return regex.test(value);
});
});
If its only for numeric click below URL and try this also:
Validate phone number in joomla 2.5

MVC3 Validation, Unobtrusive Validation - Asterisk for validation message

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();
}
}

Clear error message or reset validation using jQuery Validation plugin

I am a newbie to jQuery and I aplogize if this question is very basic.
I am using bassistance's Valiation plugin to validate a simple form. One of the text fields is mandatory - so I set required flag under rules and a corresponding message under messages. The problem I have is, once the validation kicks in, the error message I've set is displayed even if the field is blank (meaning, if the user deletes the contents of the field to start from scratch). Is there anyway to clear the error message or reset the validation if the field becomes empty? I tried to use the resetForm(), but looks like I can't use it while it's still validating. Here's part of the code:
$(document).ready(function() {
var validator = $("#form1").validate({
rules: {
fname: {
required: true,
remote: 'http://localhost/check_name.php'
}
},
messages: {
fname: {
required: "Enter a valid jobname. eg: 99999_Disney",
remote: "Name already exists";
}
}
});
Thanks for your help!
I solved the problem. Not sure if it's the most elegant way to do it, but it works. I had to clear the invalid state of the field somehow. I tried resetForm() but that didn't work. In the end I used something like this:
if ($("#ID").val()=="") {
validator.resetForm();
$("#ID").rules("remove", "required minlength");
}
and as needed added those rules back to the ID using:
$("#ID").rules("add", {
required: true,
minlength: 5,
messages: {//messages
},
});
You could try using a validation method, like so:
jQuery.validator.addMethod("ValReqJobName", valReqJobName, "Enter a valid jobname.");
And then, in the rules object, under "fname", just add "ValReqJobName: true".
Within the "valReqJobName" function you can allow for the field to be blank, or add whatever custom logic you want.

Resources