Skip Navigation

[Resolved] Create Form Fields Dynamically Based On Term Count

This support ticket is created 3 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 19 replies, has 3 voices.

Last updated by Christian Cox 3 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#1905739

I have a post type named project submission that has a taxonomy named skills-category. In this taxonomy users can add up to 3 skill terms per post. I am another CPT named expert review that is in a O2O relationship with project submissions.

The experts will review the project when it is submitted by a user. The expert will review each skill tagged in the project. The skills can be rated on a 5 point scale so I was wondering there is a way to dynamically add fields to the expert review form based on the number of terms attached to a parent term (project submission).
Expected behavior:
1) The expert would click on a review project button on the project submission and is directed to a form to enter the expert review
2) The system finds the skills tagged on the project submission and adds separate 5 point rating scale for each skill. For example, if user select just two skills, then only two 5 point scales are added. If you user select 3 skills, then 3 5 point scales are added.
3) The expert rates the user for each skill on a 5 point scale.
4) The two post types are linked by a relationship field to make sure there is an association
5) The expert submits the form and rating for each skill recorded.

It seems like this might require a loop in the CRED form with generic fields but I am not if that is possible.

#1906879

Hello, in theory it is possible, though not advised, to create a nested View structure of taxonomy terms from the parent post and use the nested View's loop to output generic fields. You would need one View of the parent post type, filtered by post ID. If you pass the parent post's ID into the page containing the Form using a URL parameter, the View could use a post ID filter set by a URL parameter. Then inside that View's loop, you could place another View of taxonomy terms to loop over the parent post's taxonomy terms. However, it is not generally advised to use Views like this in a Form. Views are intended to be used to display data, not to generate field scaffolding for a Form. If you decide to implement a nested View structure like this to output generic fields, the question becomes how to store that data from the generic fields. Are there custom rating fields implemented on the child post type? If so, it seems that you would have to set up custom fields to store the rating for each individual term:
rating-term-a-slug
rating-term-b-slug
rating-term-c-slug
...and so on. That would require you to manage these custom fields manually in the child post type, which seems impractical. You would have to add a new custom field each time a new term is added to the taxonomy. So it seems that the overall structure here is a bit convoluted, and could fail easily when things are changed in the system. It would be difficult to edit an existing review with Forms. For example, what if the terms applied to the parent post change in the future after the Review is first created? Should you display ratings for the terms that are no longer applied to the parent post? Should you delete those programmatically when the Review is edited? Very quickly it becomes a complex problem to solve.

#1908439

Chris,

Thank you the insightful answer. I tried to figure this out with a few different angles that do not create an admin mess once something like this live. A few thoughts
1) Every skill has 5 attributes
2) Every attribute can be measured on a 5 point scale
3) I thought of creating custom fields within the skills taxonomy but then I realized custom fields in a taxonomy 'associate' with the taxonomy term not with a post type. So I won't be able to link a expert review (or project submission) with a taxonomy custom field. Please let me know if this is accurate.
4) I thought of creating a custom post type called rubric that would have a skills-category taxonomy and 5 radio button custom fields. The radio buttons serve the role of attributes and could be associated with each expert review via a relationship.
5) However, (4) has limitations. First, the attributes are different for each skill so I cannot have a base list of just 5 custom fields to 'represent' all skills. I will have to create separate custom post type for each skill with it's own custom attribute fields. Second, I will have to submit four forms to rate a review - one expert review form and 3 skill CPT forms. This seems to be very complex and frankly a bad design.
6) Then I started thinking if there is a way to create a JSON from the frontend that contains the skill name, the skill attributes and 5 point scale. For example, if I have a skill named market research and it has 5 attributes. The JSON would be -
{ "skill":
{"name": "market research", "attributes":
[{"attribute-name":"market research-attribute-1", "scaledescription":
{"rating-1":"desc1", "rating-2":"desc2", "rating-3":"desc3", "rating-4":"desc4", "rating-5":"desc5"}
},
{"attribute-name":"market research-attribute-2", "scaledescription":
{"rating-1":"desc1", "rating-2":"desc2", "rating-3":"desc3", "rating-4":"desc4", "rating-5":"desc5"}
},
{"attribute-name":"market research-attribute-3", "scaledescription":
{"rating-1":"desc1", "rating-2":"desc2", "rating-3":"desc3", "rating-4":"desc4", "rating-5":"desc5"}
},
{"attribute-name":"market research-attribute-4", "scaledescription":
{"rating-1":"desc1", "rating-2":"desc2", "rating-3":"desc3", "rating-4":"desc4", "rating-5":"desc5"}
},
{"attribute-name":"market research-attribute-5", "scaledescription":
{"rating-1":"desc1", "rating-2":"desc2", "rating-3":"desc3", "rating-4":"desc4", "rating-5":"desc5"}
}]
}
}

