⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

callback_before_insert



cybersven
  • profile picture
  • Member

Posted 21 January 2014 - 15:07 PM

Hi,

 

I got an input for mobile (entry:0012345678) which I want to format to 00 12 34 56 78, so I've tried to use callback_before_insert like this :

$crud->callback_before_insert(array($this,'format_phone'));

and

my function :

 

function format_phone($post_array)
{
$post_array = preg_replace('/[^0-9]+/', '', $post_array); 

$post_array = substr($post_array, -9); 

$pattern = '0\1 \2 \3 \4 \5'; 

$post_array = preg_replace('/(\d{1})(\d{2})(\d{2})(\d{2})(\d{2})/', $pattern, $post_array); 

return $post_array;
}

 

 

But when I add a contact the number still appear in 0012345678 format, did I missed something ? (The function works fine, I've tested it)

 

 

Thx in advance

 

 


Andre Tzermias
  • profile picture
  • Member

Posted 21 January 2014 - 18:53 PM

Man, as I'm no expert I can't tell for sure, but I think from the examples that the $post_array is a array, so I would try $post_array['name_of_field'] instead of just using $post_array

 

That is because I think the callback would pass all info as an array and you should say which value you are using, right?

 

Sorry if this doesn't help and I said anything foolish.

still learning.

 

=D


cybersven
  • profile picture
  • Member

Posted 22 January 2014 - 09:44 AM

Thx for your reply but I've tested it before posting and it doesn't work :-( so I tried again and it works with this function :

 

public function format_phone($post_array)
{
     //Supprimer tous les caractères qui ne sont pas des chiffres 
$post_array['mobile'] = preg_replace('/[^0-9]+/', '', $post_array['mobile']); 
//Garder les 9 derniers chiffres 
$post_array['mobile'] = substr($post_array['mobile'], -9); 
//On ajoute +33 si la variable $international vaut true et 0 dans tous les autres cas 
$motif = '0\1 \2 \3 \4 \5'; 
$post_array['mobile'] = preg_replace('/(\d{1})(\d{2})(\d{2})(\d{2})(\d{2})/', $motif, $post_array['mobile']); 
     return $post_array;
}

Thx