Submit Two forms in one controller - php

How can I make sure that, that second form elements go to the database cause when I check there the is no data but its been inputted by the user, if both forms are called from one controller
$form = new Form_Form1();
$this->view->form = $form;
$form2 = new Form_Form2();
$this->view->form2 = $form2;
if ($this->getRequest()->isPost()) {
$formData = $this->getRequest()->getPost();
if ($form->isValid($formData)) {

Try this:
if ($form->isValid($formData) && $form2->isValid($formData)) {...

Related

Zend framework session data to form fields

I'm working on a multistep form and also there are previous buttons in that. When a user clicks the previous button they go a step back as expected, but the form is not filled with the data that is already filled in that step.
The data is stored in a session, so i thaught this wil work (in the controller):
if($this->getRequest()->isPost()) {
if ($this->getRequest()->getPost('previous')){
$data = $this->sessionContainer->PlaatsenAdvertentie;
}
else{
$data = $this->params()->fromPost();
}
$form->setData($data);
$viewModel = new ViewModel([
'form' => $form
]);
return $viewModel;
}
But no...
I made a function inside the form class:
public function populate($step,$data)
{
foreach($data['step'.$step] as $field => $value){
//uitgezond de submit en vorigestap buttons
if ($field != 'submit' && $field != 'vorigestap'){
$this->get($field)->setValue($value);
}
}
return $this;
}

Change Symfony form action once the request has been handled

I got a Symfony 2 form that has been created using createForm. Once the form has been validated I need to modify the action I initially set. Is this possible?
$formData = $this->loadData($id);
// Form builder
$form = $this->createForm(new ComposeForm(), $formData, [
'action' => $$this->generateUrl('defaultAction')
]);
// Processing form
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$myVar = $form->get('myVar')->isClicked();
if ($myVar) {
// Can I change the form action here??
}
}
It's quite straight forward in a controller :
if ($myVar) {
$form->setAction($this->generateUrl('target_route'))
}
But I dont see the point of doing so.

Validation for Add screen not working but it working in edit screen with same code

I am learning Zend framework and currently I have created add, update , delete functionality for country name and continent name and it is working perfectly.
I have set validation by
$name->setRequired('true');
and
$continent->setRequired('true');
in my form.php.
Validation is working in edit form but it return error 'An error occurred' and 'Application error' in add form.
Below is my controller code:
for Add:
/*Add Record into Database*/
public function addAction()
{
$form =new Application_Form_Add();
$form->submit->setlabel('Add Country');
$this->view->form = $form;
if($this->getRequest()->ispost())
{
$formData = $this->getRequest()->getpost();
if($form->isvalid($formData))
{
$file = new Application_Model_Country();
$name = $form->getvalue('name');
$continent = $form->getvalue('continent');
$file->addCountry($name, $continent);
$this->_helper->redirector('index');
}
else
{
$this->populate($formData);
}
}
}
for Edit:
/*Edit Record into Database*/
public function editAction()
{
$form = new Application_Form_Edit();
$form->submit->setlabel('Edit Country');
$this->view->form = $form;
if($this->getRequest()->ispost())
{
$formData = $this->getRequest()->getpost();
if($form->isvalid($formData))
{
$id = $form->getvalue('country_id');
$name = $form->getvalue('name');
$continent = $form->getvalue('continent');
$file = new Application_Model_Country();
$file->updateCountry($id,$name,$continent);
$this->_helper->redirector('index');
}
else
{
$form->populate($formData);
}
}
else
{
$id = $this->getRequest()->getparam('country_id');
if($id >0)
{
$formData = $this->getRequest()->getpost();
$file = new Application_Model_Country();
$files = $file->fetchRow('country_id='.$id);
$form->populate($files->toArray());
}
}
}
Both code are same, then why validation not working in add form?
You need to change following code in the addAction logic:
instead of:
$this->populate($formData);
use
$form->populate($formData);
The reason is $this means Action object in this context and you have correctly used $form object in EditAction so it is working properly, so it is kind of silly typing mistake.
PS: you should also use proper case in method names like isPost, isValidate etc. otherwise may get errors in Linux environment.

zf2 repopulate form with filtered values

I moved a few weeks ago from zf1 to zf2 and when I am creating forms I am getting confused.
Is there an option that values from form after being filtered going to repopulate form?
What you need is this:
$request = $this->getRequest();
if ($request->isPost()) {
$form->setData($request->getPost());
if ($form->isValid()) {
$filteredData = $form->getData();
}
}
Try this -
$request = $this->getRequest();
if ($request->isPost()) {
$form->setData($request->getPost()); //Repopulate the $form object with $_POST values
if ($form->isValid()) { //Data is filtered here
//code to save data
}
}

Add data to a submitted form object inside a controller in Symfony2

I want to save some data where part of it is from the user where they submit it through a form and the other part is generated in the actual controller. So something like:
# controller
use Acme\SomeBundle\Entity\Variant;
use Acme\SomeBundle\Form\Type\VariantType;
public function saveAction()
{
$request = $this->getRequest();
// adding the data from user submitted from
$form = $this->createForm(new VariantType());
$form->bindRequest($request);
// how do I add this data to the form object for validation etc
$foo = "Some value from the controller";
$bar = array(1,2,3,4);
/* $form-> ...something... -> setFoo($foo); ?? */
if ($form->isValid()) {
$data = $form->getData();
// my service layer that does the writing to the DB
$myService = $this->get('acme_some.service.variant');
$result = $myService->persist($data);
}
}
How do I get $foo and $bar into the $form object so that I can validate it and persist it?
Here's the general pattern I'm using:
public function createAction(Request $request)
{
$entity = new Entity();
$form = $this->createForm(new EntityType(), $entity);
if ($request->getMethod() == 'POST') {
$foo = "Some value from the controller";
$bar = array(1, 2, 3, 4);
$entity->setFoo($foo);
$entity->setBar($bar);
$form->bindRequest($request);
if ($form->isValid()) {
$this->get('some.service')->save($entity);
// redirect
}
}
// render the template with the form
}
Reading the code for the bind method of the Form class, we can read this:
// Hook to change content of the data bound by the browser
$event = new FilterDataEvent($this, $clientData);
$this->dispatcher->dispatch(FormEvents::BIND_CLIENT_DATA, $event);
$clientData = $event->getData()
So I guess you could use this hook to add your two fields.

Categories