Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Form

<?php

use Phalcon\Forms\Form; use Phalcon\Forms\Element\Text; use Phalcon\Forms\Element\Hidden;

class ContactForm extends Form { public function getCsrf() { return $this->security->getToken(); }

public function initialize()
{

    $this->setEntity($this);

    // Add a text element to capture the 'email'
    $this->add(new Text("email"));

    // Add a text element to put a hidden CSRF
    $this->add(new Hidden("csrf"));
}

} what $this->setEntity($this) ?. Please help me.I do not understand, please give me an example

And what do you want to achieve?

I do not understand $this->setEntity($this); in document phalcon chapter Form. I want to example.

This sets up self as the internal entity. If you will use

$form->bind($data);

it will bind data it to that form as public attributes.

I do not understand, please give me an example with detail.



3.7k
Accepted
answer

Here is an example of setting POST data to the $user entity.

public function initialize(User $user, $options)
{
    $this->setEntity($user);

Doing the following populates the entity Models\User with $_POST data.

// within a controller
// $user is an instance of Models\User
$form->bind($this->request->getPost(), $user);

If you set any sanitizers in your form, when you bind data to the entity, the sanitizers are run first, the sanitized data is then populated into the entity, in this case Models\User.

So then you can save the entity

// within a controller
$form->bind($this->request->getPost(), $user);

if ($this->request->isPost() && $form->isValid($this->request->getPost())) {
    $user->save();
}