Jump to:

1971 Posts in 1275 Topics by 607 members

Form Questions

SilverStripe Forums » Form Questions » HiddenField on contactform throws a "get property of non-object"-error

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

Page: 1
Go to End
Author Topic: 833 Views
  • Pipifix
    Avatar
    Community Member
    54 Posts

    HiddenField on contactform throws a "get property of non-object"-error Link to this post

    Hi everybody.

    I've got a simple contactform on a staff page. The goal is to enable the frontenduser to sent a mail directly to the staffmember of the company.
    So i tweaked arams tutorial dataobjects as pages a little. I added a contactform and wanted to extract the staffmembers mailadress via hiddenfield.

    This is my code so far

    ...

    class StaffPage_Controller extends Page_Controller {
       
       //add our 'show' function as an allowed URL action
       static $allowed_actions = array(
          'show',
          'ContactForm'
       );
       
    function ContactForm() {
    // Create fields
    $fields = new FieldSet(
    new TextField('Name', 'Ihr vollständiger Name *'),
    new EmailField('Email', 'Ihre Mailadresse *'),
    new TextField('Phone', 'Eine Rückrufnummer'),
    new TextareaField('Comments','Ihre Nachricht *'),
    new HiddenField('StaffMail', '', $this->StaffMember->Mail) //$this->ID
    );

    // Create action
    $actions = new FieldSet(
    new FormAction('SendContactForm', 'absenden')
    );

    // Create Validators
    $validator = new RequiredFields('Name', 'Email', 'Comments');

    return new Form($this, 'ContactForm', $fields, $actions, $validator);
    }
       
       function SendContactForm($data, $form) {

    //Set data
    $From = $data['Email'];
    $To = $data['StaffMail'];
    $Subject = "E-Mail vom persönlichen Kontaktformular der Webseite";
    $email = new Email($From, $To, $Subject);
    //set template
    $email->setTemplate('ContactEmail');
    //populate template
    $email->populateTemplate($data);
    //send mail
    $email->send();
    //return to submitted message
    Director::redirect($this->Link("?success=1"));
    }

    public function Success()
    {
    return isset($_REQUEST['success']) && $_REQUEST['success'] == "1";
    }
       
       //Show the StaffMember detail page using the StaffPage_show.ss template
       function show()
       {      
          .
          .
          .
       }
       
       //Get the current staffMember from the URL, if any
       public function getStaffMember()
       {
          .
          .
          .
       }
       
       //Return our custom breadcrumbs
       public function Breadcrumbs() {
          .
          .
          .
       }      
    }

    Formgeneration and validation works as expected. With firefox i'm able to inspect the hiddenfield value. The value is seeded with the right mailadress (given by the DOM via Backend) of the member. But, there's this error [Notice] Trying to get property of non-object. Whats wrong with that?

    Thanks for your help. Pipifix

    EDIT: Obviously this code '$this->StaffMember->Mail' isn't correct? Additional question: is it possible to set the mailto with this code? $To = $data['StaffMail'];

  • Pipifix
    Avatar
    Community Member
    54 Posts

    Re: HiddenField on contactform throws a "get property of non-object"-error Link to this post

    According to this codeexample and by avoiding daisy-chaining, i edited my code to this:

    function ContactForm() {
    // Create fields

    if($stfmmbr = $this->getStaffMember()) {
          
    $fields = new FieldSet(
    new TextField('Name', 'Ihr vollständiger Name *'),
    new EmailField('Email', 'Ihre Mailadresse *'),
    new TextField('Phone', 'Eine Rückrufnummer'),
    new TextareaField('Comments','Ihre Nachricht *'),
    new HiddenField('StaffMail', 'StaffMail', $stfmmbr->Mail) //Avoid daisy-chaining: $this->StaffMember->Mail
    );

    // Create action
    $actions = new FieldSet(
    new FormAction('SendContactForm', 'absenden')
    );

    // Create Validators
    $validator = new RequiredFields('Name', 'Email', 'Comments');

    return new Form($this, 'ContactForm', $fields, $actions, $validator);
    }
    }

    ...the getStaffMember method

       //Get the current staffMember from the URL, if any
       public function getStaffMember()
       {
          $Params = $this->getURLParams();
          
          if(is_numeric($Params['ID']) && $StaffMember = DataObject::get_by_id('StaffMember', $Params['ID']))
          {      
             return $StaffMember;
          }
       }

    The form is rendered nicely. The hidden form value shows the correct mail but by sending the form, there is a white screen. Mhmm?
    I seems the getStaffMember() method returns empty. But why the (html) hidden field value is showing the right mail?

  • Pipifix
    Avatar
    Community Member
    54 Posts

    Re: HiddenField on contactform throws a "get property of non-object"-error Link to this post

    Nobody's got a clue?

    I ask myself whether this is the right way? Because the complete mailadress is visible in the value of the hiddenfield. Are these hidden fields crawlable by robots? And if this is the case is there another way to bind a contactform on a dataobject (staffmembers)?

    Despite of these questions i'm still interested in solving the issue with the non-object (empty hiddenField?) error.

    Thanks, Pipifix

    And sorry for my soliloquy. ;)

  • Pipifix
    Avatar
    Community Member
    54 Posts
  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: HiddenField on contactform throws a "get property of non-object"-error Link to this post

    When you submit a form it calls the form method first to get all the fields. If your form fields rely on a url parameter you'll run into issues as when you have submitted the form those parameters won't be there.

    I would suggest changing your form method to something like

    function ContactForm() {
    $value = ($stfmmbr = $this->getStaffMember()) ? $stfmmbr->Email : "";

    $fields = new FieldSet(
    new TextField('Name', 'Ihr vollständiger Name *'),
    new EmailField('Email', 'Ihre Mailadresse *'),
    new TextField('Phone', 'Eine Rückrufnummer'),
    new TextareaField('Comments','Ihre Nachricht *'),
    new HiddenField('StaffMail', 'StaffMail', $value)
    );

    // Create action
    $actions = new FieldSet(
    new FormAction('SendContactForm', 'absenden')
    );

    // Create Validators
    $validator = new RequiredFields('Name', 'Email', 'Comments');

    return new Form($this, 'ContactForm', $fields, $actions, $validator);
    }

    So remember, your form function will run after you submit.

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