BUG - isuniquevalue on user field

rdiana

Member
Dear Friends,
I've applied the Fabrik validation "isuniquevalue" on a user field (setted on "ID") to allow only one record for each user. Unfortunately the plugin seems not working only with the USER field (it works fine with other fields). Can you verify this bug?

Thank you,

Roberto
 
It's not so much a bug, more that it just won't work with the 'user' element, just because of the way the user element works internally.

You'll probably have to roll your own validation in PHP, and use the php validation rule. The code would be something like this:

PHP:
$listModel = $elementModel->getlistModel();
$element = $elementModel->getElement();
$list = $listModel->getTable();
$db = $listModel->getDb();
$lookuptable 	= $db->NameQuote($table->db_table_name);
$user = JFactory::getuser();
$data = $user->get('id');
$data = $db->Quote($data);
$query = $db->getQuery(true);
$query->select('COUNT(*)')
	->from($lookuptable)
	->where($element->name.' = '.$data);
$pk = FabrikString::safeColNameToArrayKey($table->db_primary_key);
$rowid = JRequest::getVar($pk, '');
if (!empty( $rowid)) {
	$query->where($table->db_primary_key .' != '.$db->Quote($rowid));
}
$db->setQuery($query);
$c = $db->loadResult();
return ($c == 0);

NOTE - you'll need to update to latest github for this to work, as I just had to rename $element to $elementModel in the PHP plugin.

-- hugh
 
Thank you very much, Cheesegrits. But does it exist a standard method to allow, to each user, the addition on ONLY ONE record in a database table? In other words, is it correct, as in my case, to use a valitation rule (isuniquevalue or areuniquevalue) or other ways can be performed without any addition of PHP code?


Best regards,
Roberto
 
I think the way to go is to have a hidden field in your form to record the ID of the user when he submit its record.
Then use the PHP form plugin to perform a check when accessing the form (onBeforeLoad) : if the user ID of the connected user is already present in the database table, the user is redirected to a page or message specifying only one entry is allowed; if not, he has access to the form.
 
Dear Cheesegrits,
where can I paste your code? si it a Condition for the PHP Validation Rule of an element plugin? and what plugin? and, in this case, what PHP code and what condition (substitution)?

Thank you...
 
hi there is a PHP validation rule in the code at github that you can download and install.
It should allow you to paste the php in a text area
 
Dear Friends,
I've solved by performing, on onBeforeLoad Form-event, the following PHP code:

$app = JFactory::getApplication();
$db =& JFactory::getDBO();
$query = "SELECT count(*) FROM `res_labs_al` WHERE `user_id` = {$my->id}";
$db->setQuery($query);
$value = $db->loadResult();
if ($value > 0) {
$app->redirect(JRoute::_('index.php?option=com_fabrik&view=list&listid=4'), JText::_('Already reserved') );
}


which works fine!.
Thank you very much for your suggestions.

Cheers,
Roberto
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top