sfWidgetFormDoctrineChoice: how to iterate data from nested models? (Symfony 1.4) - symfony1

I have the ConnectForm with nested fields from other model: Numbers.
I want to render this ConnectForm with all Numbers and everything works well when i just
<?php echo $form['numbers_list'] ?>
But i need to print other Numberss' fields like: price, code etc.
How to print it?
The best way for me would something like this:
<?php foreach($form['numbers_list'] as $num): ?>
<span> echo $num->renderInput()</span>
<span>echo $num->getPrice()</span>
<span>echo $num->getCode()</span>
<?php endforeach; ?>

You probably are better off creating a custom sfWidgetFormSchemaFormatter class and then using that to output your form - you can output a whole form once you have created your new sfWidgetFormSchemaFormatter class by doing <?php echo $form ?> for example :
class sfWidgetFormSchemaFormatterCustom extends sfWidgetFormSchemaFormatter
{
protected
$rowFormat = "<span class=\"my-label-class\">%label%</span>\n <span>%error%%field%%help%%hidden_fields%</span>`n",
$errorRowFormat = "<span class=\"my-error-class\" colspan=\"2\">\n%errors%</span>\n",
$helpFormat = '<br />%help%',
$decoratorFormat = "<div>\n %content%</div>";
}
Then within the configure method of your form add the following that tells the form to use your custom decorator :
$this->getWidgetSchema()->setFormFormatterName('custom');

Related

Octobercms disble fields in relation

I want to disable form fields in the update context, see the image:
I tried this, but it did not work
public function filterFields($fields, $context = null)
{
if($context == 'update') {
$fields->books->disabled = true;
$fields->user->disabled = true;
}
}
Seems you are trying to make relation manager field disable(read only)
But I am sure they are not following same pattern as normal widgets do.
they can not be disabled directly like that as I found another easy way.
you may have used partial for rendering this relational field ( book | user ) and your partial _books.htm is looking like this.
<?= $this->relationRender('comments', ['readOnly' => false]) ?>
You need to change it with this one
<?php if($this->widget->form->context == 'update'): ?>
<?= $this->relationRender('comments', ['readOnly' => true]) ?>
<?php else: ?>
<?= $this->relationRender('comments', ['readOnly' => false]) ?>
<?php endif; ?>
The magic config value is this readOnly property it will make list read-only or active working.
try this it will work , if not please comment.

ZF2 Form\Element\MultiCheckbox: how to get each item on a new line?

I've been Googling this to no avail. I have a multi-checkbox form element in one of my forms. Here's the code I used to create it:
$this->add(array (
'name' => 'thingyId',
'type' => 'MultiCheckbox',
'options' => array (
'value_options' => $thingyArray,
)
));
In my view script, I have this:
<?= $this->formRow($form->get('thingyId')); ?>
The form element shows up fine, but all of the checkboxes are on a single line. How do I get it so that each checkbox is on a new line?
If you view this link, you can see that the fourth argument is partial. So, you can use many ways to accomplish the task.
Method 1:
echo $this->formRow($element, null, null, 'template-file');
Now, create a template file named as template-file.phtml to render the element however you like.
//template-file.phtml
<span><?php echo $label; ?></span><br/>
<?php foreach ($element->getValueOptions() as $key => $value): ?>
<input type="checkbox" name="<?php echo $element->getName() ?>[]" value="<?php echo $value; ?>">
<span><?php echo $key; ?></span><br/>
<?php endforeach; ?>
Method 2
Create your own view helper by extending the default helper.
namespace Application\View\Helper;
class MyFormRow extends \Zend\Form\View\Helper\FormRow
{
/**
* #var string
*/
protected $partial = 'template-file';
}
Now, inform our application about our new helper in your module,
namespace Application;
class Module
{
public function getViewHelperConfig()
{
return array(
'invokables' => array(
'myFormRow' => 'Application\View\Helper\MyFormRow'
)
);
}
}
Lastly use the helper:
echo $this->myFormRow($element);
I came across this question when I was having this issue myself. The code that was being used was the following:
<?php
$oMultiCheckboxField = $oForm->get('multicheckboxelement');
echo $this->formMultiCheckbox($oMultiCheckboxField);
?>
The only additional parameter you could pass to the formMultiCheckbox view helper was whether to append or prepend the label.
How I eventually chose to solve this is with the following code:
<?php
$oMultiCheckboxField = $oForm->get('multicheckboxelement');
$oMultiCheckboxViewHelper = new \Zend\Form\View\Helper\FormMultiCheckbox();
$oMultiCheckboxViewHelper->setSeparator('<hr>');
echo $oMultiCheckboxViewHelper->render($oMultiCheckboxField);
?>
From what I recall, ZF1 had the option to set the separator (I clearly remember this at least for radio buttons). Why there isn't a clearer way to do this in ZF2 is a bit puzzling. If there are better ways to do this, I would certainly like to know about it.