I could then use the JSON to create a custom table and link it to an expert review post type, which will be linked to project submission post type.

Questions for you:
1) Is my logic sound? Or am missing something?
2) Is there an easier way to do this then just use the JSON as I described above?
3) If (2) is a no, how can I create a JSON using existing toolset functionality. May be there is a way to structure a custom post type that allows me to enter the information in the JSON.

I understand this is a fairly complex setup so I am open to doing a large amount of custom work. Simultaneously, I am trying to minimize the custom work by using the strengths and flexibility of toolset.

Thanks a lot man!

#1908465

3) I thought of creating custom fields within the skills taxonomy but then I realized custom fields in a taxonomy 'associate' with the taxonomy term not with a post type. So I won't be able to link a expert review (or project submission) with a taxonomy custom field. Please let me know if this is accurate.
Yes, this is correct. In a Many-to-Many (M2M) post relationship, you can have custom fields that apply to the relationship itself (an intermediary post type). However, there is no such concept of custom fields applied to the relationship between a post and a taxonomy term. The term custom fields apply directly to the term itself, not to the relationship between the term and some post.

4) I thought of creating a custom post type called rubric that would have a skills-category taxonomy and 5 radio button custom fields. The radio buttons serve the role of attributes and could be associated with each expert review via a relationship.
I don't understand this part, could you show me a graphic or something that explains how the radios would be set up? What do you mean radio buttons are associated with a post via a relationship, could you explain?

6) Then I started thinking if there is a way to create a JSON from the frontend that contains the skill name, the skill attributes and 5 point scale.
Are you talking about generating JSON with Forms somehow, or using Forms to help automatically write JSON code? I don't really understand how you would expect this to work. Is it a text editor of some kind, or what?

#1908471
writing skill rubric.jpg

I have attached a rubric image that will explain what I mean by skill, attributes and radio buttons.

The attached rubric is for testing writing skills. The performance criteria i.e. rows are equivalent to attributes for a skill. In this rubric, the attributes are: handwriting, capitalization, punctuation, spellings, grammar.

Each attribute is graded on a 4 point scale ( I plan to use a 5 point scale to make it even more specific) and the description of each 'point' in a scale varies based on the attribute. That is, description of what satisfactory 'looks like' varies between handwriting attribute and capitalization attribute. These descriptions help the expert to evaluate a project, which means it is very important to have them shown in boxes as in the image. The expert should be able to click on the box (or a radio button) to select the field. The expert has to select one field for each attribute ( that is why a radio button makes sense) and once they are done, we will use the selections to calculate skill competency.

I don't understand this part, could you show me a graphic or something that explains how the radios would be set up? What do you mean radio buttons are associated with a post via a relationship, could you explain?
I hope the image and above description helps. The radio buttons would be custom fields in a post type called 'rubric for writing skills'. The CPT would also contain the taxonomy skills-category. Then, I can form a M2M relationship between 'rubric for writing skills' and 'expert review' CPT. I will only 'call' the 'rubric from writing skills' when the term name in 'expert review' is same as the default term name in 'rubric from writing skills'. This is a convoluted and perhaps a very inefficient way to saying that this approach will require me to create a CPT for each skill and even then matching with 'expert review' post type will be hard and confusing.

Are you talking about generating JSON with Forms somehow, or using Forms to help automatically write JSON code? I don't really understand how you would expect this to work. Is it a text editor of some kind, or what?
My goal is to store the data in the database in the form of JSON so that I can retrieve the data to create a custom table as shown in the attached image. I want to use JSON because I think it is most reasonable and structured way to store data as per my use case. If there are other ways to achieve this, I am all ears.

