Form with embedded relation won't save Symfony 1.4 Doctrine - forms

I have embedded the Mutt form within the Mix form:
MixForm.class.php:
$this->embedRelation('Mutt');
$form = new MuttForm(null, array(
'mix' =>$this->getObject(),
));
$this->embedForm('Mutt', $form);
$this->widgetSchema['Mutt'] = $form->getWidgetSchema();
$this->widgetSchema['Mutt']['mix_id'] = new sfWidgetFormInputHidden();
$this->validatorSchema['Mutt'] = $form->getValidatorSchema();
I need the newly created id form for the Mix table to populate the mix_id field in the Mutt table.
<?php echo $form->renderHiddenFields();?>
<?php echo $form['name']->renderRow();?>
<?php echo $form['parent1']->renderRow();?>
<?php echo $form['parent2']->renderRow();?>
<?php echo $form['parent3']->renderRow();?>
<?php echo $form['parent4']->renderRow();?>
<?php echo $form['parent5']->renderRow();?>
<?php echo $form['Mutt']['creator']->renderRow();?>
<?php echo $form['Mutt']['email']->renderRow();?>
<?php echo $form['Mutt']['website']->renderRow();?>
<?php echo $form['Mutt']['caption']->renderRow();?>
<?php echo $form['Mutt']['photo']->renderRow();?>
<?php echo $form['Mutt']['copyright']->renderRow();?>
<?php echo $form['Mutt']->renderHiddenFields();?>
Here is my action in modules/mix/actions/actions.class.php
public function executeEdit(sfWebRequest $request)
{
$this->form = new MixForm();
if($request->isMethod('post')):
$this->form->bind($request->getParameter('mix'), $request->getFiles($this->form->getName()));
if($this->form->isValid()):
$this->form->save();
$this->redirect('pure/add');
endif;
endif;
}
The form validation works correctly, but it won't save in either database.
What am I doing wrong??

You are defining an action for executeEdit, but processForm is the action where form validation and saving to the database occur. executeEdit is the action for displaying a form when editing an existing job.
See: http://www.symfony-project.org/jobeet/1_4/Doctrine/en/10#chapter_10_sub_the_form_action

Related

Passing a variable from phtml to CMS block in Magento 2

I have a need to sending data from block creation place in .phtml to my CMS block.
I create the block on the .phtml as follow
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->get('Magento\Framework\Registry')->registry('current_category'); ?>
<?php if ($category->getShortDescription()) : ?>
<?php echo $block->getLayout()->createBlock(
'Magento\Cms\Block\Block')->setBlockId('short_description')->setData('sd','Hello Short')->toHtml();?>
<?php endif; ?>
Here, I'm passing a parameter to the block as setData('sd','Hello Short') But the parameter is not showing on the cms block
I call this parameter/argument on my blog as
The Short Description is {{sd}}
But I expect the output should be The Short Description is Hello Short but I The Short Description is {{sd}}
I have resolved this issue by replacing the text on CMS Block.
<?php if ($category->getShortDescription()) : ?>
<?php
$shortDescriptionBlock = $block->getLayout()->createBlock(
'Magento\Cms\Block\Block',"",["short_desc" => $category->getShortDescription()])->setBlockId('short_description')->toHtml();
echo str_replace("{{short_description}}", $category->getShortDescription(), $shortDescriptionBlock);?>
<?php endif; ?>

Codeigniter redirect to empty form

