ASP.NET MVC with jQuery Validation - messages customization and localization - asp.net-mvc

I have ASP.NET MVC (4) project which localization is supported by the framework. Should I change browser settings to another language, framework automatically picks up the right resource file.
However, because I am using knockoutjs I fall back to jQuery validation at those views. Unfortunately there's no automatic support for localization there.
My question is - what are the best practices and ways to customize and localize jQuery validation messages so they will be picked automatically together with all MVC resources?
Something that jQuery validation messages will behave in a similar manner to Data Annotations messages given resources and message Ids.
In particular -
How to I make jQuery pick up the message I want from the resources instead of its default "This field is required", so it will print something like "Please enter email" and
How can I make jQuery print the same customized message in another language automatically should I change browser language ?
Thank you in advance.

1) How to I make jQuery pick up the message I want from the resources instead of its default "This field is required", so it will print something like "Please enter email".
The following is called to over-ride messages at any time. The strings below can be replaced with variables.
jQuery.extend(jQuery.validator.messages, {
required: "This field is required.",
remote: "Please fix this field.",
email: "Please enter a valid email address.",
url: "Please enter a valid URL.",
date: "Please enter a valid date.",
dateISO: "Please enter a valid date (ISO).",
number: "Please enter a valid number.",
digits: "Please enter only digits.",
creditcard: "Please enter a valid credit card number.",
equalTo: "Please enter the same value again.",
accept: "Please enter a value with a valid extension.",
maxlength: jQuery.validator.format("Please enter no more than {0} characters."),
minlength: jQuery.validator.format("Please enter at least {0} characters."),
rangelength: jQuery.validator.format("Please enter a value between {0} and {1} characters long."),
range: jQuery.validator.format("Please enter a value between {0} and {1}."),
max: jQuery.validator.format("Please enter a value less than or equal to {0}."),
min: jQuery.validator.format("Please enter a value greater than or equal to {0}.")
});
Otherwise, you can get more specific when declaring your rules within .validate().
$(document).ready(function () {
$('#myform').validate({ // initialize the plugin
rules: {
field1: {
required: true,
minlength: 5
}
},
messages: {
field1: {
required: "custom message for field 1 required",
minlength: "custom message: {0} chars required"
}
}
});
});
DEMO: http://jsfiddle.net/XV3ZR/
To dynamically change any messages after the plugin is first initialized, requires an over-ride by using the rules('add') method.
$('#field1').rules('add', {
messages: {
required: "field 1 required",
minlength: "{0} chars required"
}
});
DEMO 2: http://jsfiddle.net/PJGgE/1/
2) How can I make jQuery print the same customized message in another language automatically should I change browser language ?
I'm not sure what you mean by "change browser language", but again, the methods in #1 above are the only ways, AFAIK. These are just strings and you'll have to translate them manually or via outside methods.

I think that you can give on the server to the client script file with localization depending client's browser settings (something like i18n)

Related

How to read PlainTextInput from message view in Slack?

My app shows a simple PlainTextInput with a button in a message (Not modal).
And I would like to receive entered data by user after receiving button pressed action.
Based on slack docs, Plain-text input elements are supported in the following app surfaces: Home tabs, Messages,Modals but I don't know how to read this value from input text when using it in message (Direct message of my app and user)!
In dialog, I can easily read this value after view submission from ViewOutput data, but for messages, when reading received body data (body.message.blocks) , I have this
{
type: 'input',
block_id: 'chapterTitleInputBlock',
label: { type: 'plain_text', text: 'Chapter title', emoji: true },
optional: false,
dispatch_action: false,
element: {
type: 'plain_text_input',
action_id: 'chapter_title_input-action',
dispatch_action_config: [Object]
}
}
Which doesn't have a text field for entered value by user, how to get this value?
I should use PlainTextInputAction but I want to receive it after user pressed a button. (with ButtonAction)

Set default country for phone number field in Stripe Connect onboarding?

