Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
92 views
in Technique[技术] by (71.8m points)

php - how to read default values in a symfony 1.4 form?

I have a symfony form that its initialized with this code

$this->form =new MyForm();
$this->form->setDefault('myValue',"value");

now if I go to MyForm.class.php and execute

echo $this->getObject()->get('myValue');
die();

on configure() method, it doesnt print anything. Same happens in BaseMyForm.class.php.

Am I doing something wrong?

if I print the entire widget in the html with

<?php echo $form['myValue']->renderError() ?>
<?php echo $form['myValue']->render() ?>

it loads correctly.

question from:https://stackoverflow.com/questions/65541410/how-to-read-default-values-in-a-symfony-1-4-form

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It seems I was wrong about how form builder works, when you pass data through $this->form->setDefault(); it is not loaded in the configure method. If you need to pass data to your configure method you have to do it through your builder, in my case it would be something like:

$this->form =new MyForm(array(),array("MyValue","value");

and then to read it you need to use

$this->getOption('MyValue');

in your configure method

seen on Pass a variable to a Symfony Form


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...