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.

Binding form to model

Hi assume that I've got User Model and Registeration Form and I want to show filled form with current user information but some fields have different name in my form. in my Users model class I've got user_name but in my RegisterForm, I've got name when I bind these 2 together, how can I set value for different property?

$user = Users::findfirst();
$form = new RegisterForm($user);

thanks



33.9k
Accepted
answer
edited Sep '15

You can try this.

Form:

    private $edit;

    public function initialize($entity = null, $options = null)
    {
        if (isset($options['edit'])) {
            $this->edit = $options['edit'];
        }

        $name = new Text('name');
        if (true === $this->edit && !$this->request->isPost()) {
            $name->setDefault($entity->name);
        }

        $this->add($name);   
    }

And your code:

    $user = Users::findfirst();
    $form = new RegisterForm($user, [
        'edit' => true
    ]);


3.9k

thanks man. by the chance, do you have any idea about this Dynamic Form Fields Validation ? :)

You can try this.

Form:

```php private $edit;

public function initialize($entity = null, $options = null) { if (isset($options['edit'])) { $this->edit = $options['edit']; }

   $name = new Text('name');
 if (true === $this->edit && !$this->request->isPost()) {
     $name->setDefault($entity->name);
 }

 $this->add($name);

} ```

And your code:

```php $user = Users::findfirst(); $form = new RegisterForm($user, [ 'edit' => true ]); ```