[support] Form textfield data retreive

Austin Einter austin.einter at gmail.com
Wed Mar 16 01:10:36 UTC 2011


Hi All
This issue is resolved.

While adding textfield, I used #value attribute, and that was causing issue.
Instead, if #default_value attribute is used, then I am able to retreive the
value.

So is it a expected behavior or something wrong.

Regards
Austin




On Tue, Mar 15, 2011 at 6:37 PM, Austin Einter <austin.einter at gmail.com>wrote:

> Hi All
> I have a small working custom module. Code is as below.
>
> It's a simple form.
>
> In one line I have 4 fields (company, start date, end date, checkbox).
> Below I have "add/remove more companies" button.
>
> If user clicks "add/remove more companies" button, it adds one more line
> having company, start date, end date, checkbox fields.
>
> But in the process, it is loosing user entered data in previous line like
> company name, start date etc.
>
> Now the question is what logic / tricks I need to follow so that, I can
> keep user entered data in newly rendered form.
>
> Best Regards
> Austin.
>
>
> <?php
> function resume_submit_menu() {
>  $items = array();
>  $items['resume_submit/form'] = array(
>  'title' => t('Submit Your Resume'),
>  'page callback' => 'resume_submit_form',
>  'access arguments' => array('access content'),
>  'type' => MENU_CALLBACK,
>  );
>  $items['resume_submit/form/morecompanies'] = array(
>  'page callback' => 'resume_submit_add_more_companies',
>  'access arguments' => array('access content'),
>  'type' => MENU_CALLBACK,
>  );
>  return $items;
> }
> function resume_submit_form() {
>  return drupal_get_form('resume_submit_my_form');
> }
> function resume_submit_my_form($form_state) {
>  $form = array('#cache' => TRUE,);
>
>  if($form_state['values']['numOfCompanies'] == '')
>  {
>   $all_companies = 1;
>  }
>  else
>  {
>   $all_companies = $form_state['values']['numOfCompanies'];
>  }
>  $form['numOfCompanies'] = array(
>  '#type' => 'value',
>  '#value' => t($all_companies),
>  );
>
>  //Add a wrapper for the companies and add more button.
>  $form['work_history_wrapper'] = array(
>   '#title' => t("Work history: Mention all the companies you gave worked so
> far"),
>   '#type' => 'fieldset',
>   '#collapsible' => TRUE,
>   '#collapsed' => FALSE,
>   '#prefix' => '<div class="clear-block" id="work-history-wrapper">',
>   '#suffix' => '</div>',
>  );
>  //Container for just companies
>  $form['work_history_wrapper']['allcompanies'] = array(
>   '#prefix' => '<div id="all-companies">',
>   '#suffix' => '</div>',
>   '#tree' => TRUE,
>  );
>  //Need to do: Get the company name, start date and end date, pass it to
> resume_submit_add_one_company_form, so that it is shown properly
>  for($delta = 0; $delta < $all_companies; $delta++)
>  {
>   $company_name =
> isset($form_state['values']['allcompanies'][$delta]['companies']) ?
> $form_state['values']['allcompanies'][$delta]['companies'] : '';
>   $form['work_history_wrapper']['allcompanies'][$delta]['companies'] =
> array(
>    '#type' => 'textfield',
>    '#title' => t('Company'.'/'.($delta + 1)),
>    '#parents' => array('allcompanies', $delta, 'companies'),
>    '#value' => t($company_name),
>    '#size' => 30,
>    '#maxlength' => 50,
>   );
>   $form['work_history_wrapper']['allcompanies'][$delta]['startdate'] =
> array(
>    '#type' => 'date',
>    '#title' => t('Start Date'),
>    '#parents' => array('allcompanies', $delta, 'startdate'),
>   );
>   $form['work_history_wrapper']['allcompanies'][$delta]['enddate'] = array(
>    '#type' => 'date',
>    '#title' => t('End Date'),
>    '#parents' => array('allcompanies', $delta, 'enddate'),
>   );
>   $form['work_history_wrapper']['allcompanies'][$delta]['removecompany'] =
> array(
>    '#type' => 'checkbox',
>    '#title' => t('Check to remove'),
>    '#parents' => array('allcompanies', $delta, 'removecompany'),
>   );
>  }
>
>  $form['work_history_wrapper']['morecompany'] = array(
>   '#type' => 'submit',
>   '#value' => t('Add / Remove Companies'),
>   '#weight' => 1,
>   '#ahah' => array(
>    'path' => 'resume_submit/form/morecompanies',
>    'wrapper' => 'all-companies',
>    'method' => 'replace',
>   ),
>  );
> $form['submit'] = array(
> '#type' => 'submit',
> '#value' => 'Submit',
> '#submit' => array('resume_submit_submit'),
> );
> return $form;
> }
> function  resume_submit_add_more_companies() {
>  include_once 'modules/node/node.pages.inc';
>  $form_state = array('storage' => NULL, 'submitted' => FALSE);
>  $form_build_id = $_POST['form_build_id'];
>
>  // Get the form from the cache.
>  $form = form_get_cache($form_build_id, $form_state);
>
>  $args = $form['#parameters'];
>  $form_id = array_shift($args);
>
>  // We will run some of the submit handlers so we need to disable
> redirecting.
>  $form['#redirect'] = FALSE;
>
>  // We need to process the form, prepare for that by setting a few
> internals
>  // variables.
>  $form['#post'] = $_POST;
>  $form['#programmed'] = FALSE;
>  $form_state['post'] = $_POST;
>
>  // Build, validate and if possible, submit the form.
>  drupal_process_form($form_id, $form, $form_state);
>  //drupal_set_message(t("3. Output is:
> ").t($form_state['values']['numOfCompanies']));
>  $form_state['values']['numOfCompanies'] =
> $form_state['values']['numOfCompanies'] + 1;
>
>  // This call recreates the form relying solely on the form_state that the
>  // drupal_process_form set up.
>  $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
>
>  // Render the new output.
>  $choice_form = $form['work_history_wrapper']['allcompanies'];
>
>  unset($choice_form['#prefix'], $choice_form['#suffix']); // Prevent
> duplicate wrappers.
>
>  $output = theme('status_messages') . drupal_render($choice_form);
>  drupal_json(array('status' => TRUE, 'data' => $output));
>
> }
> function resume_submit_validate($form, &$form_state) {
> }
>
> function resume_submit_submit($form, &$form_state) {
> }
> ?>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/support/attachments/20110316/ee25190a/attachment-0001.html 


More information about the support mailing list