Hibernate Validator Depends on - hibernate-validator

Is there a way to achieve depends on behavior with hibernate validation. For instance if i have two custom validations
#InvalidAmount // Validates the amount is invalid with some custom logic
#AmountNotAccepted // Validates the currency is not accepted along with some custom logic
The idea is not to merge both of them together and throw second error only if the first one succeeds. Is there a way to do it? Something like run the second validate only first first is not an error.
For example:
#AmountNotAccepted(dependsOn = {InvalidAmount})

You may be after group sequences?

Related

Having multiple validation errors in single error message in Peoplesoft

Is it possible to get multiple error messages for missing required fields in a single error message to the user? For example, if I left all 5 of my required fields blank, the error message that is displayed when clicking Submit will show all 5 messages in the same window.
This is a fantastic idea and should be added to the PeopleSoft Idea Spaces in MyOracle Support. Unfortunately, this is not delivered. There are multiple ways a person might trigger an exception, including:
Choosing an invalid value
Leaving required fields blank
Failing FieldEdit or SaveEdit PeopleCode
Unfortunately, PeopleSoft fails on the first, not all. Regarding the last item: FieldEdit/SaveEdit, as soon as you trigger the PeopleCode Error function, PeopleCode halts, so it is impossible for us to use the Error function to queue multiple exceptions.
With all that said, nothing is impossible. One way to accomplish this would be to use our own JavaScript/CSS to mark all fields that fail validation. This would require us to write additional PeopleCode, etc., to work around Oracle's delivered validation.
One approach to do that is to perform the validations "manually", stack then and present to the user.
In order to do that, you need to have a custom "Save" or "Validate Button", and your peoplecode would stack the errors.
You can use the vanilla required fields validation + any conditional validation you need.
A good way to present this is just have a field(long) or an html area and nicely present the errors:
Emplid is required
Setid is required
Enter an end date less than start date
But otherwise, this is not part of Peopletools, although some modules do offer this (eSupplier Portal for instance has a similar feature).

Do axon state-based aggregates have a way of specifying #CreatedDate and #LastModifiedDate?

When creating an Axon JPA state-based aggregate is there a way to mark certain fields as being the #CreatedDate and #LastModifiedDate (as is possible with spring data jpa)?
In other words does Axon have the functionality where if any state of the aggregate is changed then axon automatically updates the #LastModifiedDate without us having to repeat it in every #CommandHandler?
Try using #CommandHandlerInterceptor inside your aggregate to intercept all commands and set
lastModifiedDate field.
#CommandHandlerInterceptor
public Object intercept(Object myCommand, InterceptorChain interceptorChain) throws Exception {
this.lastModifiedDate = Instant.now();
return interceptorChain.proceed();
}
I believe the proper solution would be to implement Axon's HandlerEnhancerDefinition interface to update these fields. This way you can grab the same timestamp (Instant) from the event that gets persisted in the event store and use that on your state-stored aggregate to make them match.
I wrote a blog post with a working example with a detailed explaination how to do this: https://michael.jeszenka.com/automatically-updating-timestamp-fields-for-axon-state-stored-aggregates/
Essentially, you will need to implement the wrapHandler() method to specify which types of event handlers you want to wrap with your enhancer. Then you will need to define a wrapper class to execute your desired behavior, which in our case is automatically handling the timestamps of the state-stored aggregate. This wrapper class will need to implement the Object handle(Message<?> message, T target) method which will allow us to grab the event timestamp from the meta data and use it to set the state-stored aggregate fields.

the best way to handle

