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 element can't get value

I have a form with one element named 'title', and code is

<?php
$form = new MyForm();
$form->bind(['title' => '1122'], $form);
foreach ($form->getElements() as $v) {
 echo $v->getName().':'.$v->getValue();
}

the output is "title:1122"

but when I use this code in post method

<?php
if ($this->request->isPost()) {
  $form = new MyForm();
  $form->bind($this->request->getPost(), $form);
  foreach ($form->getElements() as $v) {
   echo $v->getName().':'.$v->getValue();
  }
}

I can't get the value, even I change code like this

<?php
if ($this->request->isPost()) {
  $form = new MyForm();
  $form->bind(['title' => '1122'], $form);
  foreach ($form->getElements() as $v) {
   echo $v->getName().':'.$v->getValue();
  }
}

I still can't get the value, I must write $form->getValue($v->getName()) to get the value. that make me crazy.



33.8k

Do:

echo '<pre>';
var_dump($this->request->getPost());
die();

To see what values it returns.



6.7k

is array( 'title' => 'the value I post' )

the post value has no problem. even I use hard-code array value, I still can't get the value by use $v->getValue. that's strange.

Do:

echo '<pre>';
var_dump($this->request->getPost());
die();

To see what values it returns.



33.8k

But you get the name of the field, isn't it? Maybe the INPUT used for title isn't inside the form of the view. Or the form's method isn't POST. Tell more.



6.7k

I have read cphalcon's code, and the "getValue" function is

public function getValue()
    {
        var name, form, value;

        let name = this->_name,
            value = null;

        /**
         * Get the related form
         */
        let form = this->_form;
        if typeof form == "object" {

            /**
             * Check if the tag has a default value
             */
            if !\Phalcon\Tag::hasValue(name) {

                /**
                 * Gets the possible value for the widget
                 */
                let value = form->getValue(name);
            }

        }

        /**
         * Assign the default value if there is no form available
         */
        if typeof value == "null" {
            let value = this->_value;
        }

        return value;
    }

I think the problem is

if !\Phalcon\Tag::hasValue(name)

It check the $_POST variable, if there has value, the "getValue" method can't return the form's data.

But you get the name of the field, isn't it? Maybe the INPUT used for title isn't inside the form of the view. Or the form's method isn't POST. Tell more.



33.8k

That checks if it doesn't have a default value.

BTW, MyForms is a personalized class? If it is, show me its class definition. Because your first code block doesn't work (I tried it). I just get that the form doesn't have elements.



6.7k

here is the code

<?php
use Phalcon\Forms\Form;
use Phalcon\Forms\Element\Text;

class MyForm extends Form {
    public function initialize() {
        $this->add(new Text('title'));
    }
}

That checks if it doesn't have a default value.

BTW, MyForms is a personalized class? If it is, show me its class definition. Because your first code block doesn't work (I tried it). I just get that the form doesn't have elements.



2.1k

Your codes are wrong.

First all all default value for forms are passed in via constructor

https://docs.phalcon.io/en/latest/reference/forms.html Forms + Entities

form->bind is used to transfer the data from the form to the model.

it is used as such

form->bind(model,$_POST);

form should also use

function getTitle() { return "default title"; }

but currently in 2.0 it is bugged, i submitted a fix but not sure when/or if it will get pulled.

Also you can set default value in element.

$title = new Text("title"); $title->setDefault("My default title");

$form->add($title); // $this->add($title) if you are in the form class



6.7k

I just want to get the post data which is validated by form, and there is no model to bind, so I bind to the form itself. I use foreach to get the element so that I can make sure all the element is correct, because the $_POST is unsafe. The problem is why I use $form->getValue() can get the value, but $element->getValue() can't? In my opinion, $form is container, it cantain many elements, when it use bind method, all the value must store in form's elements and Entities.

Your codes are wrong.

First all all default value for forms are passed in via constructor

https://docs.phalcon.io/en/latest/reference/forms.html Forms + Entities

form->bind is used to transfer the data from the form to the model.

it is used as such

form->bind(model,$_POST);

form should also use

function getTitle() { return "default title"; }

but currently in 2.0 it is bugged, i submitted a fix but not sure when/or if it will get pulled.

Also you can set default value in element.

$title = new Text("title"); $title->setDefault("My default title");

$form->add($title); // $this->add($title) if you are in the form class

i had used element.getAttribute('value') in volt, or $element->getAttribute('value')