1. krnsgrffn
  2. Support
  3. Wednesday, February 05 2020, 08:56 PM
Hi-

I need to check to see if at least one phone number is provided before a user is either created or updated, and if not, throw an error. I enabled the skeleton plugin, but can't figure out how to do it. Here is my code:


defined('_JEXEC') or die;

class PlgJsnSkeleton extends JPlugin
{

public function triggerProfileUpdate($user,&$data,$changed,$isNew){

if ($data['homephone'] == '' && $data['cellphone'] == ''){
JFactory::getApplication()->enqueueMessage('You need at least one phone number.', 'Error');
}
else {
$user->save();
}

}

}


However, when I save a user with no phone numbers, I get no error message and the user is saved. I am sure the fix is simple, I just don't know what it is.

Thanks.
admin Accepted Answer
Admin
Hi,
unfortunately this is not so simple, because the function "triggerProfileUpdate" is triggered after the validation process.

You can use this workaround:
- Create a Field of type Text and set it as required called "Home Phone or Cell Phone" (for example with alias phones)
- In your template options add following custom CSS to hide the field
.phones-group{display: none !important}

- In your template options add following custom Javascript code to fill the phones field
jQuery(document).ready(function($){
$('#jform_homephone,#jform_cellphone').change(function(){
$('#jform_phones').val($('#jform_homephone').val()+$('#jform_cellphone').val());
});
});


Basically the value of field "phones" will be populated with homephone and cellphone values, if both field are empty then the form will return a error like "Invalid field: Home Phone or Cell Phone"
  1. more than a month ago
  2. Support
  3. # 1
krnsgrffn Accepted Answer
Pro
Got it. Thanks for the solution.
  1. more than a month ago
  2. Support
  3. # 2
  • Page :
  • 1


There are no replies made for this post yet.
However, you are not allowed to reply to this post.