Wicket dropdown validation onchange - validation

I have a dropdown in wicket 1.4 (part of a form) which shows choices for numbers. Now when a number is selected, I change the status of that number to reserved in database using onUpdate method of dropdown.
Now, I want to create a validator which checks whether selected number is already reserved in database or not (by some other parallel process). But this validator should validate only on value change before onUpdate as the number will get reserved in onUpdate method.
So basically what I want to do is to manually invoke validator when I need and prevent it from getting invoked at other times
Or simply, how to make validate and update event work together

Just curious; why you would want a validator to do this job when you could potentially add a database call method inside onUpdate itself?
You could still create a reusable method to call it from different places and use it. Validator is something you want to use for format/range kind of validations ( This is my choice - not necessarily best one ) and have business logic validations either in service layer or close to DAO layer if I don't have 'service' layer.

I think this will never work. As you validate before the update, there is still a chance that the value was changed by another process.
I would use a nice feedback message telling the user that we were unable to update the number because someone else just did it before you.

Related

SpringMVC Validator Needs to be Also Used Remotely, not just on immediate screen

I ran into this issue: our SpringMVC Validator validates a form (the normal case); this works OK.
Also, in a separate use case, it needs to be called remotely from a different location to put an exclamation point icon ("!") in front of some element on a different screen that refers to that form object.
I can validate any object of the class supported by the Validator, but the issue is how to add errors:
1) On the one hand, this Validator should reject errors for the path on its screen when it's used in the normal case:
errors.rejectValue("object.field.path", "errorCode", args, defaultMessage);
2) On the other hand, this will fail when it's called remotely, this path doesn't exist because the form is different. The only thing I can do here is something that doesn't depend on paths,
errors.rejectValue("errorCode");
Ideally the same Validator should respond to both cases... It should tell me there are validation errors on the form. In Case A it's specific, in Case B it's general. Can anyone advise how to approach this?
UPDATE The issue is general Bean Validation vs. GUI Validation. I understand is that SpringMVC's Validators are strongly tied to the GUI. But we also need a generic way to validate data beans, and hopefully reuse that. Or maybe I can create a "mock" BindingResult/JSP?

Where to put validation logic in MVC software architecture

I am actually starting to learn the mvc architecture.
I'm confused on whether to place my username registration validation logic in model or in controller.
I have some sort of status message that would tell the user whether the new username to register is available or not.
My confusion started because most sources say that it should be in the model because it involves the username data to validate before placing it on the database (rather than checking inputs to the username field). However, the status message should respond immediately prior to the change in the username field by user keypress or change, which lead me to think that it should be in the controller because it involves more on user events.
My concern is not actually on the framework to use but on the standard concept involving MVC. Where do I put the username validation logic based on the conditions/premise above?
As Shikhar says, the actual checking of whether the name is acceptable/available is a Model responsibility. The controller can provide an action that is called by some AJAX on the page, so that as each key is pressed, the text on the page is sent to the dedicated controller action which then validates it through the model (anything that touches the database is Model).
There are a couple of things to consider in the view, such as when the user is typing quickly, you should cancel the previous calls before making the new one as this can get confusing.
Also the controller post action that happens when the user submits the form at the end of their data entry should perform the same validation as the AJAX action did just to avoid race conditions between users.
It should be in the model as you have read yourself. I think you are getting confused between the "validation process" and "validation rules". Validation process will either be in controller on the client side but validation rules are properties of model.
As an addition to #Colin Desmond, a model-instance should never contain 'wrong' data and thus, in my opinion, in an MVC environment should contain validation logic. So that, regardless of the place where an instance of the model is created, it can never be initialized with wrong data and classes that operate on the model instance can rely on its data. The exception is if there is validation logic that is view dependent. View dependent logic (exceptions) should be implemented in the controller.
For example, validation of an email-address should be implemented in the model. However, a model might allow a bank transaction with a negative amount, but view A might not allow a transaction with a negative amount. The logic for this exception should be implemented on the controller.

Validating using stripes causes bound values to delete

I'm using Stripes and I am validating the values of a drop down box to ensure the user selects an option.
On initial load all data is present, but once the validation kicks in the form loses the data that was set up in the action bean on load. This includes the original list I am validating against.
I'm simply using in the jsp, and annotating the field in the action bean as #Validate(required=true).
Am I missing something simple?
Cheers
Fixed it.
I should use #Before methods to pre-populate domain objects, as per the Stripes best practice.
manual

Validate a Collection Has at Least One Item using Validation Application Block