How to adapt this wordpress loop query code to include pagination?

I have sourced the below code to query my posts in Wordpress and all works as intended.
I'd like to add pagination to this now. I don't want to alter the code too much now that I know it works, can anyone advise on the best way to adapt this to include the pagination?
I would like it to show a maximum of 18 posts per page and have next and prev links to other pages if they exist. This code is being used in custom category templates, but I have also setup a static page setup under the reading settings and the display of main posts uses home.php, I'd like to use the same or similar loop code to also paginate that. Any help is appreciated. Here is the code:
<div id="Items">
<ul>
<?php
// Grid sorted alphabetically
if (is_category('categoryName'))
{
$args = array( 'posts_per_page' => -1, 'orderby'=> 'title', 'order' => 'ASC', 'category' => 41 );
$categoryNameposts = get_posts( $args );
}
foreach( $categoryNameposts as $post ) : setup_postdata($post);
?>
<li><?php get_template_part( 'content', get_post_format() ); ?></li>
<?php endforeach; ?>
</ul>
</div><!-- #ItemsEnd -->
You should let wordpress know how much post you want to show per page in the query_posts. Replace your $args with these two lines:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'posts_per_page' => 18, 'paged' => $paged, 'orderby'=> 'title', 'order' => 'ASC', 'category' => 41 );
Now to show Next-Previous link:
<div class="paginationClass">
<?php next_posts_link(); ?>
<?php previous_posts_link(); ?>
</div>
Also in is_category function, use categorySlug, instead of categoryName.

Add a form of another module

I have a module called main which is my default module and a module called song.
I want to put into my main module an "add form" of my song module.
I don't know if I have to use component, how and where to process the form.
Could you please help me ?
Have you checked the documentation? Specially this part of the doc?
It covers the form system by using a basic contact form:
You have the controller (the actions.class.php inside your main module) which create an instance of the form and handle the submission (validation, save, etc ..)
Then the template (contactSuccess.php), which display the form
The main difference is that you probably have a model called Song, so you will have to use the SongForm instead of creating a new one (using new sfForm()). For this part, you can see, on the same documentation page, the part Forms Based on a Model which cover the case of an article model.
Edit:
A step by step:
In your main/actions/actions.class.php:
public function executeIndex($request)
{
$this->form = new SongForm();
if ($request->isMethod('post'))
{
$this->form->bind($request->getParameter('song'));
if ($this->form->isValid())
{
$song = $this->form->save();
$this->getUser()->setFlash('notice', 'Thank you, the song has been added');
$this->redirect('main/index');
}
}
}
In your template, main/templates/indexSuccess.php:
<?php if ($sf_user->hasFlash('notice')): ?>
<div class="flash_notice"><?php echo $sf_user->getFlash('notice') ?></div>
<?php endif ?>
<?php echo $form->renderFormTag('main/index') ?>
<table>
<?php echo $form ?>
<tr>
<td colspan="2">
<input type="submit" />
</td>
</tr>
</table>
</form>
And you're done.
I really encourage you to read the whole Jobeet tutorial. You will learn lots of things. Basically every thing I described here, is in this tutorial.
For the sf_guard_user field, you should redefine it as hidden, and then set a default value with the current connected user.
Create a new form: /lib/form/CustomSongForm.class.php
<?php
class CustomSongForm extends SongForm
{
public function configure()
{
parent::configure();
$this->widgetSchema['sf_guard_user_ud'] = new sfWidgetFormInputHidden();
}
}
Then you can define the default, like you said:
}$this->form->setDefault('sf_guard_user_id', $this->getUser()->getId());

Symfony: Is it possible to put elements inside a link_to tag?

I'm using Symfony 1.4 and wondering whether it's possible to achieve the following:
<span>Text</span>
... using Symfony's link_to helper?
Of course, it's possible to do this:
<span>Text</span>
But I'm wondering if there's a simpler way to do it, especially as combining i18n with the above will produce:
<span><?php echo __('Text') ?></span>
... a tag soup basically.
Thanks.
YOu can do it two ways...
Option 1
<?php echo link_to("<span>".__('Text')."</span>", $url); ?>
Option 2
<?php echo content_tag('a', "<span>".__('Text')."</span>", array('href' => url_for($url))); ?>
There's also:
<?php echo link_to(content_tag('span', __'Text', array('class' => 'span-class')), '#route', array('class' => 'link-class'));
I added the attribute class for each of the two HTML tags as options if you need to extend that way.

Resources