client side validation in mvc3 for dependent fields - javascript

Hi
I have an MVC3 appliocation and using client side validation and find it to be very usefull.
I am having 2 issues while using it.
-One is there any possiblity of Required filed dependency as it there for Compare
eg: If the value of a particular filed say status is="Test" then the value of other say status done field must be not blank otherwise it can be blank.
- I am having a dropdown say state .If its value is "Other" then need to make a textbox visible say "other state" .For know I am using javasript to make it visible.
I donot want to use javasript for that. Can this be performed without using javascript.

You need to write your own custom compare attribute or simply use javascript. Nothing is built in that will do this for you. The other option is to provide server side validation in your controller method where you check this situation and if it fails use ModelState.AddError to give a custom validation error.

Related

custom form control custom client side validation in Kentico

We've created a custom FormEngineUserControl to capture date input using 3 text inputs for day/month/year.
On the server we override the bool IsValid() method which works fine and displays the error message if invalid.
However we want to use client side validation also, we can use a CustomValidator control and assign a ClientValidationFunction to call a JS method however this is not then combined with the server validation function and we end up with two validation messages, one that removes when client validation passes, and one that only disappears when the form is re-submit, basically rendering the client validation useless.
Is there no way to register a client validation method with a custom form control? that will then be combined with the server method and error label etc. ?
This should work pretty much like any other .net user control. How are you preventing further processing if the validator returns false? You might need to set StopProcessing property to true.
There a few similar topics which might address your porblem:
stop execution in Custom validator if it false
Custom validator fires but does not prevent postback
I don't think this is feasible as I'm not sure how you could combine client vs server side validation. You cannot ever disable server side validation. The client side validation is just there for better UX, its not secured by any means. The general idea this is used is that you disable form submission until all fields are verified on the client side. If the validation is the same on the server, you should not see the same error messages because otherwise it wouldn't go pass the client in the first place.
The way you could do this in Kentico is to use some custom js (or better yet some framework/library to help you with validation such as validate.js), give Kentico form some id/class and connect the form validation with your js and have each side (client/server) do its job.

Microsoft Dynamics CRM run script (or something similar) in background

I wrote a script that adds the values of field A and field B. It then writes the result in field C.
My problem now is that, as I'm using a script, it only runs if I the form is open. However, I need to run it whenever field A or field B changes, regardless whether the form is open or not. For example, if a workflow changes the value of field A in the background, I need the script to calculate the new value of field C in the background as well.
I know that scripts only run on forms. That's why I'm looking for an alternative for scripts. I am aware that I could normally solve this by using a workflow, but I can't access field A through the workflow (it's a calculated field).
Are there any other possibilities?
I find that for custom calculations the following approach is effective and doesn't require much effort to implement and mantain:
Create an ACTION (let's call it new_action) without any steps
Create a PLUGIN which does the math you want, register it to the new_action message
Identify all the Simple fields involved
Create a workflow for each entity where you found the fields, set it to run on Update of the Simple fields you identified in that entity
The workflows should all be the same and the only step would be EXECUTE ACTION -> new_action
The end result is this behavior:
Simple field involved in your math changes -> Workflow starts -> Action starts -> Plugin does the math
NOTE: I usually make the action Unbound and "hand-craft" the data received by both the action and the plugin, but I glossed over this aspect because the approach itself stays the same.
Use CRM plugin. It works on server side so it will run whenever fields values will change (user interface, workflow, system process, CRM API call, etc.).
Detailed information: https://msdn.microsoft.com/en-us/library/gg328263.aspx
1.Create an update message plugin with filtering attribute as A and B,
This will fire only when your form is updated and also only when attribute A and B has changed on that form.
2.write your logic in your plugin
3. choose async and sync depending on how frequent the changes are on A and B, ideally, I will use sync so that my value of C gets updated and be in sync all the time w.r.t A and B.

communicating between javascripts and struts2 action

I have the following scenarios:
User will input some value , and this values will be validated against the value from DB in the action class.
If if the value is invalid, want to show a confirm box to the user whether he still want to proceed with the invalid value.
Depend on user's answer YES or NO, which is brought back to the action class, the process of the action class will be different.
For the above scenarios, step 1 is OK.
But how do I achieve to bring the value from the action class to the javascripts and getting user's confirmation and bring back the value to the action class?
The second approach can be done by using Ajax. Struts 2 provides many ways to use Ajax:
Use the "stream" result type to send data back to your JS code.
Use the JSON plugin to get JSON back.
Consider the Struts 2 jQuery plugin.
A Google search will give you a lot of examples as how to use Ajax with Struts 2.

Javascript form hidden visible actions

Im trying to make a "coupon" form. So that when my users go to the text field and type in 'hello' persay it then in turn will open the form that corresponds to the code. I am going to make various words/codes to use for the coupon. I'd like to make it so that there is no way they can bypass this, and that they can not see this javascript if they click the view source file. Can someone help me. It seems simple I believe, though I do not know anything about javascript. so //comments would be perferred so that I understand each command and statement happening for further use.Thanks ahead of time.
You can't hide javascript because the javascript has to be accessible for the browser to execute it. And if it is accessible to the browser then it is possible for anyone to access it manually through the URL.
If you don't want users to see the code that handles coupon values, you need to do it in the server-side, not in javascript.
You can use javascript to make an ajax call that sends to the server the coupon value entered. The server side can use this value to choose and return the HTML that corresponds to the coupon value. But the code that checks the coupon value has to be in the server-side if you want it to be hidden.

Sending parameters with the autogenerated post submit/create button in MVC 3 razor

I read online about how to send a variable from javascript with ajax but I am not sure that solution fit my needs.
What I am trying to do is this:
I have an automated form for creating a row at my DB.
What I Want to do is run a javascript code that will create a variable with logic based on the user input and than send this variable along with all the other fields that are automatically being sent because of the automated creation and binding of the model.
Basically what I want is to add another array to the automated post call that I didn't wrote because it is being generated automatically by mvc. and than I can retrieve this variable (array at my case) at the create method on the controller
Do you have a solution for this?
Thank you very much
You could add some hidden fields to the form and use Javascript to fill their values in the submit event.

Categories

Resources