Trouble creating form interface on front end
Created 7 years ago by emergingdzns

I've got a new module I'm building for a client. I've got an admin interface using the streams platform stuff going. But I wanted to create a form interface for their users to enter the data and submit.

Let's say the module is called Mymodule. The stream is called Guide.

I've got a Mymodule\Guide\Form\GuideFormBuilder.php file. I have a method in my front end controller that loads the view, which has {{ form('guide') }} in it.

It properly routes the page and tries to load the form as defined in the form builder array. However I'm getting the following error:

Method Anomaly\Streams\Platform\Ui\Form\FormCriteria::__toString() must not throw an exception, 
caught Exception: Field type [] not found.

Here's the form builder file:

<?php namespace ClientName\Mymodule\Guide\Form;
use Anomaly\Streams\Platform\Ui\Form\FormBuilder;
class GuideFormBuilder extends FormBuilder
{

    protected $model = false;

    protected $roles = [
        'user',
    ];

    /**
     * The form fields.
     *
     * @var array|string
     */
    protected $fields = [
        'name' => [
            'instructions' => 'Give this Health Guide a name. This is for your purposes only in the event you build more than one Custom Health Guide',
        ]
    ];
    protected $buttons = [];
    protected $actions = [];
    protected $options = [];
    protected $sections = [];
    protected $assets = [];
}

I don't know what to do here.

emergingdzns  —  7 years ago

FYI, for the moment, there's just one field in the form builder. Trying to simply get a single text field to appear on the page. I've got a bunch more fields but I'm not sure what I'm doing wrong.

ryanthompson  —  7 years ago

Looks like you have protected $model = false; which means you would have to define the FULL field definition http://pyrocms.com/documentation/streams-platform/v1.1#ui/forms/builders/basic-usage/formbuilder-fields.

As it sits you have no model to refer to for Pyro to guess things / pull information and you have only slug / instruction for the field definition of the form.

If you would like to experiment without the form just try using this for fields:

protected $fields = [
        'name' => [
            'type' => 'text',
            'instructions' => 'Blah',
        ]
    ];
emergingdzns  —  7 years ago

Thanks Ryan. That helped. A little anyway...

I added the class like this: protected $model = GuideModel::class;

I was still getting an error, so I did a reinstall, just to be sure. Then it worked! The page came up showing the one field. So I then added in the other fields such that it now the GuideFormBuilder.php looks like this:

<?php namespace ClientName\Mymodule\Guide\Form;

use Anomaly\Streams\Platform\Ui\Form\FormBuilder;
use ClientName\Mymodule\Guide\GuideModel;

class GuideFormBuilder extends FormBuilder
{

    protected $model = GuideModel::class;

    protected $roles = [
        'user',
    ];

    /**
     * The form fields.
     *
     * @var array|string
     */
    protected $fields = [
        'name' => [
            'instructions' => 'Give this Guide a name. This is for your purposes only in the event you build more than one Custom Guide',
        ],
        'age_group' => [
            'instructions' => false,
        ],
        'gender' => [
            'instructions' => false,
        ],
        'current_weight' => [
            'instructions' => 'Enter your weight in lbs.',
        ],
        'height_feet' => [
            'instructions' => 'Enter your height in whole feet',
        ],
        'height_inches' => [
            'instructions' => 'Enter the number of inches taller than your foot height above',
        ],
        'exercise_frequency' => [
            'instructions' => false
        ],
        'exercise_duration' => [
            'instructions' => false
        ],
        'servings_fruit' => [
            'instructions' => false
        ],
        'servings_protein' => [
            'instructions' => false
        ],
        'reduce_weight' => [
            'instructions' => false
        ]
    ];

    /**
     * Fields to skip.
     *
     * @var array|string
     */
    protected $skips = [];

    /**
     * The form actions.
     *
     * @var array|string
     */
    protected $actions = [
        'blue' => [
            'text' => 'Next &raquo; Select Health Conditions',
        ],
    ];

    /**
     * The form buttons.
     *
     * @var array|string
     */
    protected $buttons = [];

    /**
     * The form options.
     *
     * @var array
     */
    protected $options = [
        'redirect'          => '/',
        'success_message'   => 'Health Guide Information Saved'
    ];

    /**
     * The form sections.
     *
     * @var array
     */
    protected $sections = [];

    /**
     * The form assets.
     *
     * @var array
     */
    protected $assets = [];

}

The GuideFormFields.php file looks like this:

<?php namespace ClientName\Mymodule\Guide\Form;

use Illuminate\Contracts\Config\Repository;

class GuideFormFields
    {

    /**
     * Handle the fields.
     *
     * @param GuideFormBuilder $builder
     * @param Repository       $config
     */
    public function handle(GuideFormBuilder $builder, Repository $config)
    {
        $builder->setFields(
            [
                'name',
                'age_group',
                'gender',
                'current_weight',
                'height_feet',
                'height_inches',
                'exercise_frequency',
                'exercise_duration',
                'servings_fruit',
                'servings_protein',
                'reduce_weight'
            ]
        );
    }
}

But then I tried loading the page and I get the same error about toString thing. I tried doing a re-install and it still errors. I'm assuming I've made some minor error in programming somewhere.

Thanks again for the help!

emergingdzns  —  7 years ago

I found the problem. There was one field I was asking for in the form builder that I neglected to add to the assignments in the migration.

Thanks again!