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

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



51.2k
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
    ]);


8.4k

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

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
  ]);