Struts 2 validation resets my dynamic data - validation

I'm trying to validate a form and it works well, the right messages appear...
My only problem is that my form fields are deleted if there are some errors.
Datas are taken by Database and be showed in forms with struts tags (so they're dynamic). If I put sono static value, that will not deleted after a wrong validation.
<s:form action="updateUser" method="post" id="updateUser"
name="updateUser" >
<s:textfield value="%{user.name}" class="modify" id="name" name="name" key="modify.name" required="true" />
this will be deleted while this:
<s:textfield value="HELLO" class="modify" id="name" name="name" key="modify.name" required="true" />
will not.
Any advice?

Have you set a User object in your Action Class? Do you have getters/setters for that object? Also how have you configured the "input" result of this action in struts.xml? Maybe you should use "chain" in result of INPUT (i guess this is the result you get from the validator.)

Related

Laravel validator on a form and send post

I used laravel validator.
I have a form wih 2 fields
If i fill one field "NAME" and i leave the other "SURNAME" empty pressing SAVE button of the form appears required alert message on SURNAME but the name inserted in the NAME field became empty! And i think form send data anyway.why?I aspect that form doesn't send datas
In the value attribute of each input you need to echo old("the-input-name") so it can retain the previous input before the error
<input name="name" value="{{old('name')}}" class="" />
You should use old('NAME') to get the value of the field after the validation fails, blade example:
<input type="text" value="{{ old('NAME') }}" />
An example In case of update forms you should display the original value from the database unless there's an error:
<input type="text" value="{{ old('NAME') ?? $your_entry->value }}" />
Take a look at the documentation for more details

Coldfusion Server Side Radio Validation

I'm trying to validate that a radio button has been selected via server side in ColdFusion. The text input errors out if empty, however, the radio button group does not trigger an error if none are selected. I cannot seem to find any documentation on this.
<cfform name="mobile_device_request" id="form" action="">
Text Box:
<cfinput type="Text" name="subscriber" message="Please enter in a subscriber" required="yes" validateat="onserver">
Subscriber:
<cfinput type="Radio" name="service_provider" value="AT&T" required="yes" validateat="onserver"><span class="label">AT&T</span>
<cfinput type="Radio" name="service_provider" value="Sprint" required="yes" validateat="onserver"><span class="label">Sprint</span>
<cfinput type="Radio" name="service_provider" value="Verizon" required="yes" validateat="onserver"><span class="label">Verizon</span>
Explanation:
To answer your question, while I have never used that feature, I would say it is a bug.
According to the documentation onServer validation works by automatically appending hidden form fields with a special naming convention. The CF server then inspects all requests for certain patterns in form field names, such as ones ending in _required or _cfformrequired, and if found performs validation on the related field.
If you view the source of your cfform, notice CF has added several hidden fields:
<input type='hidden' name='subscriber_CFFORMREQUIRED' value='Please enter in a subscriber'>
<input type='hidden' name='service_provider_CFFORMREQUIRED' value='Error in service_provider text.'>
<input type='hidden' name='service_provider_CFFORMREQUIRED' value='Error in service_provider text.'>
<input type='hidden' name='service_provider_CFFORMREQUIRED' value='Error in service_provider text.'>
Those should trigger CF's server side validation for both fields. However, as you discovered it only works with the text field, not the radio buttons. Though curiously, it works just fine if you manually add a hidden field ending in _required:
<input type="hidden" name="service_provider_required"
value='You forgot to select a service_provider'>
Answer:
Again, I think the behavior you are seeing may be a bug. Frankly, one of many with cfform. Personally, I would recommend ditching cfform/cfinput. Use plain html inputs and write your own validation instead. Since it is a radio button, the field will only be defined if one of the buttons was checked. So you can either test for the existence of the field:
<cfif NOT structKeyExists(FORM, "service_provider")>
no service_provider selected. do something
</cfif>
... OR assign a default, and test for that value:
<!--- default to empty string --->
<cfparam name="FORM.service_provider" default="">
<cfif NOT len(FORM.service_provider)>
no service_provider selected. do something
</cfif>

Prototype 1.7: Is there a way to serialize elements outside of form?

In Prototype, is there a way to serialize all 'input' elements on the page after looking up using $$? I have to work with some pages that don't have any forms or some elements are outside of form hence cannot use Form.serialize or Form.serializeElements.
Actually you can use Form.serialzeElements() outside of a form you just need to pass it a list of elements.
For example
HTML
<input type="text" name="a" id="a" value="12345" />
<input type="text" name="b" id="b" value="6789" />
Javascript
Form.serializeElements($$('input'))
returns
a=12345&b=6789
try it out in this jsfiddle http://jsfiddle.net/av5Kj/

How to execute code after the jquery.validate validation event?

I am using jquery.validate 1.9 and wish to execute code every time the form automatically validates (using the default behavior).
I hoped there would exist an OnValidated event I could hook into, but can not find one.
After validation executes I wish to conditionally enable other parts of the page if the form is valid, and disable otherwise.
How would one go about adding a method call following the existing validate() function?
I'm using jquery validate 1.8.1, but you should still be able to accomplish this using the valid() function.
valid() will either validate the entire form if you pass it the form ID or individual fields if you pass their respective ID's. It will then return a Boolean based on if they are all valid or not.
Something like this should work:
<script type="text/javascript">
function tab1Validation(){
if($('#field1, #field2, #field3').valid()){
//Logic if all fields valid - eg: $('#NextTab').show();
}else{
//Logic if one or more is invalid
}
}
</script>
<form id="yourform">
<div>
<input type="text" id="field1" name="field1" />
<input type="text" id="field2" name="field2" />
<input type="text" id="field3" name="field3" />
</div>
<input name="nextTab" type="button" value="Next Tab" onClick="tab1Validation();" />
</form>

Adding HTML5 placeholder attribute to spring 3.0 form input elements

How do I add HTML5 placeholder attributes to Spring webmvc's form:input, form:password and form:textarea elements?
As of Spring 3.0 form tags support dynamic attributes, therefore you can simply write
<form:input placeholder = "..." ... />
Regarding the new input types question - I had success by using spring bind and manually generating the input element. I'm using bootstrap so I already had a tag to wrap the control-group and apply the error message, but if you just want to inline it you can do the following.
if your path field was 'age', replace <form:input path="age"/> with
<spring:bind path="age">
<input id="age" name="age" type="number" value="${status.value}" />
</spring:bind>

Resources