#1908549

I did more digging on this and it seems like I can easily convert a array into a JSON using standard php functions. However, one of my main questions is how can I leverage CRED forms to enter the following data about a skill:

Skill Name
-Attribute A
- Score 1 description and score 1 value
- Score 2 description and score 2 value
- Score 3 description and score 3 value
- Score 4 description and score 4 value
- Score 5 description and score 5 value
-Attribute B
- Score 1 description and score 1 value
- Score 2 description and score 2 value
- Score 3 description and score 3 value
- Score 4 description and score 4 value
- Score 5 description and score 5 value
-Attribute C
- Score 1 description and score 1 value
- Score 2 description and score 2 value
- Score 3 description and score 3 value
- Score 4 description and score 4 value
- Score 5 description and score 5 value
-Attribute D
- Score 1 description and score 1 value
- Score 2 description and score 2 value
- Score 3 description and score 3 value
- Score 4 description and score 4 value
- Score 5 description and score 5 value
-Attribute E
- Score 1 description and score 1 value
- Score 2 description and score 2 value
- Score 3 description and score 3 value
- Score 4 description and score 4 value
- Score 5 description and score 5 value

Once this data is stored, I can call it to create a table shown in the attached image for a particular skill. I can then selectively populate that table based on the skills tagged on project-submission CPT and enable experts to rate each skill that should be reviewed (and not all the skills).

#1909855

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Himanshu,

Christian is currently on holiday today, however he will be back tomorrow to continue assisting with this one.

Thanks,
Shane

#1910083

Thanks for the update.
I did some more thinking and wanted to share -
1) Every attribute will have a 5 point scale. This scale be equivalent to a radio buttons or select field i.e. there is a descriptions and a value for each selection.
2) Every attribute is attached to at least one skill. For example, writing and speaking are two different skills but both can have same attribute ' structure and clarity of thought'.
3) May be we can achieve (2) by tagging skills from skills-category.
4) Based on the skills attached in the parent post, I need to render attributes in the CRED form for child post as select or radio button. Parent post id will be passed using urlparam.

#1910793

Okay I'm a bit lost here, so let me restate what you need and find out if I understand correctly.
1. User submits Form to create a Submission post, and assigns 3 Skills to the Submission. For the sake of this discussion, we will assume Skill is a CPT and Submission is a CPT. Therefore, you need a Form that creates Submission posts and programmatically assigns an M2M relationship between Submission (1) and the selected Skills (3). We will assume there is an intermediary post type here, and the intermediary post type includes one custom "select" field where the reviewer can choose a rating of 1-5.
2. While the reviewer is rating each Skill, you want to display a rubric that explains the different skill ratings so the reviewer can better understand how to rate each Skill in the review.
3. In the rubric, each "row" represents a Skill and each column represents the rating 1-5. There should be a text description in each grid of the rubric, which we will call an "attribute".

If that is correct, it seems like you would just use a series of 5 custom fields in each Skill post: rating 1, rating 2, rating 3, rating 4, rating 5, for example, and include the text of the attribute. Then you would use a View of the Skills related to each Submission to loop over those skills and display the 5 custom field values in a table. That would produce the desired rubric to display during the Review.

So I'm not sure I understand the need for JSON, or why it's important to capture that JSON in a Form. Perhaps I have misunderstood what you want to achieve?

#1910975

