We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Catchable fatal error: Object ... could not be converted to string. Form select does not work with entity

Hello! I'm trying to use one Form for both "Add" and "Edit" Post pages. According to this docs page I use a Posts model to set default values in the form’s elements. Sadly something went wrong with Select element.

BlogPostConroller (EditAction):

  $form = new Form($post);

Form:

class AddBlogPostForm extends Form
{

public function initialize(\Project\Models\BlogPosts $post = null)
{

    $title = new Text('title', [
        'placeholder' => 'title'
    ]);

    $title->setLabel('Title of the post');

    $title->addValidators([
        new PresenceOf([
            'message' => 'no title'
        ]),
        new StringLength([
            'min' => 12,
            'messageMinimum' => 'Very short title'
        ]),
    ]);

    $title->setFilters(['trim', 'string']);

    $this->add($title);

    $select = new Select("type", BlogTypes::find(),
            [
                "using" => [
                    "id",
                    "name",
                ]
            ]
        );

     $select->addValidator(new PresenceOf([
            'message' => ' Choose the post type'
        ]));

     $this->add($select);

    $this->add(new Submit('Add'));
    }

}

I'm getting Catchable fatal error: Object of class Project\Models\BlogTypes could not be converted to string in \app\cache\volt\app_views_blog_post_edit.volt.php on line 19 which referrences to

$this->form->render('type')

How does Form + entity interact with Select Element? How to solve my issue?

Your select should be like that :

$select = new Select([
            "type", 
            BlogTypes::find(),
            "using" => [
                 "id",
                 "name",
            ]
]);