Jump to:

1999 Posts in 1421 Topics by 613 members

Form Questions

SilverStripe Forums » Form Questions » form action

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 2456 Views
  • Roelfsche
    Avatar
    25 Posts

    form action Link to this post

    Hi,
    i try to create my own form. Everything works fine except my action is never reached. I derived the form class from Form because I want my own template to be used.
    First I created a new page type called ContactFormPage:

    class ContactFormPage extends Page {
    }

    class ContactFormPage_Controller extends Page_Controller {
       
       public function init() {
          parent::init ();
          
          //setze die locale, damit das Suchform auf deutsch switcht
          if ($this->dataRecord->hasExtension ( 'Translatable' )) {
             i18n::set_locale ( $this->dataRecord->Locale );
          }
       }
       public function ContactForm() {
          return new ContactForm ( $this, 'Kontakt' );
       }
       
       function submit($data, $form) {
          Debug::message("am i here?");   

          $this->redirect('EmailDanke');
    /*   $email = new Email('kontakt@schloss-open-air.de', 'rolf.staege@lumturo.net', 'Webseitenkontakt');
          $email->setTemplate('EmailForm');
          $email->populateEmail($data);
    //      $email->send();
    */      
       }
    }


    Then I created my form class as follows:

    DEFINE ( 'TEXTFIELD_MAXLENGTH', 20 );
    class ContactForm extends Form {
       function __construct($controller, $name) {

          if ($controller->getRequest ()->getVar ( 'recv_chef' ))
             $rcc = new CheckboxField ( 'recv_chef', _t ( 'ContactForm.RECV_CHEF', 'geschaeftsleitung' ), 1 );
          else
             $rcc = new CheckboxField ( 'recv_chef', _t ( 'ContactForm.RECV_CHEF', 'geschaeftsleitung' ) );
          
          if ($controller->getRequest ()->getVar ( 'recv_presse' ))
             $rcp = new CheckboxField ( 'recv_presse', _t ( 'ContactForm.RECV_PRESSE', 'presse' ), 1 );
          else
             $rcp = new CheckboxField ( 'recv_presse', _t ( 'ContactForm.RECV_PRESSE', 'presse' ) );
          
          if ($controller->getRequest ()->getVar ( 'recv_booking' ))
             $rcb = new CheckboxField ( 'recv_booking', _t ( 'ContactForm.RECV_BOOKING', 'booking' ), 1 );
          else
             $rcb = new CheckboxField ( 'recv_booking', _t ( 'ContactForm.RECV_BOOKING', 'booking' ) );
          
          if ($controller->getRequest ()->getVar ( 'recv_produktleitung' ))
             $rcpr = new CheckboxField ( 'recv_produktleitung', _t ( 'ContactForm.RECV_PRODUKTLEITUNG', 'produktleitung' ), 1 );
          else
             $rcpr = new CheckboxField ( 'recv_produktleitung', _t ( 'ContactForm.RECV_PRODUKTLEITUNG', 'produktleitung' ) );
          
          $fields = new FieldSet ( new OptionsetField ( 'geschlecht', 'geschlecht_t', array ('1' => _t ( 'Contactform.MALE', 'Herr' ), '2' => _t ( 'Contactform.FEMALE', 'Frau' ) ) ), new TextField ( 'name', 'Name', '', TEXTFIELD_MAXLENGTH ), new TextField ( 'vorname', 'Vorname', '', TEXTFIELD_MAXLENGTH ), new TextField ( 'strasse', 'Strasse', '', TEXTFIELD_MAXLENGTH ), new TextField ( 'plz', 'PLZ', '', 5 ), new TextField ( 'ort', 'Ort', '', TEXTFIELD_MAXLENGTH ), new EmailField ( 'email', 'E-Mail Adresse', '', TEXTFIELD_MAXLENGTH ), new TextField ( 'telefon', 'Telefon', '', TEXTFIELD_MAXLENGTH ), $rcc, $rcp, $rcb, $rcpr, new TextareaField ( 'message', 'titel', 10, 180, '' ) );
          
          $actions = new FieldSet ( new FormAction ( 'submit', 'Abschicken' ) );
          
    //      $requiredFields = new RequiredFields ( 'geschlecht', 'name', 'vorname', 'strasse', 'plz', 'ort', 'email', 'telefon', 'message' );
          //    $requiredFields = new RequiredFields('name', 'plz');
          
    // parent::__construct ( $controller, $name, $fields, $actions, $requiredFields );
    parent::__construct ( $controller, $name, $fields, $actions );

       }
       
       function forTemplate() {
          return $this->renderWith ( array ($this->class, 'Form' ) );
       }
       
       function submit()
       {
          echo "or here...";
       }
    }


    The HTML-form-Code, that is created looks like this:

    <form id="ContactForm_Kontakt" action="/kontakt/Kontakt" method="post" enctype="application/x-www-form-urlencoded">
    ...
    <input class="action" id="ContactForm_Kontakt_action_submit" type="submit" name="action_submit" value="Abschicken" title="Abschicken" />
    </form>


    As I understand, the method "submit" of my form class should be called, if the form will be transmitted. But this method is never reached. Does anybody know, what's the reason for that?
    Thank you,
    roelfsche

  • zenmonkey
    Avatar
    Community Member
    527 Posts

    Re: form action Link to this post

    I haven't checked if your method is sound but I think the submit action needs to be defined on your form class

  • Roelfsche
    Avatar
    25 Posts

    Re: form action Link to this post

    I defined it in my Form class. It's called 'submit' and I define my Action as follows:

    $actions = new FieldSet ( new FormAction ( 'submit', 'Abschicken' ) );


    I also define it in my Form-Class-Page-Controller, because I red it somewhere (but I think it's only necessary, if I don't derive my own Form).
    But neither of them is called...
    Roelfsche

  • Roelfsche
    Avatar
    25 Posts

    Re: form action Link to this post

    I started again from scratch and now it works.
    One thing I changed is, that the form template now resides in "includes" and not in "templates". Maybe, that's the reason...
    Roelfsche

    2456 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.