I have struts 2 checkboxlist as followed:
<s:checkboxlist list="listOfOptions" name="someName" />
I would like to add validation to make sure that at least ONE of the checkboxes has been checked. Any thoughts?
Thanks
I use JS to do that.
<sx:submit
id="button_submit"
name="button_submit"
onclick="return validateNotEmptyCheckbox();" />
And JS:
function validateNotEmptyCheckbox() {
var checkboxes = document.getElementsByTagName('input');
for (i = 0; i < arguments.length; i++) {
var fieldName = arguments[i];
var atLeastOne = false;
for (j = 0; j < checkboxes.length; j++) {
if ((checkboxes[j].type == "checkbox" || checkboxes[j].type == "radio")
&& checkboxes[j].name == fieldName
&& checkboxes[j].checked == true)
atLeastOne = true;
}
if (atLeastOne == false) {
alert("Choose one!!!");
return false;
}
}
}
Also for radio buttons, as you can see.
I made an validation method on the action to whom the form is submitted.
deliveryTimesInputList is here the list of integer with the values from the checkboxlist.
#Override
public void validate() {
if(deliveryTimesInputList == null || deliveryTimesInputList.isEmpty()){
addFieldError("deliveryTimesInputList", "Du må velge minst et leveringstidspunkt for dette postnummeret");
}
}
Use the following code for struts2 xml validation .The value of checkbox List is stored as a string.Hence,you can use requiredstring.
<field name="someName">
<field-validator type="requiredstring">
<message>You need to select at least one option</message>
</field-validator>
</field>
Related
I am creating a mobile application in that I need to send a message for query or support when the user creates that type of message and then click on return or done from keyboard entry height should be adjusted and if the user clicks on the back than it should also be managed. In that, I am not able to use Editor control.
I am using a shared project to develop the application.
I try with the following code:
Sample.xaml
<StackLayout Padding="50" HeightRequest="150">
<Entry TextChanged="Entry_TextChanged" x:Name="EntryCustom"/>
</StackLayout>
Sample.xaml.cs
private void Entry_TextChanged(object sender, TextChangedEventArgs args)
{
int isElse = 0;
if (args != null && args.OldTextValue != null && args.NewTextValue != null)
{
string[] oldval = args.OldTextValue.Split('\r');
string[] Newval = args.NewTextValue.Split('\r');
if (oldval.Count() > Newval.Count())
{
EntryCustom.HeightRequest = EntryCustom.HeightRequest - 33;
isElse = 2;
}
else
isElse = 1;
}
if (args.OldTextValue != null && (args.NewTextValue == "\n" || args.NewTextValue.EndsWith("\r")) && EntryCustom.HeightRequest < 100 && (isElse == 1 || args != null || args.OldTextValue.Count() > args.NewTextValue.Count()))
EntryCustom.HeightRequest = EntryCustom.HeightRequest + 33;
}
Can anyone look into this and suggest me what should I have to change in the code?
You should use an Editor. Use <Editor AutoSize="TextChanges"/> and the editor will adjust it's size based on the entered text.
I have an entity which contains 2 forms, I want to prevent navagation between these 2 forms based on the value of two option field. In other words if the value of need prescoring is yes navigation is not possible and the inverse, how can I do this ?
Is it possible to simply hide the list ?
Thanks,
No, you cannot dynamically change the forms the user can select. This can only be done statically based on security roles.
Instead I suggest using a single form, where you hide and show the relevant fields/sections/tabs based on the value of your Need Processing field.
You can decide based on your project complexity wrt number of form controls/tabs/sections. We did something like this to maintain & forced navigation based on form control value.
var taskFormOptionSet = {
Form1: 1,
Form2: 2,
};
var FormNames = {
Form1: "Form1",
Form2: "Form2",
};
var myform = Xrm.Page.getAttribute("need_Prescoring").getValue();
var currentform = Xrm.Page.ui.formSelector.getCurrentItem();
if (currentform != null) {
var formId = currentform.getId();
var formLabel = currentform.getLabel();
}
if (myform == taskFormOptionSet.Form1 && formLabel != FormNames.Form1) {
var items = Xrm.Page.ui.formSelector.items.get();
for (var i in items) {
var form = items[i];
var formId = form.getId();
var formLabel = form.getLabel();
if (formLabel == FormNames.Form1) {
form.navigate();
return;
}
}
}
As it's not supported I used another solution which is to check if the boolean is true and the name of the, if the user tries to change the form he will be redirected to the right form until he changes the value of the boolean.
DiligenceSwitch: function(){
if (Xrm.Page.ui.formSelector.getCurrentItem() != null) {
var currentform = Xrm.Page.ui.formSelector.getCurrentItem();
}
if (currentform != null) {
var formId = currentform.getId();
var formLabel = currentform.getLabel();
}
var kycId = Xrm.Page.data.entity.getId();
SDK.REST.retrieveRecord(kycId, "kyc_Kycdiligence", "kyc_Needprescoring", null, //field for searching the targeted field, entity, targeted field, ...
function (kyc) {
if (kyc != null || kyc.kyc_Needprescoring != null) {
if (formLabel != "Pre-Scoring" && kyc.kyc_Needprescoring == true) {
var windowOptions = { openInNewWindow: false };
var parameters = {};
parameters["formid"] = "4B0C88A9-720C-4BFA-8F59-7C1D5DD84F02";
Xrm.Utility.openEntityForm("kyc_kycdiligence", kycId, parameters, windowOptions);
alert("Vous devez faire le pre-scoring");
}
}
},
function (error) {
Xrm.Utility.alertDialog(error.message);
});
},
I have defined Ajaxvalidatoncalloutextender control with unique behaviour ID in my page for all the page controls.
<ajaxToolkit:ValidatorCalloutExtender ID="vceNotProgress" runat="server" TargetControlID="revInvalidNotClosedProgressValue" BehaviorID="b_vceNotProgress">
</ajaxToolkit:ValidatorCalloutExtender>
<ajaxToolkit:ValidatorCalloutExtender ID="vceProgress" runat="server" TargetControlID="revInvalidClosedProgressValue" BehaviorID="b_vceProgress">
</ajaxToolkit:ValidatorCalloutExtender>
I'm validating on client click of submit button like this
function ValidatePage()
{
if (typeof (Page_Validators) == "undefined") return;
var noOfValidators = Page_Validators.length;
var Page_Callout = new Array(2);
Page_Callout[0] = "b_vceNotProgress"; -- behaviour ID
Page_Callout[1] = "b_vceProgress"; -- behaviour ID
for (var validatorIndex = 0; validatorIndex < noOfValidators; validatorIndex++) {
var validator = Page_Validators[validatorIndex];
ValidatorValidate(validator);
if (!validator.isvalid) {
showValidatorCallout($find(Page_Callout[validatorIndex]));
return false;
}
}
return true;
}
My Problem: I donot want to hardcode the array like Page_Callout[0],Page_Callout[1]. I need to populate the array based
on the visible controls (i.e assign behaviour ID) in the page.
if (typeof (Page_Validators) == "undefined") return;
var noOfValidators = Page_Validators.length;
var count = 0;
var Page_Callout = [];
for (var i = 0; i < Page_Validators.length; i++) {
Page_Callout[Page_Callout.length++] = Page_Validators[i]._behaviors != null ? Page_Validators[i]._behaviors[0]._id : "";
count++;
}
for (var validatorIndex = 0; validatorIndex < noOfValidators; validatorIndex++) {
var validator = Page_Validators[validatorIndex];
ValidatorValidate(validator);
if (!validator.isvalid) {
showValidatorCallout($find(Page_Callout[validatorIndex]));
return false;
}
}
return true;
}
function hideValidatorCallout() {
AjaxControlToolkit.ValidatorCalloutBehavior._currentCallout.hide();
}
function showValidatorCallout(currrentCallout) {
AjaxControlToolkit.ValidatorCalloutBehavior._currentCallout = currrentCallout;
AjaxControlToolkit.ValidatorCalloutBehavior._currentCallout.show(true);
}
*First Post
I have a JQuery error handler for my Ajax posts that I must use, it appends an error to the html based on the field name for that element like this
$(document).ready(function () {
function myHandler(e, error) {
var tag = "";
if (error.Success == true) { $('.field-validation-error').remove(); return; } // if success remove old validation and don't continue
if (error.Success == false) { $('.field-validation-error').remove(); } // if success remove old validation and continue
for (i = 0; i < error.Errors.length; i++) {
var t = error.Errors[i];
//get error key and assign it to id
tag = t.Key;
//clear down any existing json-validation
for (j = 0; j < t.Value.length; j++) {
//this part assumes that our error key is the same as our inputs name
$('<span class="field-validation-error">' + t.Value[j].ErrorMessage + '</span>').insertAfter('input[name="' + tag + '"], textarea[name="' + tag + '"], select[name="' + tag + '"], span[name="' + tag + '"]');
}
}
}
$.subscribe("/******/errors", myHandler);
});
This works perfectly out of the box with our fluent validation setup until I try to add a custom modelstate error at controller level like so:
foreach (var item in model.Locations)
{
var cityRepos = new CityRepository(NhSession);
var cityItem = cityRepos.GetAll().FirstOrDefault(o => o.Country.Id == item.CountryID && o.Name == item.City);
if (cityItem == null)
item.City
ModelState.AddModelError("City", string.Format(#"The city ""{0}"" was not found, please ensure you have spelt it correctly. TODO: add a mail to link here with city not found subject", item.City));
}
the problem is that the modelstate error needs to be attached to the html field name not my magic string "City". The html name property is MVC Generated and looks something like this:
name="Locations[0].City"
I have encountered this problem in a html helper before and used the method:
.GetFullHtmlFieldName(
ExpressionHelper.GetExpressionText(propertySelector)
);
which resolved my problem in that case.
My question is can I use this method on my model property in an MVC post action to obtain the html name property it has come from?
Thanks in advance
ok so it's not ideal but I have implemented this Helper method until I can find a better solution that doesn't involve magic strings:
public static class ModelStateErrorHelper
{
public static string CreateNameValidationAttribute(string collectionName, int index, string propertyName)
{
string template = "{0}[{1}].{2}";
return string.Format(template, collectionName, index.ToString(), propertyName);
}
}
Is it possible to find if a treeListData is having a sibling or not. In adobe Flex 4 and actionscript 3
Yes, you could check the item property of the TreeListData instance inside an ItemRenderer and either:
use item as ITreeDataDescriptor and check the hasChildren property
use item as your custom class and possibly check the length of your children collection (depending on your data-model).
Example code:
protected function dataChangeHandler(event:FlexEvent):void
{
var node:TreeNode = treeListData as TreeNode;
if(node != null)
{
if(node.children != null && node.children.length > 0)
{
hasChildren = true;
return;
}
}
hasChildren = false;
}
Hope this answers your question.