Précédent   Forum des professionnels en informatique > PHP > Langage > Formulaires
Formulaires Forum d'entraide sur les formulaires avec PHP. Avant de poster -> FAQ formulaires, Cours de formulaires et Sources de formulaires
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 01/11/2011, 11h13   #1
Invité de passage
 
Inscription : mars 2009
Messages : 7
Détails du profil
Informations forums :
Inscription : mars 2009
Messages : 7
Points : 0
Points : 0
Par défaut Formulaire Drupal, erreur mb_strlen()

Bonjour,

Je développe en PHP pour le CMS Drupal. J'ai réalisé un formulaire avec l'API Drupal. Le formulaire s'affiche correctement, cependant je reçois l'erreur suivante :

Citation:
Warning: mb_strlen() expects parameter 1 to be string, array given in drupal_strlen() (line 441 of C:\wamp\www\drupal-7.8\includes\unicode.inc).
Je pense que cette erreur vient du nom donné au champ "proportion" de mon formulaire, soit : '#name' => 'proportion[channel_'.$i.']' . Le nom étant, je le pense, considéré comme un array. Cependant j'ai été obligée de donner ce nom là pour pouvoir récupérer les données dans la fonction channel_form_submit(). Je n'arrive pas à résoudre cette erreur, quelqu'un aurait-il une idée ?

Je met le code plus complet à la suite, si besoin. Merci.