Chris,
I think we are close to a mutual understanding of the problem. Let me try to explain once again clearly and answer your questions.
Expected Behavior
1) A user opens a post submission form to submit a project using a CPT named Project Submissions.
2) The Project Submission CPT has a skills-category taxonomy
3) When user submits the form, she selects up to 3 skill terms from the taxonomy
4) User publishes the project submission post. The post contains project name, details about the project and 3 skill tags.
5) Now, the user wants to get feedback on the project from experts in their field of work.
6) The user creates an expert review request (another CPT) which is in a O2O relationship with the project submission CPT.
7) In the expert review request post type, the user enters name of the project-submission post in the post relationship select field. If use is coming from the project submission post directly, then url parameter is used to pass default value.
8) User then enters an anonymized link of the project in a custom url field and submits the form
9) When the form submits, I have custom code that populates taxonomy terms from parent post (project submission) to child post (expert-review-request). This code works flawlessly because the expert review request CPT also has the skills-category taxonomy linked to it.
10) Now the expert review request has been posted and you can image many people would submit expert review requests.
11) All the expert review requests are visible on the expert dashboard. Now the expert has to select an expert review request and start reviewing.
12) To record a review, I have another CPT named "expert review". This CPT also a skills- category taxonomy linked to it. "Expert Review" CPT is in a O2O relationship with "Expert Review Request" CPT.
13) Every expert review request post has a button saying "review project". The button has the following url: hidden link
14) This button takes the user to the submit expert review page and this where I want to render a form that dynamically shows rubric fields based on the skills tagged in the expert review request. As I mentioned earlier, expert review request post has been populated with skill terms from the project submission post type so we can forget about project submissions from here on. We just need to work with expert review request and expert reviews.
15) Every skill , which is represented by taxonomy terms, needs to have 5 attributes. For example, the skill named "writing skill" has 5 attributes named "handwriting", "capitalization", "punctuations", "spellings" and "grammar".
16) Each of these 5 attributes needs to rated on a 5 point scale.
17) Now let's take it from bullet (13), you are an expert and your expert dashboard shows all the expert review request submitted by the user as cards. You click on a card and are taken to the expert review request post that has 3 skill tags - Skill A, Skill B and Skill C. On the post, you click on the "review project" button and are taken to a form that shows the following: a) post relationship link that has already been populated with expert review request id using url param, b) post title, c) rubric for Skill A, d) rubric for skill B, e) rubric for skill C, f) submit button.
18) As you are on the form, let us dive into what a single rubric looks like. The single skill rubric contains 5 select field (or radio fields) ,one for each attribute.
19) Let us assume Skill A is "writing skills". It has 5 attributes: "handwriting", "capitalization", "punctuations", "spellings" and "grammar"
20) As an expert, you will see 5 select (or radio button) fields titled "handwriting", "capitalization", "punctuations", "spellings" and "grammar". For each select (or radio button field) , the expert has choose the value on a 5 point scale. For example, you may give "handwriting" a 3 and "capitalization" a 4.
21) Once you rate each attribute on a 5 point scale for one skill, you repeat the process for remaining 2 skills. Essentially, you rate 15 attributes (3 skills*5 attributes per skill) on a 5 point scale.
22) Once you are done, you submit the form and the expert review CPT is published and values are stored for every attribute for every skill.

Let's stop here and see if you have any questions.

Note: you are right. We do not need to think of JSON here. My apologies for not making that clear.

#1911245

Okay great, yes this all makes sense and is very close to what I understood.
- How do you plan to manage attributes? Will each attribute be a post in some custom post type?
- How do you plan to manage which Attributes are connected to each Skill? Since Skills are taxonomy terms, it seems the obvious way would be to have an Attributes post type and assign the related skill term to each Attribute post. Otherwise, you might need to create a custom post type Skill that corresponds to the Skill taxonomy. Each Skill taxonomy term would have one corresponding post, ideally using the same slug. Or maybe you intend something else, like storing a JSON data string in a custom field on the Skill term, holding JSON data that describes the 5 attributes?
- How do you plan to save the ratings for each Attribute per Skill in an Expert Review? Is each rating stored in a custom field somewhere, or are you planning to store JSON string data holding all the rating values for all attributes somewhere in the Expert Review post?

#1911297

I want to use a custom post type named Skill Attributes with the following fields:
Field 1 - Post Title or Attribute Name
Field 2 - Skill- category taxonomy (could be multiple skills)
Field 3 - Level 1 rating description for the attribute
Field 4 - Level 2 rating description for the attribute
Field 5 - Level 3 rating description for the attribute
Field 6 - Level 4 rating description for the attribute
Field 7 - Level 5 rating description for the attribute

This way I can add attributes posts with all the information I need on the form.