I am writing a module(frontend extjs 4.2.1, backend asp.net mvc with the EF). I meet a little problem:
when the user clicks the search button, a panel of extjs will be displayed and he/she can fill some blanks inside the textfield. after the filter information was submitted, the sever side will use appropriate c# code to deal with it to filter some records from the mssql database, here is my problem:
if the user inserts nothing into the field, the standard and best practice will be that this field will be neglected, however, the blank textfield's value will be '' which I can not use this as the filter string, for instance:
there is a textfield named "sex", if the use types nothing into the field, the value passed to server will be '', if i write the lambda expression in this way:
var filter = x=>x.sex ==""; Apparently it will not work. You may say that i can use if-else to condition out the stuff. but if i have numerous fields, using the if-else will be really waste of time.
so, what is the best practice to do this
There are various ways to write this condition, but what you're talking about is a condition so it's not really "a waste of time" to write the code for it.
For example, if you have an IEnumerable<T> and you want to filter it, you might do something like this:
if (string.IsNullOrWhitespace(someFieldValue))
result = result.Where(x => x.SomeField.Equals(someFieldValue));
Or if you're building up expressions, you might conditionally define the expression. Which might look something like this (this is entirely freehand, so I'm not sure it would work like this, but you get the idea):
var filter = string.IsNullOrWhitespace(someFieldValue) ?
x => true :
x => x.SomeField.Equals(someFieldValue);
You can abstract this behind some private helper methods so that consuming code is a bit cleaner, you can refactor commonly-handled filters to reduce duplication, etc. But at the end of the day the logic your code is expressing is:
In a specific known state, do one thing. In other states, do something else instead.
That's a conditional statement.
It can be prevented at client side before request submitted to server.
At server side , You can abstract validation part from service layer.
In my Typical Spring MVC application -
1.Request goes to controller.
2.Controller delegates to Validator.
3.If success, controller delegates to service layer.
4.If failure, controller redirects user to error page.

How to filter in Symfony2?

First of all i am very new to Symfony 2 and started to learn.
Is there any possibility for filter a value? Perhaps filter chains too?
I know this concept from Zend Framework 1 and 2.
E.g.:
i have a string "1A- N "
now i want to filter so that only numeric values pass; result "1"
Do i have to implement this on my own i Symfony?
I would like to do something like:
$text = '1A - N';
$numberFilter = new NumberFilter();
$filteredText = $numberFilter->filter($text);
//now in $text i find '1'
But for now i nowhere found something like this in Symfony what surprises me a lot. I thought it is a full stack framework and such function is so basic.
I found something like validators but they only say if a value e.g. contains only numbers or not. Or is the validation concept of symfony like that it does not only say if it is numeric or not but filter all other smybols out, too?
Depending on what you want precisely:
disallow user input not fulfilling certain rules
use validators in forms
use asserts in entities
chenge user input in case it's wrong
use viewransformers in forms
use event listeners in forms
use event listeners for doctrine
change data that already exists in the database
use filters in twig
create a command to execute from commandline
You can also try http://php.net/manual/ro/filter.filters.sanitize.php
I have built quite big apps with Symfony and never needed such a feature. Filters are mostly used in views anyway. Symfony comes with Twig, which has filters, that can be chained, and you can write your own filters. But if you need filters in the backend to do some background processing, you can accomplish it the way you suggested.
I suggest you write an interface and use a factory pattern, so you set a standard if you make many filters, so it will be easier to make the chaining work;)
After the answers and searching i got to the following conclusion.
There is no concept for this for now in Symphony 2.
You have to write it on your own.

Model Binding and Validation Errors

I am using asp.net MVC3 and I am very new to this technology.
My models are designed in such a way that the properties will throw validation errors if the data is invalid. In this case, the properties are not set with invalid data.
When I redisplay my editing-view, validation error messages are shown; however the values that the user previously entered are gone because the model that it is bound to only contains the old-valid data.
For example, say I had a Person class and the Name property cannot be a null or empty string otherwise it throws a validation exception and prevents the property from being set. Now say the user removes the value from the Name property and tries to save the Person from the web. A validation exception will be thrown and handled properly to add the error to the ModelState so that it is displayed on the screen; however the old value for the Name is redisplayed since the invalid, empty string never made it into the property.
I do not know how to solve this problem and any advice on the issue would be greatly appreciated.
My advise is allow invalid data but use validation attributes. You wont save invalid entities so there is no problem and this is the standard approach these days. If you don't want do that, there is no easy solution. Most simple solution would be using the info from Request.Form
You should implement IValidatableObject to performe this kind of validation at server side.
From MSDN IValidatableObject Interface:
Provides a way for an object to be invalidated.
Theres an exemple here Using IValidatableObject Custom Validation, also from MSDN.
The solution to this problem was to create a ViewModel that allowed invalid data to be entered into this. This solution also simplified my ModelBinder classes because it took on most of the work.

Resources