Can I use a JSF validator both ways? - validation

I have an application where you can create Items. Those items are saved in a database with the primary key beeing its itemnumber. In the facelet where you create the Item I have a h:inputText with a validator which checks if the item is already in the database or not. If it is I give out an error message. I now have a second facelet where you can order Items by placing its number into an inputText field. Now I want the Validator to react in the opposite way as before.
So is there any way I can define the expected outcome of a validator? Or do I have to make a second validator?

Related

Validate multiple fields inside p:dataList in JSF

I have to validate a name and group field for uniquness for an item. The user can create several items. When the users clicks on a save button I want to check the name and group for all items. In the backing beans the values of the last item are always not set in the validate method. For the last item the values are not set because I am inside the validation at this time. But I do not know how to get the inputtexts name and group for the last item and call getSubmittedValue() because all is inside a dataList and the ids are generated. Any idea?
Cheers
Oliver

JSR 303 - Validate a field if it is not null

I have a form which is filled in, and some of the fields are options. I want to apply validations on the information that was filled in but the optional fields should be validated only if there was something filled in, so if theses are not null. Did any of you do something similar?
Or do you have any suggestion?
Bean Validation constraints usually accept null as valid value (with the exception of #NotNull of course). Depending on your UI framework you might retrieve empty strings instead of null for fields without user input. If you're are working with JSF 2, you can set the context parameter javax.faces.VALIDATE_EMPTY_FIELDS to false to avoid a validation of empty fields.

Spring MVC update field based on calculation

I have page with spring form fields.
They're bind using command.
Couple of fields in form need to be update based on calculation from other fields.
Those fields can be modified by user.
How can I do that?
I think about trigger java calculation in model using onchanged in web page. But I don't know how access methods from model and how read fields from page.
I notice, that field are update by setter when page is submit. This is too late if I want calculate 'live' when depending fields are changing.
If you need these calculations to be done on server (java model)... I suggest to use input's onChange event as you said, send values with AJAX to server and calculate there, returning the result to client again for update UI in real-time.
With this approach, you will get the 'live thing' you are wondering for.

Custom Data Annotation Attribute using Metadata

My requirement is to create a custom data annotation attribute for my project. The requirement is to validate the min/ max length of a specific product from the database, which will be retrieved from the database using the ProductID. I have a dynamic page for each product where there are two fields called max length & min length. User inputs the values in these two fields which needs to be validated from the database. Product table contains all the products & one will be selected by passing a productId.
Please suggest some pointers to implement the above.
Thanks in advance.
This validation you can do only in the server side and not in client, so I see two options.
Remote Validation - You can use remote validation when you want to perform the validation and show the error message through ajax.
IValidatableObject - By implementing this interface in the class you can do both the validations at the same time and return all the validation error messages as a collection. By this way the validation will happen after the form is normally submitted.

How to Bind Data coming from DB to spring input tag

i have the following scenario.
I have a list of auctions coming from the DB. I am displaying each auction properties on the JSP page as a record.
I am using spring 3.0.5, Tomcat 6.0.29 and eclipselink as JPA provider.
In my controller i am seeting this list as model attribute and passing it to the JSp page.
I want to use spring tags (form:input) to display the data.
Using Form backing bean we can display default values on a form. But in my case is there any way to bind data to form:input tag in the JSP?
Thanks
There is no reason to display your data in form:input tags other than that you want to allow your user to edit the data. If that is not the case, then using form:input just to display the data is pointless.
If you want to allow your user to edit the data, how are you going to get back the changes he has done? Obviously you are going to need your Command object. So there is no reason not to use formBackingObject() in your FormController to set values in your Command object, which will be consequently displayed on your JSP. When user will make any changes and submit the form, you will get all those modified values in your Command object which you can process in onSubmit() method of your FormController.

Resources