Hi,

I create a form with Drupal API. The form print good, but I receive this error :

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).

I think, this error come from the name of this input :

$form['channel_'.$i]['proportion'] = array('#title' => '','#type' => 'textfield','#size' => 10,'#id' => 'prop_'.$i ,'#name' => 'proportion[channel_'.$i.']');

The name 'proportion[channel_'.$i.']', looks like an array for Drupal. But I need it for my *_submit() function. Someone has an idea ? And can help me ? Thanks.

Comments

Badi’s picture

Hi,

you can not use attribute '#name' for 'textfield' element.
http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....

Someone4’s picture

Thanks for your response.

How can I recover informations of my formulaire ? I used the '#name' attribute, because I thought that the _submit() methode use the name of the textfield to bring informations back.

My _submit() methode looks like that :

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;
}

So, I recover informations of my textfield, like that : $form_state['values']['proportion'][$key] (the key value is 'channel_1', 'channel_2',... )

My textfield :

$form['channel_'.$i]['proportion'] = array('#title' => '','#type' => 'textfield','#size' => 10,'#id' => 'prop_'.$i ,'#name' => 'proportion[channel_'.$i.']');

I haven't found an other way to bring informations back. I thought that $form_state use the name of the element. I am wrong ?

jaypan’s picture

Seeing as you haven't showed us any code with mb_strlen() in it, it's hard to tell you what is going wrong.

Contact me to contract me for D7 -> D10/11 migrations.

Badi’s picture

Yes, the $form_state use the name of the element.
in your form, the name of element is 'proportion' and not 'channel_1', channel_2....

Someone4’s picture

I show you my form :


/**
* Define a channel form.
*/
function channel_form() {

//I hide the part with SQL query,  it will be more readable...


/*  I construct my form in a foreach
*/
foreach ($result_sort_channel as $sort_channel) {
		
	for($i=1;$i<= $j; $i++) {
		if(isset($channel[$sort_channel->idsort_of_channel][$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.']');
		}
			
	}
}
	
	$form['enabled'] = array(
                        '#type' => 'checkboxes',
                        '#name' => $i ,'#title' => '', 
                        '#default_value' =>$enabled,
                        '#title_display' => 'invisible',
                        '#options' => $options,);

	$form['total'] = array(
                        '#title' => '',
                        '#type' => 'textfield',
                        '#size' => 10, 
                        '#attributes' => array('readonly' => 'readonly'),);

	$form['submit'] = array('#type' => 'submit', '#value' => t('Cost calculation'),'#size' => 60,);
	$form['#theme'] = 'channel_form';
	
	return $form;
}

I construct my form in a foreach, for every channels I have 'name', 'proportion', 'commission', 'monthly cost',... elements. I name these elements like that : $form['channel_'.$i]['name'], the channel id and next the element.
Finally in the _submit method, I can't just display the proportion with : $form_state['values']['proportion'],
I use : $form_state['values']['proportion'][$key]; (and this method works, values are found, but I have the error)

Do you understand better why the name of my element must be 'proportion[channel_1]', rather than only 'proportion'? If it's only 'proportion', I can't bring back differents proportions for different channels.

Your comment about the #name attribute, is right. But if I can't modify the name of the element, I don't know how recover informations... Because all proportion elements will have the same name 'proportion'

I show you what my form looks like :
http://data.imagup.com/10/1134827538.jpg

Thanks.

Someone4’s picture

Ok.... I fix it.

I use the '#name' attribute for an other element and I didn't note it... when I tried to change code, the error persisted...

I use this method for naming textfield element : $form['channel_'.$i]['proportion_'.$id]. I differentiate proportion element with the $id.