in teoria lo puoi fare della rule che ti controlla la prtita iva, basta una semplice query facendo attenzione che la partita iva che stai controllando si stia riferendo ad un nuovo inserimento e non a una modifica.
Se guardi in libraries/joomla/form/rules è lo stesso principio della regola di validazione username
class JFormRuleCodicefiscale extends JFormRule
{
protected $regex = '^([0-9]{11})$|^([a-zA-Z]{6}[0-9]{2}[a-zA-Z]{1}[0-9]{2}[a-zA-Z]{1}[0-9]{3}[a-zA-Z]{1})$|^([a-zA-Z]{2}[0-9]{11})$';
public function test(&$element, $value, $group = null, &$input = null, &$form = null)
{
// Test the value against the regular expression.
if (!parent::test($element, $value, $group, $input, $form))
{
return false;
}
// Get the database object and a new query object.
$db = JFactory::getDBO();
$query = $db->getQuery(true);
// Build the query.
$query->select('COUNT(*)');
$query->from('#__acc_partecipanti');
$query->where('codice_fiscale = ' . $db->quote($value));
// Set and query the database.
$db->setQuery($query);
$duplicate = (bool) $db->loadResult();
// Check for a database error.
if ($db->getErrorNum())
{
JError::raiseWarning(500, $db->getErrorMsg());
}
if ($duplicate)
{
return false;
}
return true;
}
}
Un'ultima cosa che vorrei chiedervi:$form->setFieldAttribute((string)$element['name'], 'message', 'Messaggio alternativo');
$form->setFieldAttribute((string)$element['name'], 'message', (string)$element['message2']);
class JFormRuleCodicefiscale extends JFormRule
{
/*Controlla se il codice fiscale o la partita iva sono stati inseriti nel formato corretto*/
protected $regex = '^([0-9]{11})$|^([a-zA-Z]{6}[0-9]{2}[a-zA-Z]{1}[0-9]{2}[a-zA-Z]{1}[0-9]{3}[a-zA-Z]{1})$|^([a-zA-Z]{2}[0-9]{11})$';
/*Controlla che già non esista un record con la stessa partita iva o codice fiscale*/
public function test(&$element, $value, $group = null, &$input = null, &$form = null)
{
// Test the value against the regular expression.
if (!parent::test($element, $value, $group, $input, $form))
{
return false;
}
// Get the database object and a new query object.
$db = JFactory::getDBO();
$query = $db->getQuery(true);
// Build the query.
$query->select('COUNT(*)');
$query->from('#__acc_partecipanti');
$query->where('codice_fiscale = ' . $db->quote($value));
// Get the extra field check attribute.
$userId = ($form instanceof JForm) ? $form->getValue('id') : '';
$query->where($db->quoteName('id') . ' <> ' . (int) $userId);
// Set and query the database.
$db->setQuery($query);
$duplicate = (bool) $db->loadResult();
// Check for a database error.
if ($db->getErrorNum())
{
JError::raiseWarning(500, $db->getErrorMsg());
}
if ($duplicate)
{
$form->setFieldAttribute((string)$element['name'], 'message', (string)$element['message2']);
return false;
}
return true;
}
}
come vedi ho anche inserito
// Get the extra field check attribute.
$userId = ($form instanceof JForm) ? $form->getValue('id') : '';
$query->where($db->quoteName('id') . ' <> ' . (int) $userId);
<?xml version="1.0" encoding="UTF-8"?>
<form addrulepath="/administrator/components/com_accontratti/models/rules">
<fieldset>
<field
name="id"
type="hidden" />
<field
name="ragione_sociale"
type="text"
label="COM_ACCONTRATTI_RAGIONE_SOCIALE"
class="inputbox"
required="true" />
<field
name="codice_fiscale"
size="16"
type="text"
label="COM_ACCONTRATTI_CODICE_FISCALE"
class="inputbox"
validate="codicefiscale"
message="COM_ACCONTRATTI_CODICE_FISCALE_ERRORE"
message2="COM_ACCONTRATTI_CODICE_FISCALE_ESISTE_ERRORE"
required="true" />
<field
name="azienda_madre"
type="aziendamadre"
label="COM_ACCONTRATTI_AZIENDA_MADRE"
class="inputbox" />
<field
name="id_fiscale_estero"
type="text"
label="COM_ACCONTRATTI_ID_FISCALE_ESTERO"
class="inputbox" />
<field
name="ruolo"
type="text"
label="COM_ACCONTRATTI_RUOLO"
class="inputbox" />
<field
name="id"
type="text"
default="0"
readonly="true"
class="readonly"
label="JGLOBAL_FIELD_ID_LABEL"
description ="JGLOBAL_FIELD_ID_DESC" />
</fieldset>
</form>
<field
name="id"
type="hidden" />
<li>
<label id="jform_id-lbl" for="jform_id" class="hasTip" title="" aria-invalid="false">ID</label>
<input type="text" name="jform[id]" id="jform_id" value="1" class="readonly" readonly="readonly" aria-invalid="false">
</li>
Probabilmente hai un problema nel bind della form al momento della validazione, ma per questo problema diventa come cercare un ago nel pagliaio. Utilizzi le funzioni ereditate dalle classi del controller e del model oppure le hai riscritte tu?
... è possibile che non si chiami id o che sia annidato in altro
// Get the extra field check attribute.
$partId = ($form instanceof JForm) ? $form->getValue('id') : '';
$query->where($db->quoteName('id') . ' <> ' . (int) $partId);
@ricctram
se non hai un debugger inserisci un
print_r($form);die();
dopo l'assegnazione e vedi i valori contenuti. già che ci sei stampati anche la query.
ciao,
marco
JForm Object ( [data:protected] => JRegistry Object ( [data:protected] => stdClass Object ( ) ) [errors:protected] => Array ( ) [name:protected] => com_accontratti.partecipante [options:protected] => Array ( [control] => jform ) [xml:protected] => JXMLElement Object ( [@attributes] => Array ( [addrulepath] => /administrator/components/com_accontratti/models/rules ) [fieldset] => JXMLElement Object ( [field] => Array ( [0] => JXMLElement Object ( [@attributes] => Array ( [name] => ragione_sociale [type] => text [label] => COM_ACCONTRATTI_RAGIONE_SOCIALE [class] => inputbox [required] => true ) ) [1] => JXMLElement Object ( [@attributes] => Array ( [name] => codice_fiscale [size] => 16 [type] => text [label] => COM_ACCONTRATTI_CODICE_FISCALE [class] => inputbox [validate] => codicefiscale [message] => COM_ACCONTRATTI_CODICE_FISCALE_ERRORE [message2] => COM_ACCONTRATTI_CODICE_FISCALE_ERRORE [required] => true ) ) [2] => JXMLElement Object ( [@attributes] => Array ( [name] => azienda_madre [type] => aziendamadre [label] => COM_ACCONTRATTI_AZIENDA_MADRE [class] => inputbox ) ) [3] => JXMLElement Object ( [@attributes] => Array ( [name] => id_fiscale_estero [type] => text [label] => COM_ACCONTRATTI_ID_FISCALE_ESTERO [class] => inputbox ) ) [4] => JXMLElement Object ( [@attributes] => Array ( [name] => ruolo [type] => text [label] => COM_ACCONTRATTI_RUOLO [class] => inputbox ) ) [5] => JXMLElement Object ( [@attributes] => Array ( [name] => id [type] => text [default] => 0 [readonly] => true [class] => readonly [label] => JGLOBAL_FIELD_ID_LABEL [description] => JGLOBAL_FIELD_ID_DESC ) ) ) ) ) )
stampando la query questo è il risultato:
dopo l'assegnazione e vedi i valori contenuti. già che ci sei stampati anche la query.
SELECT COUNT(*) FROM #__acc_partecipanti WHERE codice_fiscale = '12345678904' AND `id` <> 0
$userId = $this->getState('user.id');
// Initialise the table with JUser.
$this->data = new JUser($userId);
if(isset($data['id']) && (int)$data['id'] > 0)
$form->setValue('id',null,(int)$data['id']);
if (!$form) {
return false;
} else {
if(isset($data['id']) && (int)$data['id'] > 0)
$form->setValue('id',null,(int)$data['id']);
return $form;
}