Avoid validation on hidden fields - Webmatrix - razor

I got a form with some hidden fields that are only displayed if an option is selected.
I got this line to validate
Validation.RequireFields("atividade", "contaCliente", "contaServico", "nomeCliente", "dataAgendamento");
The first four fields are always visible, however the field dataAgendamento only appears if an option is selected like I said above.
That said when I try to to submit the form, I get an error that dataAgendamento is required. Is there some way to avoid validation on hidden fields?
Thanks in advance

Since you're having the Option visible only when the Condition is true. You can check or execute the validation only when the condition is true.
Do this,
Validation.RequireFields("atividade", "contaCliente",
"contaServico", "nomeCliente");
// Remove the last validator, and add it using a condition
if(valueFromSelect == "somecondition") {
Validation.RequireField("dataAgendamento", "Required visible field!");
}
This would make the field required only if the condition (value in the select clause is true) otherwise it won't add the Validation to the field.

Related

How does MVC decide which value to bind when there are multiple inputs with the same name?

I have an edit page where several fields are conditionally disabled, based on the user's role. When the fields are disabled, their values are not posted to the server (as expected), which causes the ModelState to be invalid, as the values are required.
To get around this, I want to add Html.HiddenFor() for the fields; so that a value will still get posted (and so that it will retain those values if the View is returned). However, in the case that those fields are not disabled, I will then have both a TextBoxFor and a HiddenFor going to the same model property.
I have run a couple tests, and it appears that when this happens, the value of the first element on the form will be binded to the model, while the next one just gets ignored. If this is the case, then I should be able to just put the HiddenFor after the TextBoxFor, in which case the value of the hidden input will only be posted when the regular input is disabled.
#Html.TextBoxFor(m => m.FirstName)
#Html.HiddenFor(m => m.FirstName) #*Only gets binded to the model if the above text box is disabled*#
(There is some JavaScript that conditionally disabled the visible TextBox).
So two questions: 1) Is it documented that MVC binding will always work this way; can I safely have both of these fields?
And, 2) Is there a better approach to accomplishing this? I know that I can but the HiddenFor inside an #If statement so that it will only get created if the TextBox is disabled; but that is a lot of extra logic in the View that I'd like to avoid.
The DefaultModelBinder reads the values from the request in order and binds the first matching name/value pair and ignores subsequent matches (unless the property is IEnumerable). This is how the CheckBoxFor() method ensures a true or false value is always submitted to the controller (the method generates a checkbox with value="True" and a hidden input with value="False"), so you can safely add the hidden input after the textbox.
One option you might consider rather than a disabled textbox, is to make it readonly, which means it will always submit a value, therefore you only need one input (and you can always style it to look disabled if that is what you want).

SSRS Hiding Text in Footer

On my SSRS report I want to hide a page footer if none of the rows returned meet the condition where I need to show the footnote. I have a list of names and on the report I add an asterisk to indicate a certain condition for that person. For example:
Smith, Sally*
I have a footnote that says something along the lines of:
*Employee needs updated form on file
I am using the Visibility option of the Text Box Property to add an expression to hide the text in the textbox of the page footer if none of the employees have an asterisk after their name. (No sense in showing the footnote if it does not apply to anyone on the report.)
I've seen some examples here and thought this would work (setting false to not hide the text if an asterisk is appended to the name in the dataset):
=IIf(Fields!Name.Value.Contains("*"),False,True)
I don't get a syntax error but the footnote does not show no matter what I do with this expression or similar ones I have tried. I saw one place that said you cannot use a expression to hide the text in a textbox in a page footer but could not confirm this is true.
Is it my syntax or that fact that one cannot hide text in a textbox on a page footer? The option is not grayed out so it seems this should be possible.
This formula doesn't lookup a results set.
It only checks the first row: if the first row contains asterisk you will see footnote.
You can try to solve this for footer such way:
Create report variable "Has asterisk"
Create function at report code:
Public Function CheckAsterisk(ByVal CheckString As String) As String
If CheckString.Contains("*") Then
Report.Variables!HasAsterisk.Value = True
End If
Return CheckString
End Function
Call function from textbox where you display person name:
=Code.CheckAsterisk(Fields!Name.Value)
Check report variable at footer

