SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,147
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)

    Drupal: Custom Form POST not being populated on submit

    I have a created a function to be called by drupal_get_form() to build a custom form but the form_state doesn't seem to be populated with any submitted values. Anyone know why this is the case?

    PHP Code:
    function storyteller_filter_form(&$form_state,$type null) {

        echo 
    '<pre>',print_r($form_state),'</pre>'// post is empty on submit
        
        /*
        * Select options
        */
        
    $books = array('0'=>'--');
        
    $chapters = array('0'=>'--');
        
    $pages = array('0'=>'--');
        
        
    /*
        * form configuration
        */
        
    $form = array();
        
        
    $form['book'] = array(
            
    '#title'=>t('Book')
            ,
    '#type'=>'select'
            
    ,'#required'=>false
            
    ,'#options'=>&$books
        
    );
        
        
    $form['chapter'] = array(
            
    '#title'=>t('Chapter')
            ,
    '#type'=>'select'
            
    ,'#required'=>false
            
    ,'#options'=>&$chapters
        
    );
        
        
    $form['page'] = array(
            
    '#title'=>t('Page')
            ,
    '#type'=>'select'
            
    ,'#required'=>false
            
    ,'#options'=>&$pages
        
    );
        
        
    $form['submit'] = array(
            
    '#type'=>'submit'
            
    ,'#value'=>'Submit'
            
    ,'#weight' => 40
        
    );
        
        
    /*
        * Get the storyteller DAO
        */
        
    $dao dao_get('storyteller');
        
        
    /*
        * Load all books as select options
        */
        
    foreach($dao->list_all_books() as $book) {
            
    $books[$book['nid']] = $book['title'];
        }
        
        
    $books_id 0;
        foreach(
    $books as $id=>$book) {
            
    $books_id $id;
            break;
        }
        
        
    /*
        * Load chapters for selected book
        */
        
    foreach($dao->list_all_chapters('n.title,n.nid,c.chapter_number',sprintf("c.books_id = %s",db_escape_string($books_id))) as $chapter) {
            
    $chapters[$chapter['nid']] = "{$chapter['chapter_number']}.) {$chapter['title']}";
        }
        
        
    $chapters_id 0;
        foreach(
    $chapters as $id=>$chapter) {
            
    $chapters_id $id;
            break;
        }
        
        
    /*
        * Load pages for selected chapter
        */
        
    foreach($dao->list_all_pages('p.page_number,n.nid',sprintf('p.chapters_id = %s',db_escape_string($chapters_id))) as $page) {
            
    $pages[$page['nid']] = $page['page_number'];
        }
        
        return 
    $form;


    Edit:

    Even the $_POST array seems to be empty upon submitting the form.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,147
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Adding a hook to alter the form and still the form_state doesn't seem to be populated with any submitted values.

    PHP Code:
    function storyteller_form_storyteller_filter_form_alter(&$form, &$form_state) {
        echo 
    '<pre>',print_r($form_state),'</pre>';


  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,147
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    I figured it out.

    When a form is submitted and has no validation errors it gets redirected. Normally, that is fine considering the redirect page just has some feedback on it. However, in my case I always want to rebuild the form as its a filter for mining data. So the solution I have at this moment is to always set an error on the form. That way it will never actually be redirected retaining the state as it was submitted.

    If anyone else has a better way to achieve it I would be open ideas though. Setting a form error to avoid the redirect just seems dirty.

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,147
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    The redirect can also be killed by using #redirect = false for the form configuration. That will kill the default redirect behavior without hacking the validation workflow.

  5. #5
    SitePoint Member
    Join Date
    Oct 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You saved me on this solution. I was trying to figure out why the POST values was not being passed back with the $form_state.

    Thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •