I am new to MVC4. I have a form for displaying user roles and a link for editing.
This template is created by entity scaffold.
Whenver user clicks edit link a jquery modal popup displays for editing. The form contains 2 fields ID and User Role .
I wished not to display the Id part so i commented the ID part display . But after updating i will get the error like
Store update, insert, or delete statement affected an unexpected number of
rows (0). Entities may have been modified or deleted since entities were loaded.
Refresh ObjectStateManager entries
I then debugged and found the ID was 0. So I tritd with displaying ID It worked.
why is this happening. How can I dispaly and edit form without ID field.
Add a hidden input for the ID property in your form
#Html.HiddenFor(m => m.ID)
or add a route value in the form (refer all the overloads here)
#using (Html.BeginForm("actionName", "controllerName", new { ID = Model.ID })`
Related
The Setup
I am working on a project using Symfony 3 and SonataAdminBundle 3.1.
I am using the nested form method of sonata admin where I have 4 entities:
Category, Product, Supplier, and Contact. They all have one-to-many relation with each other respectively.
I am using sonata_type_collection to put products in category form, and using the same to put suppliers in product form, and using the same to put contacts in the supplier form. The contact form has a sonata_type_model_list field for zipcodes.
The Problem
I am facing the following error when I click the add new contact button on the supplier form from within the category > product form.
Could not get element id from
s58c147845272f_products_2_suppliers_0_contacts Failing part: contacts
The error does not show up when I save the form step by step - like i add a product to the category form then save the form, then add a supplier to product and then save the form, and when i add the contact - the contact form is loaded and the above mentioned exception is not thrown. Also When I go directly to Supplier form and add the contact there, the exception is not thrown.
It is only when the supplier is not saved from within the category form that the above exception is thrown.
any help is highly appreciated.
Check if any of your properties have underscore in the name, like my_file. Try to change it into myFile and also accordingly change code in your Admin class:
$formMapper->add('my_file', 'file'); => $formMapper->add('myFile', 'file');.
Update:
I think I know what it is specifically - you probably have reference to your parent in your children. If that's the case, then add in your Admin classes for entities that have reference to the parents:
$formMapper->add('[parent_reference_attribute]', 'sonata_type_model_hidden');
Replacing [parent_reference_attribute] with name of your field referencing the parent.
Then exception should be gone, at least it was in my case.
I have a main form with two subforms. The first subform (subformList) displays a list of employees and a second subform(subformDetails) provides the details for that employee.
I use the practice of having a control field in the main form hving the value of the chosen employee in the subformList subform and having the master/child link of the employee details subform based on this value.
Up to this pointe everything works great. I choose an employee is one subform and the other subform displays the new details.
The problem I have is that I need a of one of the fields in the subformDetails form to execute after the details change from clicking another employee in the subformList subform. If I code a requery of the control field in question in the subformDetails field, it simply requeries with the old value (the control is a listbox with a query as a record source. As expected I can click on the same employee in the subformList subform a second time and the correct details are displayed in the control field in question.
So the question that I have is: Is there a way to rigger an event AFTER the forms have updated each other.
Note that I thought I could simply have an event one of the subformDetails controls so when the other subform forms forces a change then there would be a trigger to perform the requery I need. However it seems events are based on direct changes and not coded changes from other forms.
Whilst writing the question I thought that maybe what I should do is manually force the subformDetails to be refreshed before executing the requery for the control (listbox) and this worked.
So just to summarise
When I click on an employee in the employee list subform I have an onclick event that would run a requery on a listbox in the employee details subform. Unfortunately this requery uses a control box in the employee details subform that does not get updated until sometime after the onclick event (the relationship between the two forms is established with the link master/child and a hidden field in the main form.
The fix is to requery the employee details subform before I run the requery for the listbox. This is because click on the employee in the list changes to record source row and refresh the form makes sure the control has that new value before the listbox requery is executed.
I still don't know what triggers the form change between the two subforms in the first place. It must be performed implicitely.
I am a beginner in java play framework. I an building an app which will take form data spread in multiple pages say 4 to 5 pages. But all the data is mapped to same model. How can I get the data uploaded by user per page, validate against my model's constraints, and at the end save whole data in my model.
For ex:- If page 1 has name field which is required, and page 2 has hobbies field which is required. Then how can i specifically validate data filled in that particular page, navigate till last page, and save all the data in model, in last page.
Model would have 60-70 fields.
I am using Hibernate ORM.
Thanks !
You could prefill the next form with the values of the last form, save them in the database, go to the next form, load the entered values from the values and pre-fill the next form and so on. For this, use a method in your controller:
public static Result fillOutForm1(){
Hobby form1 = new Hobby("sitting still", "diabetes");
Form<Hobby > preFilledForm = newHobbyForm.fill(form1);
return ok(views.html.YOURVIEWCLASS.render(preFilledForm);
}
With this you "send" some values to the first view class with a form. In there, the user answers some more fields and hits Submit.
So you need a route back to your controller, to handle the new input in routes.conf:
GET /Form1 controllers.Application.fillOutForm1()
POST /Form1 controllers.Application.sendForm1()
This is in your controller class:
public static Result sendForm1(){
Form<Hobby> boundHobby = newHobbyForm.bindFromRequest();
Hobby newHobby = boundHobby.get();
//In your hobby class you need a finder implementation, so that you can interact with the database. With save() you put it into the database. This may be different for the database you use!
newHobby.save();
return redirect(routes.Application.index());
}
You can then not redirect to the index, but to the next view class with the next form and there get your last answers from the db, prefill them again and put them into the form. Do this as often as you need.
I have an object displaying within a form, a hidden field being related to its PK (field id).
When I create a new object, the field has a null value. Submitting the form, the object is inserted into the DB and it now has an ID, but the field in the page still has a null value.
If a reload the page, now the ID is indeed set in the hidden field.
In my opinion, this is due to the form processing of Symfony: when a create an object, it creates a form, with this form valid the object is saved but the form still uses the data before it was saved.
The question is: how to get the auto-incremented key in the form up-to-date? Shouldn't the form only have a reference to the object? Can't the value be updated?
make sur that you call $entityManager->flush() method after insert and that you bind your form whene you have same data in your request object
$form->submit($request->request->get($form->getName()));
You should have an Entity assigned to your Form by FormFactory. Then Symfony will fill that Entity with submitted values. What's left is only persist the Entity and flush to database.
You can find steb-by-step form submission in Symfony Cookbook
I currently have the option in my application for a user to select favorite Items from a list. The form element is of type Entity and shows the list of objects. Users can select a number of them as their personal favorite.
However, although the list must show Items, what has to be stored is actually a FavoritedItem (which contains extra information about when/how the user favorited his Item).
I cannot figure out how to convert the entity types so that the user can pick from a list of Item entities but that the form stores FavoritedItems connected to the user.
I guess your entity has a relationship with FavoritedItem, when you are creating your form, don't include that field, but a dummy field to contain the data the user selected:
$builder->add('favs', null, array('property_path' => false, 'type'='entity'.....));
Find this line:
if ($form->isValid()) {
// perform some action, such as saving the task to the database
return $this->redirect($this->generateUrl('task_success'));
}
and for each of your items, set the appropriate FavoritedItem on your entity. It would look something like:
$favorites = $form->get('favs')->getData();
foreach($favorites as $f){
$FavItem = new FavoritedItem($f);
$yourEntity->addFavorite($FavItem);
}