I have the follow form in Codeigniter:
The controller:
public function item($alias = NULL){
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Edit menu';
$data['menu_item'] = $this->menu_model->get_menu($alias);
$data['articole'] = $this->menu_model->get_articole();
$data['menuactive'] = $this->menu_model->get_menuactiv();
$data['errors'] = $this->form_validation->error_array();
$this->form_validation->set_rules('position','Position','required');
$this->form_validation->set_rules('position','Position','numeric');
$this->form_validation->set_rules('name','Name','required');
if ($this->form_validation->run() === FALSE) {
$data['name'] = $data['menu_item']['name'];
$this->load->view('templates/header', $data);
$this->load->view('templates/youarehere', $data);
$this->load->view('templates/menu', $data);
$this->load->view('templates/admin', $data);
$this->load->view('menu/item', $data);
$this->load->view('templates/footer');
}
else {
$this->menu_model->update_menu();
redirect('menu');
}
}
The item view is:
<?php echo validation_errors(); ?>
<?php echo form_open('menu/item'); ?>
<?php echo form_label('ID ', 'id'); ?>
<?php echo form_input('id', $menu_item['id'], 'readonly'); ?><br><br>
<?php echo form_label('Name ', 'name'); ?>
<?php echo form_input('name', $menu_item['name']); ?><br><br>
<?php echo form_label('Position ', 'position'); ?>
<?php echo form_input('position', $menu_item['position']); ?><br><br>
<?php foreach($articole as $articole_item):
$articol1[] = $articole_item['id'] . ' ' . $articole_item['title'];
endforeach; ?>
<?php echo form_label('Associated article ', 'associated_article'); ?>
<?php echo form_dropdown('associated_article', $articol1, $menu_item['articol_asociat']); ?><br><br>
<?php echo form_label('Menu activ ', 'activ'); ?>
<?php echo form_checkbox('activ', '1', TRUE); ?><br><br>
<input type="submit" name="submit" value="Save menu"/>
</form>
In this view I edited menu items. Everything works fine when when everything is right. When I introduce something wrong in a field, like string into "position" field, the form redirect to item view, but with empty fields and with the error message. I want to keep what is entered in field and the error message.
What is wrong with my code?
Change your form :
<?php echo form_label('ID ', 'id'); ?>
<?php echo form_input('id', set_value('id'), 'readonly'); ?><br><br>
<?php echo form_label('Name ', 'name'); ?>
<?php echo form_input('name', set_value('name')); ?><br><br>
<?php echo form_label('Position ', 'position'); ?>
<?php echo form_input('position', set_value('position')); ?>
set_value() replace your old values

ZF2 How to set search form in layout

