Jump to:

1999 Posts in 1421 Topics by 613 members

Form Questions

SilverStripe Forums » Form Questions » Upload photo in Frontend forms

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

Page: 1
Go to End
Author Topic: 1515 Views
  • _Jam_
    Avatar
    Community Member
    9 Posts

    Upload photo in Frontend forms Link to this post

    Hi, I hope somebody has already did this. I've been searching in the Forum but I haven't found the right solution to my problem.
    I have a custom form that updates member information with upload photo. Im using SimpleImageField here but I cant seem to make it work.

    class updateMemberInfo_Controller extends Page_Controller {
    ..

    function getMemberForm() {

          // List your fields here
          $fields = new FieldSet(
             // List your fields here
             new TextField( "FirstName", "First name" ),
             new TextField( "Surname" ),
    new EmailField( "Email", "Change Email Address" ),
    //new EmailField( "RetypeEmail", "Re-enter Email" ),
    new OptionsetField( "Gender", "Gender", array( "Male" => "Male", "Female" => "Female" ) ),
    new TextAreaField( "Address" ),
    new TextField( "Contact" ),
    new TextField( "Country" ),
    new SimpleImageField( 'Photo' )
    );

    $actions = new FieldSet( new FormAction( "doSave", "Save", "", "", "big-button" ) );

          $requiredFields = new RequiredFields( "Email", "FirstName", "Surname", "Address" );

    $form = new Form( $this, "Form", $fields, $actions, $requiredFields );

    return $form;
    }

    function doSave( $data, $form ) {

    $file = new Image();
    $file->loadUploadedImage( $_FILES[ 'Photo' ] );

    // Member object and load the form data into it
    $member = Member::currentUser();
    $form->saveInto( $member );

    // Write it to the database. This needs to happen before we add it to a group
    $member->Birthday = $data[ 'Month' ]."/".$data[ 'Day' ]."/".$data[ 'Year' ];

    $form->saveInto( $member );

    $form->AddErrorMessage( 'message', "Your profile has been updated.", 'good' );

    return Director::redirectBack();

    }

    Any help would be greatly appreciated. Thanks!

    /John

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: Upload photo in Frontend forms Link to this post

    I did this a while ago - might not be the cleanest (I use uploadify now) - but I hope it helps...

    $up =new Upload();
    $bUploadNew = false;

    if (!empty($data['Photo']['name']))
       $bUploadNew = true;

    if ($bUploadNew) {
       $img = Object::create('Image');
       $up->loadIntoFile($data['Photo'], $img, 'Uploads');
       if($up->isError()) {
          $form->addErrorMessage('Logo','error','required');
          $bValid = false;
       }
    }

    if (!$bValid) return Director::redirectBack();

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