validation.xml change mask value on condition - validation

I am working on struts server side validation(version 1.2). I have used validwhen several times. I have 2 textboxes that submit to server. text1(value comes here from a drop down selection) can have name or id. text 2 holds its value. I have to write validation on text2. Is there a way where i can change mask var-value in validwhen ? i.e if text1 value is name then text2's mask should be a regex for name(alphanumeric and space). if text1 value is id then text2's mask should be a regex for id(only numbers). Is there a way this can be achieved? I don't think we can write 2 different validation on same field.
thanks,

IMO validation like that belongs on the Java side.
You can either wrap it up in a custom validator if it will be used across the app, or as part of the form's validation method, where you could also combine it with standard XML validation.

Related

Laravel validation; human names for array fields

In Laravel form validation you can do this: file_description.* to validate each item of an array according to a set of rules. The problem is, the system automatically returns "file_description.1 is required" as an error message if the field is required and not filled in.
More on that here: https://ericlbarnes.com/2015/04/04/laravel-array-validation/
Now, I'm not a complex man, I just want the field to say "File Description 1 is required". I am aware you can set messages but a) my input arrays are dynamically generated by jquery (click to add more type scenario) so I'd have to use a loop like in the above example b) I feel like there must be a better way.
Is there a way to either extend the core validation system to simply return a humanized name for the array field as Laravel does with regular fields, or is there an option I missed in the docs that allows for this? I'd rather not get involved with doing some regex type search to fix this.

Spring MVC code inject validator

I have two fields, one that not allow save HTML tags on DB and the other allow.
How I create an validator that remove spetial chars and tags from my string before save, and apply on String fields only?
PS.: I need something to block SQL inject too.
Actually validator cannot remove any chars. Validator just checks whether entered content is valid or not.
Read for example
For your case you can define form fields and mar them with
#Pattern(regexp = "the detect html expression here")
See regular expressions to [detect][2] (and remove) html

I would like to define a component as optionally required (based on another field's value)

I built two custom components using built-in CQ components. The components' JSP's, along with some custom javascript, have a line like below in order to leverage the built-in components (and their formatting, labels, etc.).
<cq:include path="." resourceType="foundation/components/form/dropdown" />
I have one each of my custom dropdown components on a form. I can mark field A and field B as both being required in their respective components' editor dialog. I can provide required messages so that when either field is blank and the form is submitted, I get a message that the fields are required (with my custom messages). However, what I really want to do is hide or disable field B based on the value supplied in field A. I'm handling this manually via jQuery. However, this of course presents a problem on validation. I want field B to be NOT required when it is hidden/disabled and for it to be required when it is enabled/visible. Since the showing/hiding is done client-side, the server-side validation has know knowledge of the change and still expect a value to be provided for field B.
I'm been trying to poke around in the CQ Widgets API to find if there is something I can do on the client side to set/unset the required property so that when the form gets posted it is handled correctly. I'm guessing that there isn't since the validation seems to be happening on post instead of client-side.
Any ideas/thoughts/options?
It turns out that the link from my comment gave me the idea that I can have custom server-side validation for my form, I just wasn't sure how to connect it to the field. It turns out that all I had to do was create a servervalidation.jsp file in my "B" component. CQ will see the servervalidation.jsp and automatically invoke it for my custom component.
I was then able to examine the submitted value of field "A" (by getting the parameter from the request object) and doing some other custom logic for my needs.
I was then able to optionally do this:
FieldDescription field = FieldHelper.getConstraintFieldDescription(slingRequest);
FieldHelper.checkRequired(slingRequest, field);

Scala Play form mapping and validation

I'm creating some system, that can accept different incoming JSONs with ~40 fields inside. But as you may know, Play form mapping, can accept up to 18 field maximum.
So I thought it could be a good idea to split these incoming fields in to groups. For example Group1, Group2.. Group8. So I would accept only 8 parameters inside each group and these groups will have their own form mappings (where all fields are optional).
It was ok, until I came to the point: for some form mappings (for case classes) these fields must be mandatory.
For example, FormMapping1 must have Group1.field1, if not - form should give an validation error. But FormMapping2 can ignore Group1.field1, but Group2.field2 is mandatory for it.
But how should I create Group1 form mapping then?
As Forms do convert JSONs to objects, it's hard for me to imagine, how can I even do this validation. I mean, my case classes should have different constructor for each case I'm having mandatory field?
I hope I've described the situation well.
Would be great to hear from you some strategies of dealing with such issues. As I suppose my strategy isn't the best.
Thanks in advance!
P.S. currently I'm thinking about move away from Play Form validation to other.

mvc Html.TextBoxFor

I have a question here about html.textboxfor
I knew that we can use Html.TextBoxFor to restrict the size of the user input
Html.TextBoxFor(model => model.EmployerNumber, new {size=3})
But can we configure it to take numeric value only ?
I try to get Html.ValidationMessageFor to throw error when input value is not numeric.
Many thanks.
a textbox accepts text so its not really capable of being limited just to numbers. the best way to do what you want to do is have a model set up that only accepts numeric values and then use client side validation to alert the user that only numeric values are acceptable.

Resources