Using the Enterprise Library 4.1 Validation Application Block, how can I validate that a collection property contains at least one item?
I'm assuming you mean out of the box. If so, then I don't think there is way to validate directly the number of items in a collection.
These are some other ways that you could try:
Decree that you only deal with null collections and not empty collections and use a Not Null Validator. Not practical, though.
Use self validation and have the object validate in code that the collection(s) have the correct number of items. Will work but it's nice to have the validation in the configuration file.
Expose the collection count as a property. This could be done, assuming an employee collection for example, with an EmployeeCount property on your object that contains the collection or you could create your own custom collections that expose a count property. Then you could use a Range Validator to validate on the Count property.
Create a custom validator that can validate the number of items in a collection -- something like CollectionCountRangeValidator.
If I wanted to develop something quickly, I would probably go with option 3. However, option 4 fits in well with the Enterprise Library approach and also allows your class design to be independent of the validation requirements. Plus you could always reuse it on your next project. :) And does anyone really miss creating their own collections when a List will do nicely?
This is already implemented in the EntLib Contrib.
This is called CollectionCountValidator.

What is the best way to handle domain-centric validation while providing a rich UI experience?

My company is developing a GUI application that allows users to query a legacy database system and have the results displayed back to them on the screen (the results just come back in a blob of plain-text). I'm struggling with the best way to structure the interaction between the user interface and the domain layer, especially validation of user input.
Basic Use Case
User selects a query to run from a menu in the application.
The application code displays the data entry form for the selected query.
The user enters the parameters for the query. If a field contains invalid data, it is immediately highlighted in red, and its tooltip text is changed to display an error message (i.e. if you are entering a Person query, and you enter a date of birth in the future, for example, the date of birth field will immediately turn red).
When the user clicks Run Query, the application runs a second validation pass; this second validation pass is required in order to run validation checks that involve multiple fields. If the this validation check passes, and all the fields are valid, the query is sent; otherwise, the user is prompted to fix any remaining errors.
My Current Validation/Error Reporting Strategy
Currently, I'm using domain-centric validation, but the overall design seems messy to me and maybe a little too over-engineered. A brief overview of the current design:
Domain layer: I have one class per query. Every query class contains a collection of IQueryField objects that hold the values entered by the user. Each query class implements a common IQueryMessage interface, which defines (among other things) a Validate method. This method is called to enforce message-level validation rules (i.e. rules that must examine the state of multiple fields at once). The IQueryField interface also defines a 'Valdate' method (among other things). This is to support per-field validation rules.
Per-field validation: To handle the per-field validation and error reporting, the data entry code binds each input control to an IQueryField; whenever the user changes the value of a control, it calls the the corresponding IQueryField's Validate method, which in turn fills a Notification object (just a collection of strings at the moment) with any errors detected in the value entered by the user. The user interface code then checks the Notification object and changes the appearance of the user control to indicate an error condition, if necessary.
Message-level validation: When the user tries to send a query, the application calls the Validate method on the IQueryMessage instance associated with the data entry form (at this point, the data binding code has also ensured all the message's fields have been populated from the input controls on the form, and the per-field validation code has been run). If there are any validation errors, the user interface displays them at the top of the form. If there are no errors, the data entry form is closed and the query is serialized and sent over the network.
Is Something Wrong Here?
I feel like something isn't "right" here. I have a few issues with the current design:
I would like the domain-level validation code to indicate the name of any fields that are in error, bur I don't want to hard-code the UI label captions into the domain classes. One possibility I thought of was to have the domain-level Validate methods generate messages with a field placeholder, such as "%s cannot be in the future", and have the UI code fill in the placeholder with the correct label.
The IQueryMessage and IQueryField interfaces both have a method called Validate. I'm thinking this should be extracted into a separate interface, (IValidatable perhaps), but I wonder if I am making things needlessly complex.
I'm using VB6, so I can't use inheritance in my classes (VB6 supports classes but not inheritance). I can only define and implement interfaces. Because of this, and because of the way my current interfaces are designed, I'm duplicating a lot of boiler-plate code in my implementation classes. I am thinking of solving this with an inversion-of-control approach. For example, I was thinking of defining a single concrete QueryField class, which could be initialized with a collection of IValidationRule instances that define what validation rules to use, then the QueryField.Validate() method would just collect the results of executing each rule. This way, the validation rules can be tailored to each field, but the QueryField class can handle all the common field-related stuff (field name, field length, required/not required checks, etc.).
How Can I Improve This?
I'm interested in any refactoring suggestions and hints on improving the current design. Also, I'm not necessary tied down to domain-centric validation; other suggestions are welcome. The main motivation behind using domain-centric validation was to keep increase encapsulation, and allow query message and field objects to be used in a non-GUI environment, without having to rewrite all the validation logic.
When you initialize a QueryField object, pass a label to it from the GUI. Then it's the UI that is responsible for setting the label name which seems reasonable to me.
I don't think this is necessary.
What you are describing doesn't really sound like IoC but rather just plain old composition. Since you can't even use inheritance this improvement seems to make sense. Generally you want to prefer composition to inheritance anyways. However if you are almost done with the work then I wouldn't bother refactoring this late in the game.

Resources