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

How do you change the name used for input elements ?

Suppose that we create a form element using:

$lastname = new Text("lastname",array('placeholder' => 'Lastname','name' => "customerLastname"));

This will create a form input element with the name customerLastname, so far so good. But when you bind the $_POST variable to the form it will not recognise the field. Any ideas how to name a field differently in the form then how it was defined in the model ?



1.4k

why not do this ? $lastname = new Text("customerLastname",array('placeholder' => 'Lastname'));



1.5k

Because in the model Customers the field is called lastname, which will result in the value not being updated in the model when you do something like:

$form->setEntity($customer);

The reason for this question is that I have 2 entities of a different type with identical field names in one form.... So I have to give one of them a different name but I don't know how to fix this.



1.4k

$lastname = new Text("aNameIwillChange",array('placeholder' => 'Lastname')); $lastname->setName('customerLastname');

also seems to work.

but what you do here $lastname = new Text("lastname",array('placeholder' => 'Lastname','name' => "customerLastname")); Though the name will change the id of the element won't. No sure if that matters.



1.4k

i see sorry ... i didn't understand it right