Struts 2 validation - clearing the error message - validation

Good day!
I used the Struts2 xml validation as instructed in this website.
The problem is when I clicked the submit button twice. The error message also appears twice...
My question is... how to clear the first error message before another action is processed to accommodate new set of error messages.
Thank you in advance.

If you are using the spring integration you have to define your bean as scope="prototype", then you get a new instance of your Action for every request.
The default scope for spring is singleton.
It's a good idea to do this for every Action.

Remove validate="true" from form tag.

There is a validator interceptor in the default stack. You just need to use that and using that is very simple. Just make a method in your action class by the name public void validate(). Within that validate() you can access the fields using their getters & then put the required validation onto them.
Also, with this implementation you would not have to worry about the multiple messages being shown, because it will show only the message what you set in the addFieldError method and removes any previously kept messages.
NOTE: Be sure to use getters of the variables in your validate(), because the variables in the action are not set at the time this interceptor is invoked.
Here is a link to a very nice tutorial.
http://www.vaannila.com/struts-2/struts-2-example/struts-2-validation-example-1.html

If you will keep validate=false, then validation xml will be called on submit action.
To clear messages on each submit click, if you are using table in your jsp then keep table outside of form. It will work.

Related

How to stop grails erasing custom validation errors

In my Grails app, I am using methods in a service to do complicated validation for user submitted data. Unfortunately, Grails is quietly sabotaging me.
I populate the domain instance with the user submitted data
I hand the instance off to the service, which analyzes the properties.
If errors are found I add them using
instance.errors.rejectValue('myValue','errors.customErrorCode','Error')
BEHIND THE SCENES, when the service passes the domain instance back to the controller grails checks for changed properties and calls validate() before returning the instance. (verifiable by seeing the beforeValidate event called on returning a domain instance from a service to a controller where one or more properties has changed)
That behavior clears any custom errors I have added and the instance I get back in the controller is now incorrectly without error.
How can I
A) stop grails from validating between service and controller
OR
B) prevent a validate() call from wiping my custom errors.
EDIT
So far I've found one partial answer,
If you use instance.get(params.id), grails will self validate behind the scenes wiping custom errors.
If you use instance.read(params.id) you can bypass this behavior to an extent.docs
But this solution is limited by domain relationships. Any other solutions welcome.
Seems that it is not custom validation. It can be because of transactional service. Service opens separate transaction for each method and clears entities after method end. You can find this mentioned in docs(read the last paragraph of part ). So errors dessappear not because of validation.
Don't know if your service is transactional. But if it is - you can add #NotTransactional annotation to method were you want not to loose errors. And
errors will be saved.
Hope it helped,
Matvei.
Not sure how your code looks like or what is causing the problem, but in any case I strongly suggest implementing custom validators in the domain class or in a command object within the constrains.
Here are some examples from grails docs:
http://docs.grails.org/2.4.0/ref/Constraints/validator.html

Laravel validation service doesnt work with custom rules

I have implemented a Gateway -> Repository pattern and want to integrate validation service with it. I have created the service
https://github.com/octabrain/Laravel4-Patterns/blob/test/app/lib/Sampleapp/ServiceProviders/ValidationServiceProvider.php
Which adds the CustomValidator
https://github.com/octabrain/Laravel4-Patterns/blob/test/app/lib/Sampleapp/Extensions/Validation/CustomValidator.php
This is the actual validation that is getting performed on the user data from the controller
https://github.com/octabrain/Laravel4-Patterns/blob/test/app/lib/Sampleapp/Gateways/UserGateway.php
This are the rules for uservalidator
https://github.com/octabrain/Laravel4-Patterns/blob/test/app/lib/Sampleapp/Services/Validators/UserValidator.php
But the hex validator is never getting called !
I have followed these articles
http://culttt.com/2013/07/29/creating-laravel-4-validation-services/
http://culttt.com/2014/01/20/extending-laravel-4-validator/
Please help.
Edit : Full code is available here : https://github.com/octabrain/Laravel4-Patterns-Full
It was my mistake, I was not passing anything in the request so the rule was not getting triggered !
Updated the code
See here : https://github.com/octabrain/Laravel4-Patterns

