Skip to content

Instantly share code, notes, and snippets.

@mrcmorales
Created October 14, 2013 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrcmorales/6973357 to your computer and use it in GitHub Desktop.
Save mrcmorales/6973357 to your computer and use it in GitHub Desktop.
<?php
namespace CliCons\ExtranetBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class ConstructionFormType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('description')
->add(
'address',
'clicons_extranetbundle_address',
array('label' => false,'required' => false)
)
->add('workers', 'collection', array(
'type' => new ConstructionWorkerFormType(),
'allow_add' => true,
))
->add('register', 'submit',array('label' => 'form.user.registration.register'));
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'CliCons\CoreBundle\Entity\Construction'
));
}
/**
* @return string
*/
public function getName()
{
return 'clicons_extranetbundle_construction';
}
}
<?php
namespace CliCons\ExtranetBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use CliCons\CoreBundle\Enum\PrincipalAgentTypeEnum;
class ConstructionWorkerFormType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('principalAgentType','choice', array('choices' =>PrincipalAgentTypeEnum::getTranslations() ))
->add('user','genemu_jqueryselect2_entity', array('class' => 'CliCons\CoreBundle\Entity\User', 'property'=>'companyName',
))
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'translation_domain' => 'extranet',
'data_class' => 'CliCons\CoreBundle\Entity\ConstructionWorker'
));
}
/**
* @return string
*/
public function getName()
{
return 'clicons_extranetbundle_constructionworker';
}
}
{% extends "@CliConsExtranet/extranet.html.twig" %}
{% block stylesheets %}
{{ parent() }}
{% stylesheets filter='cssrewrite'
'bundles/cliconsextranet/css/ui/jquery-ui.min.css'
'bundles/cliconsextranet/css/extranet.css'
'bundles/cliconsadmin/css/select2.css'
%}
<link href="{{ asset_url }}" type="text/css" rel="stylesheet" media="screen"/>
{% endstylesheets %}
{% if form is defined %}
{{ form_stylesheet(form) }}
{% endif %}
{% endblock %}
{% block javascripts %}
{{ parent() }}
<script src="http://maps.google.com/maps/api/js?sensor=false&language=es"></script>
{%- javascripts
'@CliConsExtranetBundle/Resources/public/js/jquery.min.js'
'@CliConsExtranetBundle/Resources/public/js/ui/jquery-ui.min.js'
'@CliConsExtranetBundle/Resources/public/js/ui/jquery.ui.addresspicker.js'
'@CliConsAdminBundle/Resources/public/js/select2.js'
filter="" %}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
{% if form is defined %}
{{ form_javascript(form) }}
{% endif %}
{% endblock %}
{% block content %}
{% block id 'usuario22' %}
{% block article %}
<h1>{{ block('title') }}</h1>
{{ form_start(form) }}
{{ form_row(form.name) }}
{{ form_row(form.description) }}
{{ form_row(form.address) }}
<h3>workers</h3>
<ul class="workers" data-prototype="{{ form_widget(form.workers.vars.prototype)|e }}">
</ul>
{{ form_end(form) }}
{% endblock %}
<div class='map-wrapper'>
<div id="map"></div>
<div id="legend">You can drag and drop the marker to the correct location</div>
</div>
<div class='input-positioned'>
<label>Callback: </label>
<textarea id='callback_result' rows="15"></textarea>
</div>
<script type="text/javascript">
// Get the ul that holds the collection of tags
var collectionHolder = $('ul.workers');
// setup an "add a tag" link
var $addTagLink = $('<a href="#" class="add_tag_link">Add a tag</a>');
var $newLinkLi = $('<li></li>').append($addTagLink);
$(function (){
// add the "add a tag" anchor and li to the tags ul
collectionHolder.append($newLinkLi);
// count the current form inputs we have (e.g. 2), use that as the new
// index when inserting a new item (e.g. 2)
collectionHolder.data('index', collectionHolder.find(':input').length);
$addTagLink.on('click', function (e) {
// prevent the link from creating a "#" on the URL
e.preventDefault();
// add a new tag form (see next code block)
addTagForm(collectionHolder, $newLinkLi);
});
});
function triggerJavascript(id)
{
$field = $('#' + id);
{{ form_javascript(form.workers.vars.prototype, true) }}
}
function addTagForm(collectionHolder, $newLinkLi) {
// Get the data-prototype explained earlier
var prototype = collectionHolder.data('prototype');
// get the new index
var index = collectionHolder.data('index');
// Replace '__name__' in the prototype's HTML to
// instead be a number based on how many items we have
var newForm = prototype.replace(/__name__/g, index);
// increase the index with one for the next item
collectionHolder.data('index', index + 1);
// Display the form in the page in an li, before the "Add a tag" link li
var $newFormLi = $('<li></li>').append(newForm);
$newLinkLi.before($newFormLi);
// Dynamically add the form and get its id
var id = '{{ form.workers.vars.id }}_' + id;
// Once HTML has been added, let's trigger the javascript on it
triggerJavascript(id);
{##}
}
</script>
{% endblock %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment