Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Form Questions /

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

SS3 - Front end UploadField issue


Go to End
Reply


2 Posts   818 Views

Avatar
Spambanjo

Community Member, 18 Posts

27 October 2013 at 4:28am

Edited: 27/10/2013 4:39am

I'm having a problem with a front end UploadField.

I am creating a large SiteConfig extension module designed to give added functionality to various collections of general information such as people, email addresses, telephone numbers, etc, using individual data objects for each element. I then have, for example, a contact page type which lists people, telephone, etc, assigned to the site config.

I have added a $has_one = array('Person'=>'Person') relationship to member. Person then $has_one = array('Image'=>'Image') which is working fine from the CMS. I'm now trying to create a the field on a front end form of my new FrontEndMembers module to allow members to edit their avatar image.

I've stripped everything down to a bare minimum figure this out. I'm currently getting a "SyntaxError: JSON.parse: unexpected character" error on the field when I try to upload.

Here's the code for the Member and Person:

class Person extends DataObject {
    static $has_one = array(
        'Image' => 'Image'
    );
}
class MembersMemberExt extends DataExtension {
    static $has_one = array(
        'Person' => 'Person'
    );
}

And the form:

class MemberPage_Controller extends Page_Controller {
    public function Form(){
        if($m=Member::currentUser()){
            if($this->urlParams['Action']=='image'){ 
                    $f = new FieldList();
                    $pf = new UploadField('Image','Image',$m->Person()->Image()) ;
                    $pf->setConfig('allowedMaxFileNumber', 1); 
                    $pf->setRecord($m->Person());
                    $path = preg_replace('/^' . ASSETS_DIR . '\//', '', 'UserUploads/' . $m->ID);
                    $pf->setFolderName($path);    
                    $pf->setCanAttachExisting(false);
                    $pf->setCanPreviewFolder(false);
                    $pf->setCanUpload(true);
                    $pf->setTitle('Image');
                    $pf->getValidator()->setAllowedExtensions(array('gif','jpg','png','jpeg'));
                    $f->push($pf);
                    $a = new FieldList(
                        new FormAction('saveimage', 'Save')
                    );
                return new Form($this, 'saveimage', $f, $a);
            }
        } else {
            // ...
        }
    } else {
        // ...
    }
}

I've done a lot of Googlging and looked at other similar problems but I just can't see where I'm going wrong. I'm going to revert to a FileField for now so that I can continue the project but I would love to get this working ideally, so any help would be appreciated.

Cheers.

Avatar
Spambanjo

Community Member, 18 Posts

21 November 2013 at 12:13am

Is there anywhere else I can go to get help with this? I assumed the official forum would be the obvious choice but I never seem to get any responses when I ask questions.

This should be simple :(