Posted on by

Showing a Record Edit Form Based on a Value in the Record

When using Participants Database to represent two or more classes of people or things, you sometimes need to customize the frontend edit form so that is only shows the fields that are relevant to the type of record that is getting edited. We can easily do this with a custom template.

If you’re not familiar with the use of custom templates, read this article: Using Participants Database Custom Templates

For our example, we are continuing the example used in “Multiple Databases” with Participants Database where you have a kid’s sports team and you have records for the players and the parents. We want to show a different record edit form for each of the two classes of people.

When someone wants to edit their record, they will typically use either a “private link” or a login page provided by the Particpant Login add-on. The way this works, when they go to their edit record page, we use a custom template to determine what kind of record edit form to show, and then the template invokes the record edit form with the desired parameters. It’s a template that uses shortcodes, as you will see.

As in the example, you have your fields organized into three groups: “contact”, “player” and “parent”. The “contact” group is used for all records and the other two are specific to the type of person. There is a field named “type” that tells us which type the record is for, and we use that to determine which set of field groups to show.

Here is a custom template that does that, it is named pdb-record-usertype.php

To use the template, use this shortcode on your record edit page:

[pdb_record template=usertype]

That’s it! Of course, you’ll need to customize this to your particular situation, but that is the general technique.

5 thoughts on “Showing a Record Edit Form Based on a Value in the Record

  1. Thank you for your answer, I did not see it immediately.

    I picked up a similar code from your repository but used an if loop. In effect, we used $this->participant_valuesin this code and it worked. I adapted your code for my case.

    Thanks again,
    Best regards

  2. Hello Roland,

    Thank you for your prompt response.
    I modified the shortcode in my previous code and followed your advice. It still does not work.

    I did some tests:

    – I changed the variable $type by something else thinking it could be already used elsewhere.
    – I use another participant_record
    – I add a step to the switch (case”) to know if my variable $typetestvar was filled with the record participant I want.

    Now I can see that the problem is at $typetestvar = $this-> participant_record [‘type1’];
    because it is empty and show the last message don’t work.

    Do you have an idea ?
    Thanks in advance and have a nice weekend,
    Best regards,

    last edit: https://gist.github.com/Donma28/be6b9addfae7a141f2211acc9283be31

    1. The property you want to use is $this->participant_values

  3. Hello Mr Roland,

    I have a small problem with this code. Could you help me please?

    https://gist.github.com/Donma28/be6b9addfae7a141f2211acc9283be31

    I took example on your tutorial above and I just modified the values of the shortcode as well as added an additional box

    When I use the shortcode [pdb_record tabs = "true" template = usertype] I have nothing that appears on my page. Do you have an idea ?

    I take this opportunity to ask another question
    Is it possible to adapt this code for the shortcode [pdb_single] so that the right groups are displayed according to the record and not the user who looks at the record.

    I hope I have been clear, my English is not very good please excuse me.

    Thank you very much in advance for any help you can give me.
    Regards

    1. First, if you are using a custom template, you should not use the “tabs=true” attribute in the shortcode. Also, don’t put spaces around the =. So your shortcode should be:

      [pdb_record template=usertype]

      Now, your template will be used to show the record.

      Setting up a similar custom template for the pdb_single shortcode is the same, you just need to use the correct name for your custom template, then change all the shortcodes in the custom template to be pdb_single.

Leave a Reply
You have to agree to the comment policy.

5 thoughts on “Showing a Record Edit Form Based on a Value in the Record

  1. Thank you for your answer, I did not see it immediately.

    I picked up a similar code from your repository but used an if loop. In effect, we used $this->participant_valuesin this code and it worked. I adapted your code for my case.

    Thanks again,
    Best regards

  2. Hello Roland,

    Thank you for your prompt response.
    I modified the shortcode in my previous code and followed your advice. It still does not work.

    I did some tests:

    – I changed the variable $type by something else thinking it could be already used elsewhere.
    – I use another participant_record
    – I add a step to the switch (case”) to know if my variable $typetestvar was filled with the record participant I want.

    Now I can see that the problem is at $typetestvar = $this-> participant_record [‘type1’];
    because it is empty and show the last message don’t work.

    Do you have an idea ?
    Thanks in advance and have a nice weekend,
    Best regards,

    last edit: https://gist.github.com/Donma28/be6b9addfae7a141f2211acc9283be31

    1. The property you want to use is $this->participant_values

  3. Hello Mr Roland,

    I have a small problem with this code. Could you help me please?

    https://gist.github.com/Donma28/be6b9addfae7a141f2211acc9283be31

    I took example on your tutorial above and I just modified the values of the shortcode as well as added an additional box

    When I use the shortcode [pdb_record tabs = "true" template = usertype] I have nothing that appears on my page. Do you have an idea ?

    I take this opportunity to ask another question
    Is it possible to adapt this code for the shortcode [pdb_single] so that the right groups are displayed according to the record and not the user who looks at the record.

    I hope I have been clear, my English is not very good please excuse me.

    Thank you very much in advance for any help you can give me.
    Regards

    1. First, if you are using a custom template, you should not use the “tabs=true” attribute in the shortcode. Also, don’t put spaces around the =. So your shortcode should be:

      [pdb_record template=usertype]

      Now, your template will be used to show the record.

      Setting up a similar custom template for the pdb_single shortcode is the same, you just need to use the correct name for your custom template, then change all the shortcodes in the custom template to be pdb_single.

Leave a Reply
You have to agree to the comment policy.