1

I'm a little confused about how I can amend the following code that generates a form to allow for the $Show->DateTimes() to automatically generate a set of new fields and populate the price depending on how many dates are available.

I'm not sure if this is even possible. Although I have added the foreach inside the comments explaining how I think it should work, I think I just have my PHP mixed up.

I have this code at the moment:

EDIT: Final fixed code:

public function RegistrationForm() {
    $date_id = (int) $this->getRequest()->requestVar('DateID');

    if(!$date = DataObject::get_by_id("ShowDateTime", $date_id)) {
        return $this->httpError(404);
    }

    $date_map = array();
    if($Show = $date->Show()) {
        if($all_dates = $Show->DateTimes()) {
            $date_map = $all_dates->toDropdownMap('Price','DateLabel');
        }   
    }

    $fields = new Fieldset (
        new TextField('Event', _t('Show.Event','Name of Event'),$Show->Title),
        new EmailField('Name', _t('Show.Name','Name')),
        new EmailField('Email', _t('Show.Email','Email')),
        new TextField('Address', _t('Show.Address','Address')),
        new TextField('Telephone', _t('Show.Telephone','Telephone')),
        new DropdownField('DateID', _t('Show.CHOOSEDATE','Choose a show'), $date_map, $date_id)         
    );

    // Loop through the time/events creating a fieldset for: class,horse,number,price
    $i =0;
    foreach($all_dates as $show_price){
        $fields->push(new TextField('Class_'.$i, 'Class'));
        $fields->push(new TextField('Horse_'.$i, 'Horse'));
        $fields->push(new TextField('Number_'.$i, 'Number'));
        $fields->push(new CurrencyField('Price_'.$i, 'Price',$show_price->Price));
        $i++;
    }


    $form = new Form (
        $this,
        "RegistrationForm",
        $fields,        
        new FieldSet (
            new FormAction('doRegister', _t('Show.REGISTER','Register'))
        ),
        new RequiredFields('Event','Name','DateID')
    );

    return $form;
}

1 Answer 1

2

Inside the $form = new Form ( you can not place a loop expression. You can only put simple expressions in there, like a $variable or new. But nothing that needs a higher level of execution.

So first generate a list of the objects into variables of it's own (or one array) and then add it to $form.

The Form constructor expects to get a FieldSet object containing the fields. You can create a new one and add the fields to it and then pass this object into the constructor. Consider the following, alternative approach:

   $controller = $this;
   $name = 'RegistrationForm';
   $fields = new FieldSet();
   // add fields to $fields, setup $actions ...
   $form = new Form($controller, $name, $fields, $actions);
Sign up to request clarification or add additional context in comments.

3 Comments

I tried this first although create a new Fieldset() only works from inside the new Form()
Ok thanks I see I only need one Fieldset although I still cant figure out how to append a loop of fields to the new Fieldset()
Try with $fields->push(new XXXField(...)); - docs are here: api.silverstripe.org/2.4/forms/fields-structural/FieldSet.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.