Redirecting back to Portlet from ResourceMapping in Spring 3 portlets

I am trying to work out a way to provide a CSV download through a Spring 3 Portlet. I have a method that uses the #ResourceMapping annotation to define a handler that takes some report params in the form of a #ModelAttribute, builds the report, and returns it. The catch-22 I am running into is validating the parameters being send in from the client form.
If I make the handler a #ResourceMapping, I can set the headers and write out the report as using the ResourceResponse, but I can't seem to figure out how to redirect the user back to the Portlet view with errors when their input fails validation. However, if I make it an #ActionMapping, I can then check the BindingResults and forward them back to the form as needed, but the ActionResponse doesn't allow me to set the Content-Disposition header nor write out the CSV bytes, which is sort of critical for sending the report back.
I am at a total loss here, as I don't even know what my options are. Is it even possible to do what I am trying to do with a Portlet? Are there other examples I could look at for a possible work-around?
I suggest you to use both #ActionMapping and #ResourceMapping to fulfill your requirement.
As you said you were able to handle the validation errors using the #ActionResponse, I'll tell you how to handle the Resource Streaming.
As you know every #ActionResponse is followed by a #RenderResponse, just return the same view but, with a hidden iframe this time whose src points to the ResourceURL.
Now the Request you receive in #ResourceMapping is something which is already Validated. So, you can now serve your CSV.
I dont know how complex is your UI and if you are using jsp as views in your application. If nicely managed, Validation can be handled by #ResourceMapping.
Thank you

Check Using JSTL/Spring-MVC tag regarding Spring Binding Error

I am trying to find a way by which i can check if there is any binding error and based on them i want to show some messages to the user in Spring-MVC. i know one way of this like
<spring:hasBindErrors name="userName">
</spring:hasBindErrors>
but this seems to be with respect to a specific filed of my form, what i want is to check if there is any binding error at all for the current input form or not?
i have also experience of Struts2 and they have a very convient method hasError() which allow a developer to see if there is any error at all for the input fields.
Is there such method defined for Spring-MVC validation or not?
Not an expert of Spring-MVC as i have used it very rarely but if i am correct all you want an equivalent of bindingResult.hasErrors() and if i am correct all you need to use
hasBindErrors tag. For more details refer to the doc
hasBindErrors
Hope this might help you

DRY Remote Validation in ASP.NET MVC 3

I've read David Hayden's great post on MVC 3 Remote validation.
However there is presented what you should do to enable remote (javascript) validation. If the user has javascript disabled the post would still be made even if data is not valid. Therefore a server-side validation should occur.
How could we make this check as DRY (Don't Repeat Yourself) as possible? Of course, including the same check code in the post action as in the remote validation action (or just the same call) can work but I am wondering if a one-liner or something more elegant is available.
Perfectly acceptable answers include "no, it can't be done". :)
See my MSDN article How to: Implement Remote Validation in ASP.NET MVC
I use the remote client validation code in the HttpPost Create method to test server side when JavaScript is disabled.
[HttpPost]
public ActionResult Create(CreateUserModel model) {
// Verify user name for clients who have JavaScript disabled
if (_repository.UserExists(model.UserName)) {
ModelState.AddModelError("UserName", ValidationController.GetAltName(model.UserName, _repository));
return View("Create", model);
}
It 'can' be done.. but you would need to write your own custom attribute that basically emits for client side and is validated server side. For me I just extract the validation code into a method and check on the server.
Something similar came up recently as well:
Prevent form from submitting when using unobtrusive validation in ASP.NET MVC 3
I wonder if one couldnt inherit from the remote attribute and add their own server side code them. hmm.. maybe I'll have to try this.
I would be happy though if someone here said they already did this : )
I have done this, it's a bit of a long solution, so it's all available on my blog here:
http://www.metaltheater.com/tech/technical/fixing-the-remote-validation-attribute/
I had to create a new subclass of the RemoteAttribute class, create my own custom model binder by inheriting from DefaultModelBinder, and then use reflection to call the validator on the controller.

Resources