validation for username in related data

enzo2016

Member
Hi,
I have a related data like:
"country" table - "add region" table. Each country can have many regions. The regions could be added from the same user who was adding the correspondent country. So for example only the user adding France as country could add the regions of France.
It is only an example to explain my need to validate the user filling the "add region" form because only the user who added the correspondent country can add its regions. How could I validate the user filling the "add region" form?

Thanks

Enzo
 
You'd have to use the PHP validation, and look up the user ID from the country table.

So assuming your country table is something like id, country_name, user_id .... and your region table is id, region_name, country_id, user_id ...

Code:
$myCountryId = $formModel->formData['region___country_id_raw'];
$myCountryId = is_array($myCountryId) ? $myCountryId[0] : $myCountryId;

if (!empty($myCountryId)) {
   $myDb = FabrikWorker::getDbo();
   $myQuery = $myDb->getQuery(true);
   $myQuery->select('user_id')->from('country')->where('id = ' . $myDb->quote($myCountryId));
   $myDb->setQuery($myQuery);
   $countryUserId = $myDb->loadResult();
   if (JFactory::getUser()->get('id') == $myCountryUserId) {
      return true;
   }
}
return false;

-- hugh
 
I added the code into the user_id element (php validation) of 'region' form and tried filling that form from a user different from who added the correspondent country. I have no error, but it seems that anyone (every user) can fill that form.


Enzo
 
You'd have to debug it. Put in var_dump() to see what's actually going on in the code.

So before the first if, do ...

var_dump($myCountryId );exit;

... then submit the form, and see what it outputs, to make sure that got picked up.

if that works, remove it, and before the loadResult() line, do ...

var_dump((string)$myQuery);exit;

... and make sure it's creating the right query. If you aren't sure, paste that query into phpmyAdmin and run it, see if it selects what you expect.

I can't really go any further as part of subscription support for this kind of custom code. I could write / debug this for you as billable work.

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

Thank you.

Members online

Back
Top