How to display bean validation message in modal dialog? - jsf-2

For modal dialog from bean I followed this link:
Automatically show validation messages in p:dialog on validation failure
But at the same time I want to show data-table record also. If I used action event in the button, its showing only modal message, no data table records. In case of using string function in the button, it shows only record, not validation message. How to fix this one?

Related

Angular binding model property to ng-click not passing value to MVC controller method

I have a calendar with events on it, each having a distinct EventId. When a user clicks an event, it triggers an angular controller method which in turn gets the data for the event via an MVC controller and displays it on the page. I also have a button on the page that when clicked should open up a modal to edit the details of the data being displayed and I am using data binding to set the URL for the modal window. The data-ng-click attribute of the button looks like this:
data-ng-click="app.modal.OpenModalWindow({ template: '[my url here]/?eventId={{vm.EventId}}' })"
In the markup I see this:
data-ng-click="app.modal.OpenModalWindow({ template: '[my url here]/?eventId=18' })"
but when I click the button, the browser is trying to do the following (from the Chrome Network tab in dev tools):
GET [my url here]/?eventId={{vm.EventId}} 500 (Internal Server Error)
If I take the URL from the markup or hard-code the eventId everything works fine so I know it is not the OpenModalWindow so I'm thinking it is some sort of binding issue. I have placed {{vm.EventId}} on the page and know that it is getting set properly, but somehow this is not getting to my model.
The error I am getting back is that the parameter "eventId" in my MVC controller cannot be null, which makes sense because I'm sending it a string with the value "{{vm.EventId}}" but the param type is an int. Its like my markup is getting updated, but the data bound to the click event is not. Any suggestions would be greatly appreciated!

How can I use a shared error message Dialog in PrimeFaces

In PrimeFaces, when you want to show an error Message you can:
create in View a command Component that calls an action
in 'update' attribute, you set the id of the p:message or p:growl component that will display the message
in The backing Bean, in the action function you throw a message
As a result, the error message will be displayed in the redirect page, in the message component with the matching id
But what if :
You want to display the message in another page, that doesn't contain the command component that called the action.
The action can redirect to lots of different pages, depending on some Backing bean Logic.
The action is not called from a command Component, at least not directly
I've thought of putting a p:message component with a specific id, and include it in every xhtml page. But this would not necessarily cover the 3rd scenario.
For example, there could be a function that checks the connection to another Web Service. A Connection error could be thrown from lots of different Actions.
Or a session expiration
Or a denial of permission
How would you manage this kind of generic error messages ?
You could put the common <p:dialog> or <h:message> in a template file which is being used for all pages and give it a unique id. That way, it will be rendered for all the pages using that template.
This, is assuming that you're using templates that is.
UPDATE: If you wish to programmatically update the component, you can do so using RequestContext#update()
For e.g.
if (someErrorCondition) {
RequestContext.getCurrentInstance().update("errorDialogId");
}
where errorDialogId is the ID of the common error dialog.
If you intend to use this approach, you need to remove the update atribute from your command component.
See Also
Calling Primefaces dialog box from Managed Bean function
Why not use the RequestContext#showMessageInDialog(FacesMessage)?
As per their User Guide:
Displaying FacesMessages in a Dialog is a common case where a
facesmessage needs to be added to the context first, dialog content
containing a message component needs to be updated and finally dialog
gets shown with client side api. DF (Dialog Framework) has a simple utility to bypass
this process by providing a shortcut;
Using this you don't need to have additional codes to put in every page you have. Just add it in your beans.

Submitted value scope

I have a primitive view: a form containing a validatable text input component and a command button. The input value is pointing to a session-scoped backing bean.
I open the page, enter an invalid value and submit the form: after the postback, a validation error is appearing and the input component is displaying the submitted value which did not pass validation. The model value in my session-scoped bean is left intact, as expected.
Ok, now I open another tab in the browser and open the same page. To my surprise, the input component is displaying the submitted value from the first tab. I've been supposing the view state to be new at another GET request and the plain model value from my session-scoped bean to be shown instead.
If I use a view-scoped bean instead of a session-scoped one the model value is being rendered for the input component in the second tab, not the submitted value from the first tab.
Is the submitted value not the part of the view state and kept somehow along with the model? Or is its scope tuned up in some smart way depending on the referenced bean's scope?
Sorry in advance if this question is stupid but I will be very grateful for removing my misunderstanding.

MVC Controller Error Messages

In MVC 3 app I have a few conditional elements in the controller. for example I have a number say "10" which has met the model state requirements but I have a if statement that checks if the number "10" exists in another table. Should it exist the data is submited but should it not exist I return the view and would like to return a error message.
My question is what would be the best way of displaying a error in this situation. I have looked at returning a viewbag message but I would like to style the error message with a box and by adding this style to the view it always gets displayed which is a problem.
You could add the error message to the modelstate:
ModelState.AddModelError("somekey", "some error message");
and inside your view display error messages using the validation summary helper:
#Html.ValidationSummary(false)
You could of course add a string property to your view model and set its value in case of error. Then inside the view check whether the model property has a value and if it does display the error messages inside a custom styled element. It seems a bit like a wheel reinvention assuming you could simply append the error message to the modelstate but worth mentioning.

I need to remove the jquery validation messages from a modal form

I am using a Jquery UI modal form to post data back to my action and it works fine. However when I put client side validation on and the user closes the modal without submitting the form retains the validation messages and styles.
It there away to clear the validation messages on the client? What element is the message wrapped in?
You could use the .resetForm() function:
var validator = $("#myform").validate({
...
...
});
And later when you close the modal dialog:
validator.resetForm();

Resources