Compare Three elements

HyperOsmar

Member
Dear Colleagues, I need your help...

I need to compare three fields:


'{cad_eventos___local}'; (the space available for the class)
'{cad_eventos___turno}'; (the shift in which the class will take place)
'{cad_eventos___data_inicio}'; (the class start date)

I created a form to schedule class spaces, so locations, shifts and dates cannot coincide.

Within the element '{cad_eventos___data_inicio}' I enabled validation in PHP to compare the fields.

Please where is the error:

$loc = '{cad_eventos___local}';
$tur = '{cad_eventos___turno}';
$ini = '{cad_eventos___data_inicio}';

$registrosVerificados = array();

$key = $loc . '_' . $tur . '_' . $ini;

if (in_array($key, $registrosVerificados)) {
return 'Date unavailable for the selected location and shift.';
} else {

$registrosVerificados[] = $key;

return 'Condition successfully verified!';
}
 
I'm not a specialist, but for me it seems $registrosVerificados is always an empty array if the validation takes place. Except you have more code to save and load the array to/from another field...

Just saw: you are still on J3/F3 ????
 
Last edited:
Like wezetel said, you are searching in empty array of $registrosVerificados as you do not add any data to it.

You need to query the database for existing space-shift-class combinations, then add the results to array and do your array search after that. See here for reference:
https://fabrikar.com/forums/index.php?wiki/php-common-tasks/

And you need to either return false or true, not just some text sting for the validation to work.

As this is mostly generic PHP, you should be able to validate your code also in ChatGPT quite successfully. Like: "I have this validation code: ...your code here... The code does not work. Please see if you can find some errors." or similar.
 
Dear Colleagues,

The record is being saved, but the following message appears:

Table 'formsaude181223.#__cad_eventos' doesn't exist

And it's saving conflicting records...

I'm using the code below in the form's PHP plugin, onAfterProcess option.

// Obtenha os dados do formulário
$local = JFactory::getApplication()->input->get('cad_eventos___local', '', 'string');
$dataInicio = JFactory::getApplication()->input->get('cad_eventos___data_inicio', '', 'string');
$dataTermino = JFactory::getApplication()->input->get('cad_eventos___data_termino', '', 'string');
$horaInicio = JFactory::getApplication()->input->get('cad_eventos___horario_inicio', '', 'string');
$horaTermino = JFactory::getApplication()->input->get('cad_eventos___horario_termino', '', 'string');

// Adapte o prefixo conforme necessário
$prefixo = 'formsaude181223_';

// Realize a validação
if (validarReserva($local, $dataInicio, $dataTermino, $horaInicio, $horaTermino, $prefixo)) {
// A reserva é válida, prossiga com o salvamento no banco de dados
// Insira aqui o código para salvar no banco de dados usando Fabrik
// Exemplo: $formModel->store();
return "Reserva efetuada com sucesso!";
} else {
// A reserva não é válida, exiba uma mensagem de erro
return "Erro: a reserva não pôde ser efetuada devido a conflitos de horário.";
}

// Função para validar a reserva
function validarReserva($local, $dataInicio, $dataTermino, $horaInicio, $horaTermino, $prefixo) {
// Adapte esta função de acordo com a estrutura do seu banco de dados e lógica de validação

// Verifique se a sala está disponível no período especificado
// Exemplo de consulta SQL (utilize a biblioteca de banco de dados do Joomla)
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('id'));
$query->from($db->quoteName('#__' . $prefixo . 'cad_eventos')); // Adiciona o prefixo à tabela
$query->where($db->quoteName('cad_eventos___local') . ' = ' . $db->quote($local));
$query->where('(' . $db->quoteName('cad_eventos___data_inicio') . ' BETWEEN ' . $db->quote($dataInicio) . ' AND ' . $db->quote($dataTermino) . ' OR ' . $db->quoteName('cad_eventos___data_termino') . ' BETWEEN ' . $db->quote($dataInicio) . ' AND ' . $db->quote($dataTermino) . ')');
$query->where('(' . $db->quoteName('cad_eventos___horario_inicio') . ' < ' . $db->quote($horaTermino) . ' AND ' . $db->quoteName('cad_eventos___horario_termino') . ' > ' . $db->quote($horaInicio) . ')');
$db->setQuery($query);

$conflitos = $db->loadResult();

// Se houver conflitos, a reserva não é válida
return empty($conflitos);
}

Any suggestion???...

upload_2023-12-19_10-23-58.png
 
Last edited:
Have you had a look at your DB? The error states clear that the table you are querying doesn't exist. I usually enter the correct name of a table (with no variables or do you try to create the table dynamically).
In addition I see multiple different where statements. As I'm no SQL specialist I don't know, if this is allowed
 
Dump your variables you are trying to get (not sure if "onAfter" knows still about the theapplication input) and the query you are creating.

Additionally
Stuff like $db->quoteName('cad_eventos___data_inicio') doesn't look correct, too.

If your element name is 'cad_eventos___data_inicio' then your table is 'cad_eventos' (without any J! prefix #__ ) AND the the column name is 'data_inicio' (not 'cad_eventos___data_inicio')
 
I think a validation should be done with a php validation or a php plugin onBeforeStore (as you have seen otherwise your record will be stored).

About your code:
Follow the adviced of the other posters here, debug your variables to see what your really get etc.
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top