Forums

Autocompleter w/required field - no message when left empty

Nicole Rosset 25 Nov, 2015
Hello,

I modified the demo-Autocompleter to make the country field required. I alse removed the debugger On Submit.

When I leave the country field empty, I'm not able to submit the form (which is fine), but no error message appears (and I have asked for one).

How can I have the form display an error message asking to fill the country field ?

Thank you very much in advance
Nicole Rosset
GreyHead 26 Nov, 2015
Hi Nicole,

I have looked at this and have succeeded in writing a Custom validation that stops the form submitting - but so far have failed to display any useful message. I'll look again tomorrow.

The autocompleter works by hiding the original input and replacing it so the validation message is hidden along with the original input.

Bob
Nicole Rosset 26 Nov, 2015
Hi Bob,

Thank you very much for having looked at my question.

If you find a good solution, I'd be very grateful !

Take care
Nicole
GreyHead 27 Nov, 2015
HI Nicole,

I've found way of getting this to work using a different autocompleter :-(

There's a little jQuery autoComplete library here

You need to download it and copy the jquery.auto-complete.min.js and jquery.auto-complete.css files into a new folder at /components/com_chronoforms5/extras/jq_autocomplete/ (you can use another location and change the code below to match.

The Ajax event in your form needs to return an array of strings in this case. I was able to modify the CF example code like this:
<?php
$countries = array("Afghanistan", "Albania", . . ."Zambia", "Zimbabwe");

$json = array();
if ( empty($form->data['q']) ) {
  echo json_encode($json);
  return;
}
foreach ( $countries as $c ) {
  if ( stripos($c, $form->data['q']) === false ){
    continue;
  }
  $json[] = $c;
}
echo json_encode($json);


And in a Load JavaScript action add code like this
<?php
$jdoc = JFactory::getDocument();
$jdoc->addScript(\JURI::root().'/components/com_chronoforms5/extras/jq_autocomplete/jquery.auto-complete.js');
$jdoc->addStyleSheet(\JURI::root().'/components/com_chronoforms5/extras/jq_autocomplete/jquery.auto-complete.css');
?>
jQuery(document).ready(function(jQ) {
  jQ('#text1').autoComplete({
    minChars: 3,
    delay: 150,
    source: function(term, response){
      jQ.getJSON('/index.php?option=com_chronoforms5&chronoform=test_jq_autocompleter&event=ajax&tvout=ajax',
        { q: term },
        function(data){ response(data); }
      );
    }
  });
});
The first part in <?php ?> tags it just to load the new files.
In the second part you need to edit the form name and Ajax event to match yours.

Because this library leaves the original input as it was the required validation still works as usual.

Bob
Nicole Rosset 27 Nov, 2015
Hi Bob,

THANK YOU SO VERY MUCH !

I have tried to do everything you said, but it doesn't work so far. No autocompleter shows up when I'm trying to fill the country field.

Do I need to leave the "old" Autocompleter command in the On Load section of the form ?

Where do I need to put the Load Javascript code ? In the On Load section of the form ?

Sorry to keep bothering you
Nicole
Nicole Rosset 28 Nov, 2015
Hi Bob,

I worked on it again.

I think I have a problem with the Ajax event part. Is this "countries_list" in the demo-autocompleter ?
If yes, where do I have to put it in the command :
"jQ.getJSON('/index.php?option=com_chronoforms5&chronoform=test_jq_autocompleter&event=ajax&tvout=ajax"

Thank you very much in advance
Nicole
GreyHead 28 Nov, 2015
Hi Nicole,

The JavaScript goes into a Load JavaScript action in the form On Load event before the HTML (Render form) action.

You can remove the old AutoCompleter as this is a replacement (if that is still there and linked to the same element it may be the cause of the problem).

I included the countries list from the demo-autocompleter just because that was simple to use for a test. You can see my test form here.

The code in the Ajax event in your form - which is where the countries list is - may need to be tweaked to return a plain array of strings. What do you have in there now?

By all means email or PM me the site URL, the form name, and a SuperAdmin login and I'll take a quick look.

Bob
Nicole Rosset 28 Nov, 2015
Hi Bob,

Thank you SO very much.

When I test your form, there is no autocompleter, no drop down list. I tried it on Firefox, Chrome and IE (Windows 8 and 10), but to no avail.

I'll send you the access code by PM.

Thanks once again
Nicole
GreyHead 28 Nov, 2015
Hi Nicole,

I checked in Chrome, Edge and FireFox and the test form is working. It is set up to require 3 characters and with a 150ms delay so that it keeps the number of Ajax calls down. Also it does nothing if no result is found.

Bob
Nicole Rosset 28 Nov, 2015
Hi Bob,

YES ! You're right ! I was expecting a drop down list of some kind et didn't wait long enough.

Sorry !
Nicole
Nicole Rosset 28 Nov, 2015
Hi Bob,

So now my form is working the same way as yours. Great !

The official demo-autocompleter displays a drop down list and doesn't allow you to choose a country that's not in the list. Which is very helpful. But your form doesn't do that.

Maybe it's the only way to get an autocompleter that displays an error message when the field is left empty ...

Thank you once again for your help ! I really appreciate it !

Nicole
GreyHead 28 Nov, 2015
Hi Nicole,

Hmm . . . I think it's possible to add some code to do something if no values are returned. What would be helpful behaviour?

Bob
Nicole Rosset 28 Nov, 2015
Hi Bob,

Thank you for your perseverance !

If no result is returned by the autocompleter, it means that this is no valid country and this should not be accepted.

So, yes ! Anything that will prevent the user from choosing a non existing country would be great !

Thank you once more
Nicole
GreyHead 06 Dec, 2015
Answer
1 Likes
Hi Nicole,

After a lot of experimentation I think I have this working - this time using the jQuery UI autocompleter. It ends up being not too hard to do. The problem is that the jQuery UI autocompleter has a lot of options and - to me at least - the documentation is sometimes less than helpful.

You can see my test form here - it's running off the same Countries list as before.

+ Make sure that the jQuery UI library is being loaded on your site. I use the JQueryEasy extension, you can also require it from Joomla! - see the Joomla! Docs here if it isn't loading on your site.

+ In the ChronoForms v5 Designer tab add a standard text input and set the validation to Required.

+ In the Setup tab add a Load JavaScript action with code like this:
jQuery(document).ready(function(jQ) {
  jQ('#text1').autocomplete({
      source: function( request, response ) {
        jQ.ajax({
          url: '/index.php?option=com_chronoforms5&chronoform=test_jqui_autocompleter&event=ajax&tvout=ajax',
          dataType: 'json',
          data: {
            q: request.term
          },
          success: function( data ) {
            response( data );
          }
        });
      },
      minLength: 3,
      select: function( event, ui ) {
        // add any On Select code
      },
      open: function() {
        jQ(this).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
      },
      close: function() {
        jQ(this).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
      },
      change: function( event, ui ) {
        if ( ui.item == null ) {
          jQ(this).val('');
          jQ(this).focus();
        }
      }
    });
});
Replace text1 with the ID of the text input and edit the URL to suit the form event you are calling.

+ Edit the Code in the Ajax event to return an arrays of values like this array('value' => 'aaa', 'id' => 'zzz'); Here's the code I used to test:
<?php
$countries = array("Afghanistan", . . . "Zimbabwe");

$json = array();
if ( empty($form->data['q']) ) {
  echo json_encode($json);
  return;
}
foreach ( $countries as $c ) {
  if ( stripos($c, $form->data['q']) === false ){
    continue;
  }
  $json[] = array('value' => $c, 'id' => $c);
}
echo json_encode($json);
?>


If you add a value from the dropdown list then edit it when the focus shifts away from the box the value will be cleared and the cursor moved back into the input.

Bob

Note: there are many more options for this AutoCompleter. See the docs here and examples here. The 'required' code came from this StackOverFlow answer.
Nicole Rosset 08 Dec, 2015
Hi Bob,

THANK YOU EVER SOOOOOOOOOOOOO MUCH !

You're awesome !!!

It's great ! It's exactly what I was looking for ! Thank you very much !

Sorry for not having answered sooner, I had an exam to grade ...

Take care and thank you once again
Nicole
This topic is locked and no more replies can be posted.