This is pretty minor, but for better UX, during stripe connect onboarding, I would like the phone number field to default to the connected user's country (just like the address field does). How can I make this happen? Note that I do not have the user's phone number, so I can't provide it as a parameter to Account.create()
Reproducible example
Create an new account and onboarding link
country = "US"
Stripe.api_key = Rails.application.credentials[Rails.env.to_sym][:stripe][:secret_stripe_test_key]
account = Stripe::Account.create({
country: country,
type: 'express',
capabilities: {
card_payments: {
'requested': true,
},
transfers: {
'requested': true,
},
},
settings: {
payouts: {
schedule: {
interval: "manual"
}
}
}
})
account_links = Stripe::AccountLink.create({
account: account.id,
refresh_url: 'https://example.com/reauth',
return_url: 'https://example.com/return',
type: 'account_onboarding',
})
When visiting the onboarding link (i.e. via account_links.url), despite the account's country being "US", the phone number defaults to Australia:
Question
How can I make the phone number field default to the country of the account?
Note
The address field does default to USA, as desired:
Unfortunately, you can not make stripe set country code for phone field based on country parameter you have sent. Stripe does not consider the country parameter for setting phone. If phone field is null, then it fills it automatically.

Slack static_select avoid backend call when change

I have a slack modal with a static_select
{
block_id: "my_block_id",
type: "section",
text: {
type: "mrkdwn",
text: "Please select one"
},
accessory: {
type: "static_select",
action_id: "select_type",
placeholder: {
type: "plain_text",
text: "Select one"
},
options: self.types_options
}
},
When the user selects one of the option a backend call is been triggered, can I avoid that and have the call triggered on submit form button click?
Put your static_select inside an input rather than a section.
When someone uses an interactive component in your app's modal views,
the app will receive a block_actions payload.
This does not apply to components included in an input block (see
below for details about those).
https://api.slack.com/surfaces/modals/using#components
Any interactive components used within input blocks will not send this block_actions payload. They are included in view_submission payloads only.
https://api.slack.com/reference/interaction-payloads/block-actions
Yes, but not within a normal message.
Instead you want to use a Modal. Models are modal dialogs, which have a submit button and can have multiple input elements.

Create or Update SharePoint ListItem with hyperlink field

I am unable to create or update a SharePoint Online listitem that contains a hyperlink field using Microsoft Graph. I can create or update items to the same list if I remove the hyperlink field from the body of the API call.
For update, I am sending a PATCH request to:
/v1.0/sites/<site>/list/<listid>/items/<item>/fields
with the following headers:
"Authorization" : "Bearer <token>"
"accept" :"application/json;odata.metadata=full;odata.streaming=true"
Here is the body:
{
"myUrl": {
"Description": "http://www.google.com",
"Url": "http://www.google.com"
}
}
I will get the following error message in response:
{
"error": {
"code": "invalidRequest",
"message": "The request is malformed or incorrect.",
"innerError": {
"request-id": "cfe30f57-31ba-4341-89e9-2c1bd4e15539",
"date": "2017-11-10T21:55:30"
}
}
}
It is worth noting that I can update other fields in the list item using the exact headers and target URL, as long as there is not a hyperlink field in the json body.
The format I am sending is the exact format I receive the URL field in when I do a GET. Does anyone know the appropriate format for sending URL fields back to SharePoint?
Update 1/8/2018
I posted this same issue to Microsoft's Graph Github on 11/11/2017 and they have yet to provide an answer. I have heard unofficially that REST operations on listitems that have a nested structure (e.g. hyperlink, user, taxonomy) are not supported at this time. Here is a link to the issue just in case they answer it later: https://github.com/microsoftgraph/microsoft-graph-docs/issues/1965
I do have a workaround for hyperlink fields that I am using. I changed the column on the SharePoint List that was previously a hyperlink to a "Single line of text" field. When a hyperlink is entered in a Single line text field, it is showing as a underlined and clickable link when you view the event in a browser. You can't use alternate text if you have a long and ugly URL like with the hyperlink field type, but it works!
Looks like the body has the values in reverse. You have your URL in the Description Field and the Description in the URL field. Try:
{
"myUrl": {
"Description": "google",
"Url": "http://www.google.com"
}
}

How to get failed validation rule?

I'm using knockout validation plugin. I have a few validation rules for my observable.
Is it possible to define which rule failed during the validation?
I tried to use method rules(), but it just returns list of the attached rules, but not a state of the some rule (failed or not):
http://i.imgur.com/5YvHPiw.png?1?8272
I've extended observable in this way:
self.SomeField = ko.observable().extend({
required: {
message: "The field is required.",
params: true
},
maxLength: {
message: "Please enter no more than 300 characters.",
params: 300
}
});
My not validated observable looks like:
http://i.imgur.com/7ybfLfZ.png
Thank you.

Resources