My twig file looks like this:
<select name="lineItems[{{ product.id }}][quantity]"
class="custom-select product-detail-quantity-select">
{% for quantity in range(product.minPurchase, product.calculatedMaxPurchase, product.purchaseSteps) %}
<option value="{{ quantity }}">
{{ quantity }}{% if product.packUnit %} {{ product.packUnit }}{% endif %}
</option>
{% endfor %}
</select>
I want to change this into a Vue.js app with a quantity/count field and an 'increment' and 'decrement' button.
My Vue.js app looks like this:
<div id="app">
<a class="btn" v-on:click="increment">Add 1</a>
<a class="btn" v-on:click="decrement">Remove 1</a>
<span>[[ count ]]</span>
</div>
<script>
const App = new Vue({
el: '#app',
delimiters: ['[[',']]'],
data() {
return {
count: 1
}
},
methods: {
increment() {
this.count++
},
decrement() {
this.count--
}
}
})
</script>
If you look at my twig file, you see for example the twig variable {{ quantity }}.
How do I use this twig variable in my Vue.js app? So when the user increment the <span>[[ count ]]</span> value by 3. It add's 3 to the {{ quantity }} variable?
Shopware is not using twig. It is using twig.js for the block system. E.g. for overwriting certain parts in the templates. The logic have to be implemented in the vue component. Please descibe your full problem for more information.
Related
I am trying to style my input field in Symfony 3.4 with symfony forms for some duration(hours and minutes only). I am trying with clockpicker but I don't know where I am wrong. If someone can help me or there is a better solution. I have downloaded and included my js and css in web folder.
I am trying to make it like this in the moment:
My twig:
<div class="form-group col-md-4">
<label for="time_for_cooking">Време за готвене(минути):</label>
{% if form_errors(form.recipeCookingTime) %}
{{ form_widget(form.recipeCookingTime, { 'attr': {'class': 'form- control clockpicker-control clockpicker is-invalid'} }) }}
<div class="col">
<small class="text-danger">
{{ form_errors(form.recipeCookingTime) }}
</small>
</div>
{% else %}
{{ form_widget(form.recipeCookingTime, { 'attr': {'class': 'form control clockpicker-control clockpicker'} }) }}
{% endif %}
</div>
My RecipeType.php
->add('recipeCookingTime', TimeType::class)
document ready
<script>
$(document).ready(function() {
('.clockpicker').clockpicker();
} );
</script>
and what its generated, I think the problem is these selects that being genereted
In your FormType you have to set the html5-attribute to false to get an input.
See here https://symfony.com/doc/current/reference/forms/types/time.html#html5
As i said in title i have this
<select id="form_test" oninput="loadTemplate()">
<option>Template list</option>
{% for template in templates %}
<option id="test" value="{{ template.getReport }}">
{{ template.getTemplateName }}
</option>
{% endfor %}
</select>
It shows me all template names from {{ template.getTemplateName }} from database. However value="{{ template.getReport }}" this value always returns first report from database, as If for loop won't work inside tag.
I am using doctrine to fetch this entities from database, like this:
$templates = $this->getDoctrine()->getRepository(ReportTemplate::class)->findAll();
return $this->render('report_form/reportForm.html.twig', [
'templates' => $templates,
]);`
After I deployed site on live server it started working. It's strage I still don't know what was wrong.
I'm using Symfony to make a website for a tennis club, and I'm beating my head down about something :
I want to display an input field based on the option selected in a dropdown list.
This is the scenario :
I'm the admin of the website, and I want to make a reservation. If the reservation is a tournament (selected from a ChoiceType list), I want to display an input field to enter the tournament name.
I want to do something that would look like this in my twig view :
<div class="form-group">
<div class="col-xs-4">
{{ form_label(form.reservationType) }}
{{ form_widget(form.reservationType, {'attr': {'class': 'form-control' }}) }}
</div>
</div>
{% if reservationType == "tournament" %}
<div class="form-group">
<div class="col-xs-4>
{{ form_label(form.tournamentName) }}
{{ form_widget(form.tournamentName) }}
</div>
</div>
{% endif %}
Is it possible to do that just with twig ?
Thanks in advance!
You must use jQuery to solve this issue :
$(document).ready(function(){
$('.reservation').change(
var reservation = $(this).val();
if (reservation == 'xxxx'){
$('.tourName').show();
}else{
$('.tourName').hide();
}
);
});
<div class="form-group">
<div class="col-xs-4">
{{ form_label(form.reservationType) }}
{{ form_widget(form.reservationType, {'attr': {'class': 'form-control reservation' }}) }}
</div>
</div>
<div class="form-group">
<div class="col-xs-4>
{{ form_label(form.tournamentName) }}
{{ form_widget(form.tournamentName, {'attr': {'class': 'hidden tourName' }}) }}
</div>
</div>
No it is not possible only with twig.
What you can do is add a script to your template:
<div class="form-group">
<div class="col-xs-4">
{{ form_label(form.reservationType) }}
{{ form_widget(form.reservationType, {'attr': {'class': 'form-control reservation-type' }}) }}
</div>
</div>
<script type="text/javascript" src="{{ asset('bundles/yourBundle/theNameOfTheScriptYouPutInRessourcesPublic.js') }}"></script>
Then in the script (with jquery) you just listen to change event on the select to insert the input.
$('select.reservation-type').change(function(
if($(this).val() == 'tournament')
{
$('<input type="text" />').appendTo($(this).parent('form-group'));
}
));
If your inputs need twig variables or something you can add the inputs as hidden in the twig template and then in the script you just change the type from hidden to text or whatever you need:
$('select.reservation-type').change(function(
if($(this).val() == 'tournament')
{
$('input[name="tournament-name"]').prop('type', 'text');
}
));
If you don't want to use javascript you could consider using a form event listener: http://symfony.com/doc/current/form/dynamic_form_modification.html
I have some problem. I need add in registration template in FOSUserBundle calendar which allows choose birthday user's - the standard html tag date doesn't fit. To order to do this I used datepicker of jQuery:
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function () {
$("#fos_user_registration_form_birthday").datepicker({
changeMonth: true,
changeYear: true,
yearRange: '1950:2010',
});
});
</script>
<form action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" class="fos_user_registration_register">
{{ form_errors(form) }}
<ol>
[.........]
<li>
{{ form_label(form.birthday) }}
{{ form_widget(form.birthday) }}
{{ form_errors(form.birthday) }}
{{ form_rest(form) }}
</li>
<input type="submit" value="{{ 'registration.submit'|trans }}" />
</li>
</ol>
[.........]
</form>
It's working fine, but if I override the template and add:
{%extends 'MyBlogBundle::layout.html.twig'%}
{% block content %}
[ form registration]
{% endblock%}
It doesn't work.
So, what am I doing wrong?
And I have one more than question - how to add or change class, id in registration form? I tried it in template
{{ form_widget(form.birthday {'attr': {'class': 'task_field'}}) }}
and in RegistrarionFormType:
->add('birthday','text',array('attr'=>array('id'=>'text_field')))
But id doesn't change.
Hi I'm kinda new to this domain but I believe you should include
{% block fos_user_content %}
in your main template or overwrite them totaly in your own bundle (your bundle class should then contain getParent() method)
I believe the main template lives in (if your bundle is named UserBundle)
UserBundle\Resources\views\layout.html.twig
the most basic overwrite would then be
{# UserBundle\Resources\views\layout.html.twig #}
{% extends '::base.html.twig' %}
{% block body %}
{{ block('fos_user_content') }}
{% endblock %}
// UserBundle\UserBundle.php
namespace UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class UserBundle extends Bundle
{
public function getParent()
{
return "FOSUserBundle";
}
}
After that you can copy the templates you want to overload to corresponding bundle folder eg
UserBundle\Resources\views\Registration\xx.html.twig
from the FOSUserBundle.
After changing the "include strategy" in your project you must clear your cache also in DEV mode otherwise you wil not see the changes. Changes applied to the content do not need a clearing of your cache.
I have a Entity Type form where I list all the friends of the current user. This is e.g. used for creating a new Group. Now I want to use the same form for adding people to the same group, so the Choice field should show all the friends of the current user that are not yet in the group. I thought I just use a form event to remove the options(users) that are already in the group.
My listener looks like this:
class FriendSelectListener implements EventSubscriberInterface {
public static function getSubscribedEvents() {
// Tells the dispatcher that we want to listen on the form.pre_set_data
// event and that the preSetData method should be called.
return array(FormEvents::PRE_SET_DATA => 'preSetData');
}
public function preSetData(FormEvent $event) {
$betRound = $event->getData();
$form = $event->getForm();
$groupForm = $form->get('userGroup');
$usersForm = $groupForm->get('users');
foreach($betRound->getUserGroup()->getUsers() as $user){
if($usersForm->has($user->getId())){
$usersForm->remove($user->getId());
}
}
}
}
But I can't render it, because in my test I removed the user with the id 2 and then I get the following error message while rendering:
Key "2" in object (with ArrayAccess) of type
"Symfony\Component\Form\FormView" does not exist in
/var/lib/stickshift/1ed1210eec124c179c45aac4ad484afd/app-root/data/269891/src/Strego/UserBundle/Resources/views/Form/selectFriends.html.twig
at line 5
Update:
This might be related to my view:
{% for id, choice in choices %}
{% set user = choice.data %}
<label>
<div class="people-list people-choose unselected" style="width:270px;">
<img src="{{ user.profilePic | imagine_filter('profile_thumb') }}" class="img-polaroid" alt="{{ user.nickname }}">
{#<img src="{{ asset('bundles/stregoapp/img/profile-thumb-90x90.png') }}" alt="Profilbild" class="img-polaroid">#}
<p>{{ form_widget(form[id]) }} <span class="label">einladen</span></p>
<h3>{{ user.nickname }}</h3>
<h3><small>{{ user.firstName }} {{ user.lastName }}</small></h3>
</div>
</label>
{% endfor %}
For me it seems like I only removed the form element but not the choice.
I found the problem, it was indeed my view. As soon as I stopped iterating over the options but used the child elemts of my form element it was working:
{% for child in form %}
{% set user = choices[child.vars.value].data %}
<label>
<div class="people-list people-choose unselected" style="width:270px;">
<img src="{{ user.profilePic | imagine_filter('profile_thumb') }}" class="img-polaroid" alt="{{ user.nickname }}">
<p>{{ form_widget(child) }} <span class="label">einladen</span></p>
<h3>{{ user.nickname }}</h3>
<h3><small>{{ user.firstName }} {{ user.lastName }}</small></h3>
</div>
</label>
{% endfor %}