mardi 17 juillet 2018

Problems with class extending UserDefinedForm in Silverstripe 4

I've been stuck on this for a while now and I can't see where I'm going wrong.

I've made a class that extends the UserDefinedForm so I can have a page that also has a UserDefinedForm on it. The form loads on the page, but it isn't as easy as just saying $Form on the template file, instead I have to make and call the following function:

public function getUserForm() {
    $page = UserDefinedForm::get()->byID($this->ID);
    $controller = UserDefinedFormController::create($page);
    return $controller->Form();
}

So when I call that function on the template it displays the form, however, none of my field rules are applied and when submitting the form it takes me to a blank page with "/finished" appended to the URL: "https://example.com/finished".

Can someone please help me out here, would be much appreciated. I will put my code down below.

Class extending the UserDefinedForm:

  <?php
    use SilverStripe\Forms\FieldList;
    use SilverStripe\Forms\TextField;
    use SilverStripe\Forms\FileHandleField;
    use SilverStripe\Core\Injector\Injector;
    use SilverStripe\Assets\Image;
    use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
    use SilverStripe\Forms\GridField\GridField;
    use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
    use SilverStripe\Forms\DropdownField;
    use SilverStripe\UserForms\Model\UserDefinedForm;
    use SilverStripe\UserForms\Control\UserDefinedFormController;
    use SilverStripe\UserForms\Model\EditableCustomRule;

    class Package extends UserDefinedForm {

    private static $db = [
        'Date' => 'Text',
        'Location' => 'Text',
        'Availability' => 'Enum(array("Available","Hidden","Sold Out"))',
        'Extras' => 'HTMLText',
        'NeedTo' => 'HTMLText',
        'Price' => 'Text'

    ];

    private static $has_one =[
        'Photo' => Image::class,

    ];

    private static $has_many =[
        'FileAttachments' => 'PackageFile'
    ];

    private static $many_many = [
        'SacredTexts' => 'ImportantText'
    ];

    public function getCMSFields() {
        $fields = parent::getCMSFields();
        $fields->addFieldToTab('Root.Main', Injector::inst()->create(FileHandleField::class, 'Photo'));
        $fields->addFieldToTab('Root.Main', new TextField('Date', 'Date'));
        $fields->addFieldToTab('Root.Main', new TextField('Location', 'Location'));
        $fields->addFieldToTab('Root.Main', new TextField('Price', 'Price'));
        $fields->addFieldToTab('Root.Main', new HTMLEditorField('Extras', 'Extras'));
        $fields->addFieldToTab('Root.Main', new HTMLEditorField('NeedTo', 'What you need to know'));
        $fields->addFieldToTab('Root.Main',  new DropdownField('Availability', 'Availability',  singleton('Package')->dbObject('Availability')->enumValues()));
        $fields->addFieldToTab('Root.Main', new GridField('SacredTexts', 'Important Texts',  $this->SacredTexts(), GridFieldConfig_RecordEditor::create()),'Date');
        $fields->addFieldToTab('Root.Main', new GridField('FileAttachments', 'File Attachments',  $this->FileAttachments(), GridFieldConfig_RecordEditor::create()),'Content');
        return $fields;
    }
    /* Look up SS4 docs on SS Sitetree URL parse function and what needs to be namespaced */
    function onBeforeWrite () {
        parent::onBeforeWrite ();
        if($this->Name){
            $this->Slug =  str_replace(' ','-', strtolower($this->Name));;
        }
    }

    public function ShortContent( $word_limit = 20 ) {
        $NoHTML = htmlspecialchars_decode(strip_tags($this->NewsText),ENT_QUOTES);
        $words = explode( ' ', $NoHTML );
        return implode( ' ', array_slice( $words, 0, $word_limit ) );
    }

    public function ParentEvent(){
        return $this->Parent()->URLSegment;
    }

    public function getUserForm() {
        $page = UserDefinedForm::get()->byID($this->ID);
        $controller = UserDefinedFormController::create($page);
        return $controller->Form();
    }

    public function hasUserForm() {
        if (count($this->getUserForm()->Fields()) > 1) {
            return $this->getUserForm();
        }
    }
    }




Aucun commentaire:

Enregistrer un commentaire