CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Profiles (Moderator: Dave Greenberg) »
  • Contact reference field in registration field
Pages: [1]

Author Topic: Contact reference field in registration field  (Read 1332 times)

marc

  • Guest
Contact reference field in registration field
July 26, 2010, 05:29:00 am
Hi,

we are using a contact reference field in a civi profile that is required on registration in drupal. Since we want people only to choose from a very small subset of contacts (namely parties), we put a dropdown there instead and update the hidden value field of the contact reference with the ID of the party when it is chosen in the dropdown.

The id of the visible input field of the contact reference is custom_1, and the hidden field that I think I should update is custom_1_id. Here is the jquery for it:
Code: [Select]
<script type="text/javascript">
  $(document).ready(function () {
     $("#partydropdown").change(function() {
        $value = $("#partydropdown").val();
        $("#custom_1_id").val($value);
        $newVal = $("#custom_1_id").val();
    });
});
</script>

Even though I do this, it complains that 'Party Name is a required field' when I try to submit. Am I wrong to assume that the hidden field with id custom_1_id is the place to set the value? Or do I have to get rid of something else, maybe the jQuery that usually updates this field via autocomplete?


Marc

Hershel

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4640
  • Karma: 176
    • CiviHosting
  • CiviCRM version: Latest
  • CMS version: Mostly WordPress and Drupal
Re: Contact reference field in registration field
July 26, 2010, 05:52:45 am
I would have to see the page in question to determine the correctness of your jQuery code.

But would not a "calculated" custom field work for this instead? IE and then populate it on the back end.
CiviHosting and CiviOnline -- The CiviCRM hosting experts, since 2007

See here for the official: What to do if you think you've found a bug.

marc

  • Guest
Re: Contact reference field in registration field
August 05, 2010, 02:04:05 am
Hi,

thanks for the quick reply, I was on holiday, hence my non-reply.

What do you mean by 'calculated' custom field? Do you mean that we just have a normal drop-down and then do some filling in of the database from this drop-down to create a proper contact reference in the other field?

I'll have a look at that option and see how that goes.

It is a page on my local server only at the moment, else I'd give you the link. What part of the source do you need, I'll paste it here.

Thanks,

Marc

marc

  • Guest
Re: Contact reference field in registration field
August 05, 2010, 03:52:13 am
Hi,

here are all the bits of code that my jquery code refers to:

Code: [Select]
  <div><input name="_qf_default" type="hidden" value="Dynamic:upload" />
<input name="MAX_FILE_SIZE" type="hidden" value="2097152" />
<input name="edit[civicrm_dummy_field]" type="hidden" value="CiviCRM Dummy Field for Drupal" />
<input id="custom_1_id" name="custom_1_id" type="hidden" value="" />
<input name="contact_sub_type_hidden" type="hidden" value="Activist" />
</div>

Code: [Select]
<select id='partydropdown'><option value="0">--Select a Party--</option><option value="7272">PS, Andorra</option><option value="7273">SPÖ, Austria</option><option value="7274">PS, Belgium</option><option value="7275">Sp.a, Belgium</option></select></td></tr>

and the following is the contact reference field that already exists, with generated jQuery code:
Code: [Select]
<tr id="editrow-custom_1"><td class="label"><label for="custom_1">Party Name</label></td><td class="edit-value"><input name="custom_1" type="text" id="custom_1" class="form-text" />
<script type="text/javascript">
cj( function( ) {
    var url       = "/en/civicrm/ajax/contactlist?reset=1&context=customfield&id=1";
    var custom    = "#custom_1";
    var custom_id = "input[name=custom_1_id]";
 
    if ( !cj(custom).hasClass('ac_input') ) {
        cj(custom).autocomplete( url, { width : 250, selectFirst : false, matchContains: true
        }).result(
            function(event, data, formatted) {
                cj( custom_id ).val( data[1] );
            }
        );
    }
});
</script>

Is this enough to figure out whether my codesnippet does what I hope it should do?

Marc

marc

  • Guest
Re: Contact reference field in registration field
August 09, 2010, 07:49:02 am
Hi,

I just solved this (thanks to Xavier). The only missing bit was to put any text in the input field with id custom_1. The logic behind this:

When submitting the form, the first check is to see whether there is any text (whatever text it seems) in the visible field of the reference field (the one with the looking glass). If there is text, the value that is in the hidden field (in this case with id ='custom_1_id') gets submitted.

Therefore I had to update the hidden field with the correct value and the visible field with any value and voilà! How obvious...

Here my code:

Code: [Select]
<script type="text/javascript">
  $(document).ready(function () {
     $("#partydropdown").change(function() {
        $value = $("#partydropdown").val();
        $("#custom_1_id").val($value); // this has to be the right id
        $("#custom_1").val($value); // could be any value instead of $value, as long as it is not an empty string
    });
});
</script>


Marc

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Profiles (Moderator: Dave Greenberg) »
  • Contact reference field in registration field

This forum was archived on 2017-11-26.