What is the right way to set a (search) form with Zend Framework 2 in the layout.ptml who is visible on any page of the website?
Thanks in advance.
Nick
It is really simple to set any variables to all layouts in ZF2 by EventManager, just attach the EVENT_RENDER event such as:
class Module
{
public function onBootstrap($e)
{
$app = $e->getParam('application');
$app->getEventManager()->attach(MvcEvent::EVENT_RENDER, array($this, 'setFormToView'), 100);
}
public function setFormToView($event)
{
$form = new MyForm();
$viewModel = $event->getViewModel();
$viewModel->setVariables(array(
'form' => $form,
));
}
}
For view in layout use:
<?php if ($user = $this->identity()): ?>
<?php echo 'Login with user' . $this->escapeHtml($user->nome); ?>
| <?php echo $this->translate('Sair'); ?>
<?php else: ?>
<?php
echo $this->form()->openTag($form);
echo "<h5>Forneça seu login e senha </h5>";
echo $this->formRow($form->get('username'));
echo $this->formRow($form->get('password'));
echo $this->formRow($form->get('rememberme'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
?>
<?php endif; ?>

Zend_Dojo_Form in a layout

I have a Zend_Dojo_Form which I have moved from my view (where it works fine) to my layout, as it's something that will be useful on every page. However in the layout the form no longer works - none of the dijit elements appear and it behaves just as a normal HTML form would.
Here's the relevant part of my bootstrap:
protected function _initView()
{
Zend_Layout::startMvc(array(
'layoutPath' => '../application/layouts',
'layout' => 'default'
));
$view = new Zend_View();
$view->setEncoding('UTF-8')
->doctype('HTML5');
// init Dojo
Zend_Dojo::enableView($view);
$view->dojo()->enable()
->setCdnVersion('1.5')
->requireModule('dojo.data.ItemFileWriteStore')
[...]
->addStyleSheetModule('dijit.themes.tundra');
// assign the view to the viewRenderer, so it will be used by the MVC actions
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
$viewRenderer->setView($view);
return $view;
}
there are no errors (JS or ZF), the form just doesn't work as it should.
I assume I need to Dojo enable the Layout view in some way. I tried changing the layout part of the bootstrap method above to this:
$layout = Zend_Layout::startMvc(array(
'layoutPath' => '../application/layouts',
'layout' => 'default'
));
$view = $layout->getView();
Zend_Dojo::enableView($view);
$layout->setView($view);
but that didn't make any difference.
I found this question which sounds very similar to my problem, but the accepted answer just shows including the dojo helper in the layout, which I am doing already.
This is most probably due to that you have the layout as suggested in the docs:
<?php echo $this->doctype() ?>
<html>
<head>
<?php echo $this->headTitle() ?>
<?php echo $this->headMeta() ?>
<?php echo $this->headLink() ?>
<?php echo $this->headStyle() ?>
<?php if ($this->dojo()->isEnabled()){
$this->dojo()->setLocalPath('/js/dojo/dojo.js')
->addStyleSheetModule('dijit.themes.tundra');
echo $this->dojo();
}
?>
<?php echo $this->headScript() ?>
</head>
<body class="tundra">
<?php echo $this->layout()->content ?>
<?php echo $this->inlineScript() ?>
</body>
</html>
The problem is the echo $this->dojo() must be after the $this->form->render() otherwise the required modules won't have been registered in Zend_Dojo.

Rendering only the <form> tag

Is there any way that i can render ONLY the start <form> tag of a Zend_Form object?
print $this->registerForm->renderForm();
renders <form></form>, and i only need <form>
Edit:
After Asleys possible solution i wrote this for My_Form class
public function renderFormOpen() {
return str_replace('</form>', '', $this->renderForm());
}
public function renderFormClose() {
return '</form>';
}
Still looking for at ZF way of doing thins, even though i don't think there is any - after going through the code in the ZF library.
You could write an custom form-decorator that uses a custom view-helper that only renders the open form tag. But I think this would be overkill.
Just "hardcode" the form-tags and fill the attributes with the data provided by the form-variable in your view.
<!--in your view-template -->
<form action="<?php echo $this->form->getAction() ?>"
enctype="<?php echo $this->form->getEnctype() ?>"
method="<?php echo $this->form->getMethod() ?>"
id="<?php echo $this->form->getId() ?>"
class="<?php echo $this->form->getAttrib('class') ?>" >
<!--in case your products are represented as elements -->
<?php foreach ($this->form->getElements() as $element): ?>
<?php echo $element ?>
<?php endforeach; ?>
<!--in case your products are represented as displayGroups -->
<?php foreach ($this->form->getDisplayGroups() as $displayGroup): ?>
<?php echo $displayGroup ?>
<?php endforeach; ?>
<!--in case your products are represented as subforms -->
<?php foreach ($this->form->getSubforms() as $subform): ?>
<?php echo $subform ?>
<?php endforeach; ?>
<!--in case your products are rendered by a view helper -->
<?php foreach ($this->products as $product): ?>
<?php echo $this->renderProduct($product) ?>
<?php endforeach; ?>
</form>
Just for fun the overkill way
// Get your products form
$form = new Form_Products();
// Add custom prefix path
$form->addPrefixPath('Foobar_Form_Decorator', 'Foobar/Form/Decorator', 'decorator');
// Set OnlyOpenTagForm-ViewHelper for FormDecorator
$form->getDecorator('Form')->setHelper('OnlyOpenTagForm');
// copy Zend/View/Helper/Form to Foobar/Form/Decorato/OnlyOpenTagForm.php
// In OnlyOpenTagForm.php
// replace Zend_View_Helper_Form with Foobar_View_Helper_OnlyOpenTagForm
// replace method "form" with onlyOpenTagForm"
// replace
if (false !== $content) {
$xhtml .= $content
. '</form>';
}
// with:
if (false !== $content) {
$xhtml .= $content;
}
Done! - The Java-Guys will love it ;)
You can render just the open form tag by passing false to the form decorator like so:
<?php echo $this->form->renderForm(false) ?>
Which will output something like:
<form id="post" enctype="multipart/form-data" method="post" action="/post">
Additonally you can pass a string to the form decorator to be enclosed by the form tags like so:
<?php echo $this->form->renderForm('Some Text') ?>
Which outputs something like:
<form id="post" enctype="multipart/form-data" method="post" action="/simchas/post">Some Text</form>
Hope this helps...
You could do something like this:
echo $this->form->getDecorator('Form')->setElement($this->form)->render(false);