Can't remove the value entered in the djFilteringSelect dojo control in xPages

I am using the djFilteringSelect control to show values in a dropdown as user type a value.
The lookup and typehead is working fine. The user type a letter and the dropdown allow the user to select a value which is then displayed in the dropdown field.
If the user now decide to remove the value first selected so that the combobox is empty and leave the field, then the first value in the list is now automatically filled in.
The consequence of this is that if the user have added a value there is no way to remove the value and leave the box emtpy.
I am using required=false for both the control and the dojo attribute but it does not seem to help. There are also a few other djFilteringSelect attributes I have tried like "Autocomplete" and "trim" but it does not work
Here is the code
<xe:djFilteringSelect id="test" type="select" store="jsondata" searchAttr="data" required="false" labelType="html" invalidMessage="Not valid">
<xe:this.dojoAttributes>
<xp:dojoAttribute name="required" value="false"></xp:dojoAttribute>
</xe:this.dojoAttributes>
</xe:djFilteringSelect>
Initally the field is not required, but if the user have entered a value it is required.
My question is if there a way to prevent the djFilteringSelect control to always populate the field if I have previously added a value
I found someone who solved this in another stack overflow topic, by creating an empty entry in my data store. but I could not get this to work
Dojo: Select of empty value for FilteringSelect while required=false
I do this quite a lot. Right now I don't have a working sample to show you (since I moved to bootstrap - and have to code the selects by manually adding select2 controls) but something like this should do it...
I add an "empty" value at the top of my select - and that seems to work no matter whether I am using a combobox, djCombobox or combobox with select2 from bootstrap. My markup typically looks like:
<xp:comboBox id="inputLocationSelector" value="#{User.catchListType}" disableClientSideValidation="true">
<xp:selectItem itemLabel="(none)" itemValue=""></xp:selectItem>
<xp:selectItems>
<xp:this.value><![CDATA[${Configuration.meta.listLocationTypeOptions}]]></xp:this.value>
</xp:selectItems>
</xp:comboBox>
Then you could specify "(none)", "All" or " " for the "not-selected" value depending on your needs.
Validation is a different thing so just specifying "required=false" does not give you the "empty" value.
/John

to validate checkbox input for table using multiple rows

i using js to add multiple row in a particular table, but when submit the form all check box having the same value, so how can i validate this checkbox using js before submit so change value to if unchecked, i trying on that but got no solution, does any one this before,
thanks in advance
What are your checkboxes called? Do they all have the same name? You have two options. One is giving each checkbox an unique name, the other is giving your checkbox a name like checkboxes[]. The [] lets all the values be entered into an array so they don't overwrite eachother.
If you mean something else, you have to state more clear what you want, because it's a bit incomprehensible right now.
finally i found solution for my problem, thanks 'vindia' for the clue, i add checkbox with array, sol as below
in html
`<input id="abc[]" name="abc[]" type="checkbox" value="1">`
in js
for(var i=0;i<chkDefaultLength;i++){
if(!document.neworupdateevent["chkDefault[]"][i].checked){
document.neworupdateevent["chkDefault[]"][i].value=0;
}
}

I want to get the value of disabled text box in our next jsp but i am getting null value

I want to get the value of disabled text box in our next jsp but I am getting a NULL value.
Any idea what might be going wrong?.
Input fields marked with disabled="disabled" never send their value to the server when the form is posted. You could use the readonly="readonly" attribute in order to still make the field not editable by the user but send the initial value to the server when the form is submitted.
You can't get disable property value into server side. You need to run javascript to fetch disabled value into servlet.