This is my form, I have a radio button from 1 to 5 (very bad to very good)
parent::__construct($name);
$this->setAttribute('method', 'post');
$this->add(array(
'type' => 'Zend\Form\Element\Radio',
'name' => 'rate_box',
'options' => array(
'label' => 'Please choose your rate',
'value_options' => array(
'1' => ' Very Bad',
'2' => ' Bad',
'3' => ' Fine',
'4' => ' Good',
'5' => ' Very Good',
),
),
'attributes' => array(
'value' => '1' //set checked to '1'
)
));
$this->add(array(
'name' => 'submit',
'type' => 'Submit',
'attributes' => array(
'id' => 'submit',
'class' => 'btn btn-primary',
),
));
This is the related part of my controller
$form = new VoteForm();
$request = $this->getRequest();
if ($request->isPost())
{
$form->setData($request->getPost());
if ($form->isValid())
{
$formdata = $form->getData();
$vote = new Vote();
$data = $vote->getArrayCopy();
$data['user_id'] = $user_id;
$data['voted_user_id'] = $voted_user_id;
$data['ratescore'] = $formdata['rate_box']; //Here I take the value of radion button
$vote->populate($data);
try
{
$this->getEntityManager()->persist($vote);
$this->getEntityManager()->flush();
return $this->redirect()->toRoute('home',array('user_id' => $user_id,
'action' => 'home',
));
}
catch(DBALException $e){
}
}
}
Why I can't retrieve the value of "rate_box" and save to my $data['ratescore']? Thank you!
I just had this problem few hours ago. You can try to retrieve the value of radio button using the native $_POST['rate_box'] in your action. So, in your controller :
public function yourAction() {
$form = new VoteForm();
if($this->getRequest->isPost()) {
$data = $this->getRequest->getPost();
if($form->isValid($data)) {
$rate = $_POST['rate_box'];
// another code..
}
}
}
Hope this helps.. :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.