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

[SOLVED]How to get value from hidden element using Phalcon Forms and Volt?

Hello experts,

How to get value from hidden element using Phalcon Forms and Volt?

SalesForm.php

class SalesForm extends Form
{
    public function initialize($entity=null, $options=null)
    {       
        $this->add(new Hidden('productIdList', array('value'=>'12345')));
    }
}

sales.volt


// I tried this method, but it show no value
{{ form.getValue("productIdList") }}

I've read the API doc. But still no luck finding appropriate property or method.

Thank you.

Jim

edited Oct '14

Why not just use render() ?

{{ form.render('productIdList') }}

@Gigih

I can't. Because sometimes I need to parse the value inside 'productIdList'. I don't need <input type="hidden" name="productIdList" id="productIdList" value="123|456" />, I just need the value, which mean '123|456'.



1.5k
Accepted
answer

Hmm, if you just want to print variable, I think the simple $this->view->setVar('produdctIdList', ...) and {{ productIdList }} just more prefererable than just using Form. Anyway, all element in form will only have value after you set the entity object inside the Form object.

<?php

use Phalcon\Forms\Form;
use Phalcon\Forms\Element\Hidden;

class SalesForm extends Form
{
  public function initialize($entity=null, $options=null)
  {    
    $this->add(new Hidden('productIdList'));
  }
}

$form = new SalesForm((object)['productIdList' => 20]);
echo $form->getValue('productIdList'); /// should print 20

@Gigih

Thanks. I've followed your solutions.