La fonction qui crée le formulaire est la suivante (je n'ai gardé ici que la partie du formulaire):

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* Define a channel form.
*/
function channel_form() {
 
foreach ($result_sort_channel as $sort_channel) {
	if($sort_channel->simple == '1')	{
		$form['sort_'.$sort_channel->idsort_of_channel]['name'] = array('#markup' => check_plain($sort_channel->name));
		for($i=1;$i<= $j; $i++) 
                {
                if(isset($channel[$sort_channel->idsort_of_channel][$i])) 
                       {
			//print($i);
			$options['channel_'.$i] = '';
			$enabled[] = 'channel_'.$i;
			$form['channel_'.$i]['name'] = array('#markup' => check_plain($channel[$sort_channel->idsort_of_channel][$i]));
			$form['channel_'.$i]['proportion'] = array('#title' => '','#type' => 'textfield','#size' => 10,'#id' => 'prop_'.$i ,'#name' => 'proportion[channel_'.$i.']');
			$form['channel_'.$i]['commission'] = array('#type' => 'textfield', '#id' => 'commission_'.$i, '#default_value' => check_plain($cost[$i][1]), '#size' => 1);
			$form['channel_'.$i]['monthly_cost'] = array('#type' => 'textfield', '#id' => 'monthly_cost_'.$i, '#default_value' => check_plain($cost[$i][2]), '#size' => 1);
			$form['channel_'.$i]['cost_per_reservation'] = array('#type' => 'textfield', '#id' => 'cost_per_reservation_'.$i,'#default_value' => check_plain($cost[$i][3]), '#size' => 1);
			$form['channel_'.$i]['salary_per_hour_chf'] = array('#type' => 'textfield', '#id' => 'salary_per_hour_chf_'.$i,'#default_value' => check_plain($cost[$i][4]), '#size' => 1);
			$form['channel_'.$i]['salary_per_hour_m'] = array('#type' => 'textfield', '#id' => 'salary_per_hour_m_'.$i,'#default_value' => check_plain($cost[$i][5]), '#size' => 1);
			$form['channel_'.$i]['distribution_cost'] = array('#type' => 'textfield', '#id' => 'distribution_cost_'.$i,'#default_value' => check_plain($cost[$i][6]), '#size' => 1);
			$form['channel_'.$i]['idsort_of_channel'] = array('#type' => 'textfield', '#id' => 'idsort_of_channel'.$i,'#default_value' => check_plain($sort_channel->idsort_of_channel), '#size' => 1,'#name' => 'idsort_of_channel[channel_'.$i.']');
			}
	}
}
	}
et ici se trouve la fonction qui permet de mettre en forme le formulaire (dans mon cas, je l'insère dans un tableau )

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function theme_channel_form($variables) {
 
$form = $variables['form'];
$rows = array();
foreach(element_children($form) as $chan)
{
        $sort_award = explode("_", $chan);
	if($sort_award[0] == 'sort')	{
		$row = array();
		$row[] = array('data' => drupal_render($form[$chan]['name']), 'colspan' => '9','id' => 'sort_channel_'.$chan.'','class' => 'sort_channel',);
		$rows[] = $row;
	} else	{
		if($sort_award[0] == 'channel')	{
			//print($chan); // résultat : channel_1 channel_2 channel_3 channel_4 channel_5 channel_6 channel_7 channel_8 channel_9 channel_10 channel_11 
			$row = array();
			$row[] = array('data' => drupal_render($form['enabled'][$chan]),'id' => 'channel_'.$chan.'','class' => 'checkboxes_channel',);
			$row[] = array('data' => drupal_render($form[$chan]['name']),'id' => 'name_'.$chan.'','class' => 'name_channel',);
			$row[] = array('data' => drupal_render($form[$chan]['proportion']),'id' => 'prop_'.$chan.'','class' => 'prop_channel',);
			$row[] = array('data' => '<img src="'.$module_path.'/img/info.png" alt="" /> ','id' => 'cost_'.$chan.'','class' => 'cost_channel',);
			$row[] = array('data' => drupal_render($form[$chan]['commission']),'id' => 'commission_'.$chan.'','class' => 'costs_channel');
			$row[] = array('data' => drupal_render($form[$chan]['monthly_cost']),'id' => 'monthly_cost_'.$chan.'','class' => 'costs_channel',);
			$row[] = array('data' => drupal_render($form[$chan]['cost_per_reservation']),'id' => 'cost_per_reservation_'.$chan.'','class' => 'costs_channel',);
			$row[] = array('data' => drupal_render($form[$chan]['salary_per_hour_chf']),'id' => 'salary_per_hour_chf_'.$chan.'','class' => 'costs_channel',);
			$row[] = array('data' => drupal_render($form[$chan]['salary_per_hour_m']),'id' => 'salary_per_hour_m'.$chan.'','class' => 'costs_channel',);
			$row[] = array('data' => drupal_render($form[$chan]['distribution_cost']),'id' => 'distribution_cost'.$chan.'','class' => 'costs_channel',);
			$row[] = array('data' => drupal_render($form[$chan]['idsort_of_channel']),'id' => 'idsort_of_channel'.$chan.'','class' => 'idsort_of_channel',);
			$rows[] = $row;
 
		}
	}
  }
 
$row = array();	
$row[] = array('data' => t('Total'),'colspan' => '2', 'class' => 'total_channel',);
$row[] = array('data' => drupal_render($form['total']),'id' => 'total_prop','class' => 'total_channel',);
$rows[] = $row;
$row = array();
$row[] = array('data' => drupal_render($form['submit']),'id' => 'cost_calculation','class' => 'total_channel', 'colspan' => '2',);
$rows[] = $row;
 
$header = array( array('data' => t('title'),'id' => 't_clear',  'colspan' => '2'), array('data' => t('Proportion (%)')), array('data' => t('Informations')));
 
$output = theme('table', array('header' => $header, 'rows' => $rows,'attributes' => array('id' => 'choose_channel')));
$output .= drupal_render_children($form);
 
 
return $output;
}
Et finalement, la fonction qui récupère les données du formulaire :

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
* Handle post-validation form submission.
*/
function channel_form_submit($form, &$form_state) {
 
$selectedchannels = array();
foreach (element_children($form_state['values']['enabled']) as $key) 
{
	 if ($form_state['values']['enabled'][$key]) {
		$chan = $form_state['values']['enabled'][$key];
		$idchan = explode("_", $chan); // 1
		$prop = $form_state['values']['proportion'][$key];
		$idsort_of_channel = $form_state['values']['idsort_of_channel'][$key];
 
		$channel = array(
		'idchannel' => $idchan[1],
		'name' => drupal_render($form[$chan]['name']),
		'proportion' => $prop,
		'idsort_of_channel' => $idsort_of_channel,
		);
		$selectedchannels[] = $channel;
	}
}
 
$_SESSION['selectedchannels'] = $selectedchannels;
 
}
rps8484a est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 10/11/2011, 19h06   #2
Invité de passage
 
Inscription : août 2008
Messages : 7
Détails du profil
Informations forums :
Inscription : août 2008
Messages : 7
Points : 4
Points : 4
Citation:
Envoyé par rps8484a Voir le message
Je pense que cette erreur vient du nom donné au champ "proportion" de mon formulaire, soit : '#name' => 'proportion[channel_'.$i.']' .
Je ne serais que te conseiller de changer le nom pour être sur de la provenance de l'erreur
Underq est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 12h31.


 
 
 
 
Partenaires

Hébergement Web