However, I am struggling with your last question. I am not sure how to call attributes in the Expert review post form for each skill and 'register' the entries for the expert review post. Because, the real value is in the rating given by expert to each attribute per skill. The value behind each rating will determine the 'skill score' per skill from the expert review.

I could create a custom radio button for each attribute but how would I dynamically populate the radio button title and rating description in the custom field based on the skill tags.

In other words, could there be a loop that checks for skills and then checks for attributes per skill. Then, the loop populates custom radio button fields with content from the attribute. This keeps on happening until all attributes have been 'inserted' in the custom radio button fields. For example, each skill has 5 attributes so in total we need 15 custom radio button fields that get populated via a loop. This seems cumbersome and rigid.

I guess the question is how do I enter the rating for attributes in the expert review post form.

What do you suggest?

#1912137

The best way I can think of to achieve this Form design is to use a nested View structure and the legacy View editor. You need at least the following components:
- A View that loops over the 3 Skills terms associated with the current Expert Review
- In the loop of that View, you need a nested View that loops over the 5 related Attributes posts (a View filtered by taxonomy term, where the term is set by a shortcode attribute or set by the parent taxonomy View).
- In the loop of the inner nested View, you need to output a generic "select" field that includes the option values 1, 2, 3, 4, and 5:

[cred_generic_field type='select' field='rating-writing-handwriting']
{
"required":0,
"default":[],
"options":[{"value":"1","label":"1"},{"value":"2","label":"2"},{"value":"3","label":"3"},{"value":"4","label":"4"},{"value":"5","label":"5"}]
}
[/cred_generic_field]

- You can use the Skill term slug and Attribute post slug to generate a dynamic field slug in some standard format, something like rating-{skill}-{attribute}. So if the skill is Writing and the attribute is Handwriting, the dynamic field slug might be:

rating-writing-handwriting

The slug of the Attribute is easy to determine inside the loop of a View of Attributes, it's the wpv-post-slug shortcode:

[wpv-post-slug]

https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#vf-153368

The slug of the skill term is not so easy to determine inside the loop of a View of Attributes. I would probably pass the current term slug from the loop of the View of Skills terms into the View of Attributes using a shortcode attribute like currentslug, then use wpv-attribute to access that skill term slug inside the nested View. Here's how you pass the current skill term slug into the nested View of attributes. In the loop of the View of Skills, include the wpv-taxonomy-slug shortcode and currentslug shortcode attribute in the View shortcode:

[wpv-view name="Your Nested View of Attributes" currentslug="[wpv-taxonomy-slug]"]

https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-taxonomy-slug
https://toolset.com/documentation/user-guides/views/passing-arguments-to-views/

Then inside the loop of the View of Attributes, you can access the currentslug value using wpv-attribute:

[wpv-attribute name="currentslug"]

https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-attribute

Putting the two slugs together to form the dynamic generic field slugs:

[cred_generic_field type='select' field='rating-[wpv-attribute name="currentslug"]-[wpv-post-slug]']

That structure would produce the required generic inputs for front-end data capture. To save the selected values from these generic fields, you'll need to use the Forms API or some other custom code hooks to manage that data. The cred_save_data hook, for example, can be used to access those selected values from the $_POST superglobal. Example follows:

add_action('cred_save_data', 'tssupp_save_expert_review',10,2);
function tssupp_save_expert_review($post_id, $form_data) {
  $forms = array( 123, 456 );
  if ( in_array( $form_data['id'], $forms ) )
  {
    // get the selected value for the rating-writing-handwriting generic field
    $rating_writing_handwriting = $_POST['rating-writing-handwriting'];
    // do something with that value
    // ...
  }
}
#1912387

Chris,

Thanks for this amazing explanation. I am working through this now and will let me know how it goes.

As I build this, I was wondering if there is any restriction in using a radio button field instead of a select field. Both seem to have a similar structure. Am I correct?

#1912461

As far as generic radio input vs. generic select field technical restrictions...no, as far as I know there are no significant technical restrictions that might prevent you from using a radio button instead of a select field to capture the data. Either approach could work from a technical perspective. You might consider which input type is easier to use on the front-end.