Ecto cast_assoc Pheonix edit form validation - phoenix-framework

I am working on Ex_admin setting up the ability to have custom changesets. We are using the new cast_assoc and put_assoc features in Ecto 2. I am curious how people are handling the following scenario as it doesn't seem to be well supported to my knowledge by the Ecto Pheonix form combo:
We have a schema with a has many relationship.
The user goes to an edit form, edits the data in the parent schema in
an invalid way.
The user deletes a has many relationship.
The user submits the invalid form.
The changeset with the validation error is used to re-render the edit form so the validation error can be fixed.
In order to remove the relationship using cast_assoc, you actually need to remove the deleted item from the params passed into the cast_assoc method. When the parent schema is valid this works ok. However when the edit form needs to be re-rendered using the invalid changeset the deleted assoc is missing. This is a weird ui experience since the user should see the same state as what they submitted, with the errors added.
So how are people getting the deleted association back into the form?

So it seems you can do this on the related schema side by attaching a delete action to the nested changeset. It is covered well near the bottom of this article http://blog.plataformatec.com.br/2015/08/working-with-ecto-associations-and-embeds/

I have recently merged a PR that improves the handling of associations. We have removed the "magic" from ex_admin and delegate that responsibility to your changeset. The readme file and tests have several examples.
If you are using has_many :through for your many-to-many associations, you should convert them to :many_to_many. This can be done without any schema changes.
Please checkout the master branch on GitHub.

Related

Adding multiple entities to one main form

I am in the process of modifying forms to account for V9 of Dynamics which is being rolled out currently.
Our environment is using Dialogs but these are being deprecated with V9 which means we have to prepare the forms to be used instead of these Dialogs.
Due to how fragmented our solution is the data is all over the place in different entities and can all be written too at different times.
Is it possible to have multiple entities/records on the same form which are all editable? This way I can run rules to hide and show as people edit specific fields.
From what I can identify, there is no "true" way to achieve this. However, if there are entities which are linked via a relationship within the solution, it is possible to add the "Party List" type field to the form and this will act as the link between the two entities. When you go to search for an existing record within the related entity, there is a new button which allows you too create a new record of that entity which directly associated with the form you were working on initially.
We do have editable subgrids. Add the subgrids of related entities & allow users to edit the related records in main record form at one shot.

In VS2013 is there an easy way to update attributes in your model?

I'm using VS2013 in a MVC 5 app. Created EF6 model using the database first approach which yielded the model as expected. Subsequently I will make changes in the database objects (tables, views, stored procs). When I go into the model to update it, the visible model will get updated. Looking at the Model Browser, I have to manually clean up the artifacts that no longer exist. Am I missing something in my procedure?
When you right click and update the model from the database, it will typically add new pieces automatically, but in my experience it doesn't automatically remove pieces that are no longer there, so you'll need to watch for those and delete them manually.
On the plus side, if you had any special customizations around a field (e.g. enum types), and the field gets renamed, this gives you a chance to compare the two configurations and make sure they line up before you delete the old field from your model.

Filtering instances not related to the current contact

I have a custom entity Stuff and it contains a lookup to Contact. I created a 1:N relation with the latter as primary and the former as related entity.
On the Contact's form I added a subgrid and I only wish to list the instances of Stuff that have the lookup pointing to the currently viewed Contact. However, when I choose Only Related Records, the custom entity isn't in the list (despite it being related via the 1:N relation).
So, I'm using All Record Types, instead, but that shows all the instances of Stuff and not, as I wish, only those that are related to the currently viewed member.
So, what's the best course of action here? Should I do something with the relation? Or build a custom filter in JavaScript? How can I make the Stuff related to Contact if it's at all possible?
The image below shows the available conditions for the related entities. I notice that none of them relates to Contact, though. Do I need to activate it somehow or am I approaching it from a totally wrong angle?
So to give you more detail, you add the grid on the form:
(This is coming from the contact Form edit, after you press add subgrid)
you select there only related records, this are the stuff record i created:
As you can see only 3 of them has a lookup pointing to a valid contact, from there if you look at the grid, is showing only the related ones as you selected during the grid creation:
In any case i update the answer this is how the relationship is defined, and is a simple lookup created from the stuff entity:

How to process 1 form across 2 controllers/models in MVC (CFWheels)?

I'm an old CFML developer, new to CF on Wheels and MVC programming in general. I'm picking it up pretty quickly, but one thing that isn't evident to me is how one can offer a form to optionally update multiple db table records (models). I'd specifically like to set up a tabbed form for User info and User Profile info, where the former is required and the latter is not. This data is stored in two different one-to-one tables. What's the setup I need in order to call two "new" or "edit" views, run 2 "create" or "update" procedures, affecting two different tables. Or am I thinking about this all wrong.
Update: Adding some more info on what I'm trying to do. To keep it simple, I'll stick to 2 tabs and 2 tables, though I'm really looking at at least 3 in this instance.
So I've got a Users table and a UserProfiles table, and I've got models named User.cfc and UserProfile.cfc that are related 1-to-1, with UserProfile dependent on User. Pretty standard stuff. For each I've got controllers: Users.cfc and UserProfiles.cfc, each of those containing actions. add, edit, create, update, doing the obvious stuff (add and edit display forms). I have partials that display the add/edit form fields for each, so that's already prepared. Now, I want to create what is effectively a single add/edit form that can update both tables at the same time. The tabs don't really matter; effectively it could all be on one page.
So conceptually I'm doing something like:
#startFormTag(action=???)#
#includePartial("form_user_add-edit")#
#includePartial("form_userprofile_add-edit")#
<button type="submit" class="btn">#operation#</button>
#endFormTag()#
Do I need to create a separate controller action that basically combines the create and update actions for two different controllers?
Thanks in advance from a pleased and eager CFWheels newbie...
Brian
If all of the data is related through hasMany or hasOne associations, I'd recommend looking at nested properties.
http://cfwheels.org/docs/1-1/chapter/nested-properties
If you're a newbie though, you may want to refrain from this until you've got something simpler worked out.
I guess you are talking about two models representing these two tables, possibly associated using hasOne. Models allow you to validate data, this makes controller much simpler. This way you could create two forms under two tabs, and keep record's primary key as hidden field. Controller could run the validation and re-display the forms (partials may help)... Hold on, I am just going through the reference.
I realize this answer is pretty generic, as well as your question. I suggest you to go ahead and try something, see how it works.
After that update your question with code samples and ask if you have some specific problems. For example, validation and displaying errors in CFWheels may be a bit tricky.

MVC3 - what is the best practice to validate the old data and new data when submitting a form

I am using mvc3 for my web app.
When an user edit an existing form, make a few changes and save it.
what is the best practice to check which field in the form has been modified.
Regards,
Bubblegum.
Since we're not talking data access - upon postback, compare it to your object in your database. This is somewhat of an open question because it depends what data access you are using and what models you are using. Entity framework Entities can track their modified state so you may be able to use
TryUpdateModel(yourInstanceLoadedFromYourDatabase)
and check the status of each field. That 'may' work, otherwise you are comparing each field. However why do you need to know specifically which field changed? Save the entire object back to the database (or simply merge with the values from the page using the same TryUpdateModel above)

Resources