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

Form -> Item -> set the label in the constructor class

Hi guys!

This is my first post in the forum.

First, congratulations to Phalcom Team by this fast and promising framework, we are testing this excellent work for our future applications.

I have a doubt... (well...a lot of them :) ) about the label of items forms.

It's possible set the label using the constructor class??, I mean:

$form = new Form(); $form ->add(new Text('name_in_database', array( 'maxlength' => 255, 'placeholder' => 'Write your name, please', 'required' => 'required', 'label' => Name )));

Because, that not work. I must set the label in this way:

$form = new Form(); $form ->add(new Text('name_in_database', array( 'maxlength' => 255, 'placeholder' => 'Write your name, please', 'required' => 'required' ))); $form ->get('name_in_database')->setLabel('Name');

That works.

Thanks very much!



98.9k

Hi,

Try adding the element this way:

$form = new Form();

$element = new Text('name_in_database', array(
    'maxlength' => 255,
    'placeholder' => 'Write your name, please',
    'required' => 'required'
));

$element->setLabel('Name');

$form->add($element);


6.0k

Hi Phalcon ;) , thanks for your quickly reply.

Yes, you are right, that works, but my question is: Can i do that using the constructor class?, something like:

$form = new Form();
$element = new Text('name_in_database', array(
  'maxlength' => 255,
  'placeholder' => 'Write your name, please',
  'required' => 'required',
  'label' => 'My label'
));
$form->add($element);

This functionality not exist? I'm right?

ps: Sorry for my awfull english :(



8.1k

I'm sorry for my english, but : Every class Phalcon\Forms\Element extended create the structure of one tag (text, hidden, date email etc.). You want to generate the two entities in one class, but this is unacceptable. But you are not limited with Phalcon. You can always rewrite the class and add the necessary functionality. But, when you combine two entities, labael and input, in one class, you'll have to hide from other programmers working with you. :)



6.0k

Hi Oleg!

I understand you, but I dont know that "Label" its a different class of Phalcon\Forms\Element , maybe I'm wrong... but the label of an element not is a property member??, it's a different class?, really? , not exists "Phalcon\Forms\Label", you know?

Thanks!



8.1k

Of course. Label is a property member of Phalcon\Forms\Element. But, when we generate form, we will have want or not to display tag label. In additionally, we must dissociate entities - this is the best practice. Therefore, class Phalcon\Forms\Element have special method to generate label tag of element -

public string label () /*Generate the HTML to label the element*/.

For example, i use hidden fields in my forms, and I don't generate label tag for these fields. And I'll say it again - Phalcon doesn't have frozen structure, you can change any behavior of Phalcon practically. You can see this example in Russian blog https://habrahabr.ru/post/197254/



6.0k

Ok Oleg, I understand you, I can encapsulate this functionality ;)