validation from DB and autofill other fields CFv5

teldrive 11 Mar, 2014
Hi everyone, after several days searching on forum i must say i am lost
I need check and validate the first text box of my form called 'nif': if this'nif' exists on a local joomla table, some fields must be autofilled (one issue with this is fields ID of database are differents of my form)

I have found some clues :
1- i tried with db read, but i couldn't make condition based on a posted field
<?php
 $where = array(   'DNI' => '{$form->data['nif']}');
 return $where;
 ?>


2 I tried with Ajax and custom code but it didn't work,
<?php
 $db =& JFactory::getDBO();
$nif=JRequest::getString('nif','','post');
 $query = "
   SELECT COUNT *
     FROM `#__database`
     WHERE `DNI` = '$nif' ;
 ";
 $db->setQuery($query);
 $count = $db->loadResult();
 if ( $count < 1 ) {
   $form->validation_errors['nif'] = "No existe existe ningĂșn socio con este NIF.";
   return false;
 }


can anyone help ?
Thanks
teldrive 11 Mar, 2014
Answer
:D Hey everyone, I must tell you that all the information was found on forum, but i spent one day to locate it, I hope to make easier this work to whom can need it

Final results and needs:
1- we needed validate one form field "nif" to identify partners from a database that we create in joomla. It was made with DB read, I tried directly with php code without succes
2-we had an issue with DB read because we use "nif" as variable and it doesn't exist until form is submitted , I tried do it with Ajax/jquery in order to avoid to use different pages,and get more interactive page also without sucess, so final workaround was with multipage solution, may be not Smart but finally it works
3-And last and not less important to populate all database information of valid user to save hard work of filling forms to users.😉

We guess some expert programmer can improve this solution, i am confident it is possible
The code of dB read:
enabled yes
database name: jos_xxxxxx
muliread: no
model ID: yes
model: nav
Fields : id,Nombre,Apellidos,Email,Direccion
conditions: // nif is variable and I have to first submit a previous page to obtain it.
 <?php 
 return array('DNI' => $form->data("nif")); 
 ?>

the code of eventswitcher// this function allow adapt different fields id from database to form
<?php
 if (isset($form->data['nav']['id'])) {
$form->data['email']=$form->data['nav']['Email'];
$form->data['nif0']=$form->data['nav']['DNI'];
$form->data['login']=$form->data['nav']['DNI'];
$form->data['nombre']=$form->data['nav']['Nombre'];
$form->data['apellidos']=$form->data['nav']['Apellidos'];
$form->data['codpost']=$form->data['nav']['CodPost'];
$form->data['direccion']=$form->data['nav']['Direccion'];
$form->data['provincia']=$form->data['nav']['Provincia'];
$form->data['ciudad']=$form->data['nav']['Poblacion'];
      return success;
 }
 else {
      return fail;
 }
 ?>
This topic is locked and no more replies can be posted.

VPS & Email Hosting 20% discount
hostinger