I am using JSF 2.0/CDI and PrimeFaces 2.2.1 for a number of CRUD forms that let the user view or update the attributes of an existing entity by clicking on a link in a datatable, where the identifier of the entity is passed to the CRUD form as a View Parameter. I display the entity's ID (often just an integer) on the CRUD form in a PrimeFaces InputText field with the readonly attribute set to true (since I can't let them change it), so the user knows which entity they're editing. The backing bean of the CRUD form is RequestScoped, which works fine except when validation fails. In that case, the value of the View Parameter is lost, so a 0 is displayed in the entity ID field on validation failure.
I am able to maintain the actual entity ID in a hidden field so it's available to update the database once validation succeeds, but it's rather maddening that I've not been able to find a way to maintain the value in a visible field of some sort after a validation failure. Ideally the InputText field would retain its functionality as an inputted and validated field even with its readonly (or disabled) attribute set to true, which would let me forgo the hidden field entirely. But it doesn't appear that I can make it work that way. Any suggestions besides making the backing bean ConversationScoped, which I'd prefer to avoid?
Actually, after stating what I'm looking for a little differently in a Google search I found a novel suggestion at the link below that seems to work cleanly. Instead of making the entity ID field readonly or disabled, I leave it enabled but blur it as soon as it receives focus. I'm able to get rid of the hidden field, the user can't change the value and it survives a validation failure.
<p:inputText id="entid" value="#{RequestBean.entityID}" onfocus="blur();" />
http://www.codingforums.com/archive/index.php/t-1738.html
Related
We have a ViewModel for "create" and one for "edit." The edit inherits from create so that we're sharing common fields. We are then using an #Html.EditorForModel("User") that uses the "create" as its model.
This is for editing users, so I need the password field to be required on create, but not required on delete. Thus, the create VM has Password with [Required] decorating it, while the edit VM has password property with no decoration, and is also declared with new (public new string Password {get;set;}).
This is almost exactly similar to this question:
MVC4 Conditional Required Annotation
For my situation, it's still requiring the field I believe due to the EditorTemplate using the create object as its model instead of the edit. I tried changing it from editorformodel to:
#Html.EditorFor(model=>model, "User")
in hopes that because edit is actually a "create" due to inheritance that it would work, but it'still barking at me for not providing the required field when I post.
Is there a way to accomplish what I'm attempting, or are my options to either remove the required and handle it server-side, or split the editor template into two templates?
You can create a custom attribute to drive your check on whether the password is required or not. When the submission happens to the server your custom attribute can check to see if the you are dealing with an Update or and Insert and then invalidate the model if it needs to.
Here are some resources on creating custom attributes and custom attributes with unobstrusive jquery validation
Happy Coding!!
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.
I have made a web application in MVC Razor. When I post/submit the page DATA ANNOTATION validation fires on those fields also which was hidden/or not shown to user. As dataannodation [Required] validation are put on hidden properties also, I am getting False value on ModelState.IsValid. And since the project is on the verge of completion we can not change or remake the MODEL Class.
Please somebody suggest me how to achieve this..If my question is not clear please let me know.
You can use
ModelState.Remove("FieldName");
to remove entries in model state, that are related to hidden fields.
Please at least make sure, that safety of the system is not compromised by using [Bind(Exclude = "Property names")] attribute to disable binding of fields, that should not be sent from form.
I have some validation controls on my gridview and would like them to anly be active when I try to save/update data in a gridview row and not when cancelling using the cancel button. How can I implement this.
Thanks in advance
Set CausesValidation="False" for the Cancel Button.
Control.CausesValidation Property
If the CausesValidation property is set to false, the Validating and Validated events are suppressed.
Also, specify "ValidationGroup" for your controls:
Validation groups allow you to organize validation controls on a page as a set. Each validation group can perform validation independently from other validation groups on the page.
You create a validation group by setting the ValidationGroup property to the same name (a string) for all the controls you want to group. You can assign any name to a validation group, but you must use the same name for all members of the group.
During postback, the Page class's IsValid property is set based only on the validation controls in the current validation group.
Reference Links:
Control.CausesValidation Property
Specifying Validation Groups
I have the following model:
Customer:
ID
Name
Address
Phone
Fax
I added an Edit view based on the above model from the controller. I modified the Edit view to only allow edit on the Phone and Fax field (deleted the rest). When I submit it I get an error. It works if I leave the Edit view untouched (5 fields). However I only want to allow change in the last 2 fields.
I am lost, please help. Thanks :)
If you are using the MVC ability to populate your entity/class i.e. your action sig looks like this:
ViewResult MyAction(MyObject object) {
...
Save(MyObject);
}
then you'll need to make sure you include the other field, non-editable, either as visible information or using Html.Hidden within the form scope to ensure you have a fully populated object. Remember, the web is stateless and the server has no idea which record you were editing unless it has the keys to do so retrospectively.
The other option would be to retrieve the original object (for which you'll still need the primary key) from the database, update the fields from your form data and then submit the changes. We'd need to know the specific error to be able to help further, the code you are using would also be a great help.
Without knowing more I would guess it has something to do with binding null to a non null property in your model. Can you give me more details on the model, the error.
If you are using the default mvc model binder then it will only bind the fields you submit. So either submit as hidden or dont use a model binder and manually map the variable from Request.Form into a copy of the model you pulled form the db.