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.

Assigning Label to form this way

I need a small clarification for adding element in a Form class, generally becasue I am still trying to wrap my head around OOP...

We can declare element in form in two ways :

class SomeForm extends Form
{

    public function initialize($entity = null, $options = null)
    {

     #First Way
        $name = new Text('name', [
            'placeholder' => 'Name',
            'otherProperty' => 'Property'
        ]);

        $name->setLabel('Name');

        $this->add($name);

        #Second Way
             $this->add(new Text('name', [
            'placeholder' => 'Name',
            'otherProperty' => 'Property'
        ]));
   }
}

I understand that first one is more flexible while second one is shorter. My question is that can we add HTML Label to element in "second way" ?

Yes, you can. But imo, you should stick with First way, second way may save you space and a variable, but will waste your time when you come back after 1 month and try reading your code ;)

// Its like writing conditions like:
if (condition)
    blabla;

// Even worse
if (condition) blabla;

// Instead of simply doing:
if (condition) {
    blabla;
}

The second way should look something like this:

$this->add((new Text('name', [
    'placeholder' => 'Name',
    'otherProperty' => 'Property'
]))->setLabel('Name'));

As you can see, it looks terrible and saves you nothing :)



1.7k
Accepted
answer

Way one

  $name =new Text( 'name', [....] );
  $name->setLabel( ..... );
  $name->setDefault( .... );

  $form->add( $name );

Way two. Note the mising ; at the end of the second line except the last. This why you can chain your calls. You can place the chained commands below or on one line. Whatever you find more appealing to you.

  $name =new Text( 'name', [....] );
  $name->setLabel( ..... )
    ->setDefault( .... );

  $name->setLabel( ... )->setDefault( ... );

  $form->add( $name );

Way three. Note the extra () around the new call. This means that this is executed first and after that setLabel and setDefault are called

  $form->add(( new Text( 'name', [....] ))
    ->setLabel( ..... )
    ->setDefault( .... )
  );

Here an example that I use myself. It is a small form that I use to manage menu's in my application. I used the first way myself when I started with phalcon, but I have switched to the way below. I find it easier to read and also makes things a little compacter.

<?php
  namespace Modules\menus\Forms;

  use Backend\Common\BaseForm;
  use Phalcon\Forms\Element\Text;
  use Phalcon\Forms\Element\Hidden;
  use Phalcon\Validation\Validator\PresenceOf;

  class MenuForm extends BaseForm
  {
    public function initialize( $entity=null, $options=null )
    {
      parent::initialize( $entity, $options );

      // Id
      $this->add(( new Hidden( 'id' )));

      // Name
      $this->add(
        ( new Text( 'name' ))
        ->setLabel( 'Name' )
        ->addValidators([
          new PresenceOf([ 'message' => 'Name is required' ]),
        ])
      );

      // Label
      $this->add(
        ( new Text( 'label' ))
        ->setLabel( 'Label' )
        ->addValidators([
          new PresenceOf([ 'message' => 'Label is required' ]),
        ])
      );
    }
  }

As for how you should structure your code is up to you. You can make up your own structure that you are comfortable with or you could adhere to PSR standards out there. Experiment with them if you like to get an idea what suits you the best.



2.0k
edited May '18

Thanks @Hoet and @Nikolay-Mihaylov Mihaylov for nice clarification.

Actually, for me Method 1 looks easier for now. I think its more about how much your brain is in aquitance with various form of code. Ofcourse you explained it very nicely here, specially third way you mentioned, I had actually tried chainning that way but was missing the logic that first object has to be created then only it can be chained.

PS: I accidently posted multiple duplicated, evertime I was submitting, it was showing ERROR 500 , even after interval of several minute, there was ERORR 500 after submitting post , I thought they were failed but not, they all went through.

And yet again, I received ERROR 500 when I submitted this post, no confirmation or anything. When I reloaded this page, comment is here.

Forum has some issues at the moment, should be fixed soon. Just click "back" on your browser when submitting post or comment.