How to try-catch and display errors with PHP validation module?

fujitsuDev

New Member
Hi,

I get always a white page without any error/debug information when submit form for which I apply a 'replace' PHP validation. So I can't identify issue without this.

I had try this try-catch way but nothing happens...
https://joomla.stackexchange.com/questions/223/whats-the-correct-way-for-exception-handling

Example:

Condition:
Code:
return isset(JFactory::getApplication()->input->get('select_value_to_check', 0, 'int'));

PHP code:
Code:
try
{
  if (JFactory::getApplication()->input->get('select_value_to_check', 0, 'int') == "1") {
    return 1;
  } else {
    return 0;
  }
}
catch (Exception $e)
{
  // How to catch and display error?
}


Thanks for help!
 
White page: did you set Joomla's error reporting to max?

Not sure what you want to catch in your example.
 
Yes, on max:
ff7064f9f03787c94147e28830fd1124.jpg


This is a phony example that I wanted simplified from the original but it's not a big success indeed, yes... :confused:
With my validation I want to force the replacement of the value / option of a select field of the submitted form.
This field must be automatically defined according to the group of the connected user.
 
Your problem is almost certainly the condition. I'm pretty sure you can only call isset() on variables, not the return values from functions:

https://www.php.net/isset

And isset() usually wouldn't be the right thing to call anyway, you'd want to specifically check for an empty string or 0 with empty() ...

Code:
$myThing = JFactory::getApplication()->input->get('select_value_to_check', 0, 'int');
// return false if empty string or 0 or boolean false
return !empty($myThing);

But to report errors, the safest way is usually through J!'s system messages ...

Code:
JFactory::getApplication()->enqueueMessage('Something bad happened!  Yikes!');

If you need to report the error from an exception ...

Code:
JFactory::getApplication()->enqueueMessage('Something bad happened!  Yikes! Exception error:' . $e->getMessage());

-- hugh
 
Thanks! That's works.

---

My example for others:

Please considere tooltip indication:
The PHP code to run. This element's value is in $data. Other element values can be found in $_REQUEST (use JFactory::getApplication()->input->get() to access). Return either true/false (if 'match' selected below) or the replacement string (if 'replace' selected below).

"Status" form field (select):

HTML:
Code:
<select name="fabrik_form___status[]" id="fabrik_form___status" class="fabrikinput form-control inputbox input" data-kpxc-id="abcde">
  <option value="0">Online</option>
  <option value="1">Waiting validation</option>
</select>


PHP validation applied on this field:

Condition:
Code:
$userType = JFactory::getApplication()->input->get('fabrik_form__user_type_id', 0, 'int');
// Return false if empty string or 0 or boolean false
return !empty($userType);


PHP code (Replace mode activated):
Code:
$userType = JFactory::getApplication()->input->get('fabrik_form__user_type_id', 0, 'int')['0'];
try
{
  if ($userType <= '3') {
    return '0'; // No validation required:  status is 'Online'
  } else {
    return '1'; // Require validation: status is 'Waiting validation'
  }
}
catch (Exception $e)
{
  JFactory::getApplication()->enqueueMessage('Exception error:' . $e->getMessage());
}

That will changes select option value according to detected user type (from hidden field of this same form).
Useful for a moderation system with Fabrik user registration managed. ;)

Thanks for help again.
 
The try/catch in that code is unnecessary - there's nothing in the try block which would throw an exception. PHP itself (language constructs) don't throw exceptions. Fatal errors from PHP itself have to be caught with custom error handlers, not try/catch. Exceptions are only ever thrown by code that explicitly calls throw(). So you only need to catch exceptions that either you throw, or (in our context) J! or Fabrik library calls you make may throw.

Feel free to submit a PR in github on any tooltips you think can be improved.

-- hugh
 
You only really need it when calling library functions that might throw an exception - knowing which ones do means checking the documentation. A good example is running database queries using the J! library ...

Code:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('something')->from('sometable');
$db->setQuery($query);

try {
   $result = $db->loadResult();
}
catch (Exception $e) {
   // catch any database errors, and report them (although in real life you probably wouldn't show the user the MySQL error message
   JFactory::getApplication()->enqueueMessage('There was an error executing a query: " . $e->getMessage());
   // return whatever value makes sense for wherever this code is running
   return "";
}

// if we got this far, the query worked, so return the result
return $result;

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top