Twig/Symfony not rendering form_widget - php

I have a twig template (home.twig), and I'm using
{{ render(controller('WebsiteUserBundle:Registration:register',{ 'template': 'popup'} )) }}
inside that template to render another template (login.twig), which is used to login the user from a popup.
The problem is that the form_widget isn't rendered, but the form_label is.
This is part of my login template:
<div class="row-fluid">
{{ form_label(form.email, 'Email:')}}
{{ form_widget(form.email, { 'attr': {'class': 'span12'} }) }}
</div>
And by "it's not rendered", I mean that there isn't even an empty div or input next to the label element in the DOM.
Why is this happening?

I had the same issue and was because I had the follow sentence before the form_widget
{{ form_rest(form) }}
Check that the form_rest be after of all your form_widget.

if you get only form_widget without extra params, like this :
{{ form_widget(form.email) }}
do you have more informations or any output ?

Related

Form two Textfields in one row

I want to set two Text fields in one row in the from class. That they loke like this:
Name/surname ______ _______
I can't find something in the Internet.
I use this code in the Form class:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextareaType::class)
->add('surname', TextareaType::class);
}
But this will be shown like this:
Name ____
surname ____
and this is wrong in my situation. Is it possible to do that in the form class?
Thanks for your help.
You can show your form_rows in a more custom way by doing this:
Instead of showing your form in the more universal way:
{{ form_start(myForm) }}
{{ form_widget(myForm) }}
{{ form_end(myForm) }}
You can, in fact, do the next:
{{ form_start(myForm) }}
{{ form_row(myForm.field1) }}
{{ form_row(myForm.field2) }}
{{ form_widget(myForm) }}
{{ form_end(myForm) }}
This will allow you to show those rows how you want with extra arguments, and it will continue to show the rest, so you do not need to use form_row for every row once you have started using it.
Now, you can achieve what you are asking using display: flex, and setting it's direction as row.
Using bootstrap:
{{ form_start(myForm) }}
<div class = "d-flex flex-row">
{{ form_row(myForm.field1) }}
{{ form_row(myForm.field2) }}
</div>
{{ form_widget(myForm) }}
{{ form_end(myForm) }}
Without bootstrap:
{{ form_start(myForm) }}
<div style = "display: flex; flex-direction: row;">
{{ form_row(myForm.field1) }}
{{ form_row(myForm.field2) }}
</div>
{{ form_widget(myForm) }}
{{ form_end(myForm) }}
You can put the label you want only to field1, and so, field 2, without a label, will appear as you are asking, next to the input field of field 1.

Symfony Assert doesnt working with form

I used Assert to validate fields like
#Assert\NotBlank(message="this field cannot be empty")
$private title;
#Assert\NotBlank(message="this field cannot be empty")
$private description;
#Assert\NotBlank(message="this field cannot be empty")
$private price;
Now, when Im using form in html.twig
{% body block %}
{{ form(form, {"attr": {"novalidate": "novalidate"}}) }}
{% endblock %}
everything is allright, if I have an empty field I got my message, but when Im trying to divide this form like
{% body block %}
{{ form_start(form, {"attr": {"novalidate": "novalidate"}}) }}
{{ form_widget(form.title) }}
{{ form_widget(form.description) }}
{{ form_widget(form.price) }}
{{ form_rest(form) }}
{{ form_end(form, {"attr": {"novalidate": "novalidate"}}) }}
{% endblock %}
I'm getting something like default message that I cannot add an advert, but there are no messages next to my fields. What am I doing wrong?
I've tried use novalidate attribute in every form field but it still doesn't working
You should use {{ form_row(form.title) }} instead {{ form_widget(form.title) }} and so on. Alternatively add {{ form_error(form.title) }} to every {{ form_widget(form.title) }} and so on.
Explanation: form_widget render only form control ie. input box or drop-down. form_row render: form_label - the title of field, form_widget - the control, form_error if needed - the errors attached to field. It also wrap everything on nice div to group related parts.

Blade templating (Laravel) overwrite issues

I have a "master" layout that has a section: #yield('other-scripts')
I use this master template in another view (especie.blade.php):
#extends('layouts.master')
#include('layouts.editarEspecie')
#section('other-scripts')
{{ HTML::script('js/lightbox.js') }}
#stop
Inside layouts.editarEspecie, #section('other-scripts') is also overwritten:
#section('other-scripts')
{{ HTML::script('js/chosen.jquery.js') }}
{{ HTML::script('js/chosenScriptModal.js') }}
{{ HTML::script('js/scripts.js') }}
#stop
The problem is that #include('layouts.editarEspecie') goes first, so #section('other-scripts') never adds the part inside especie.blade.php.
What can I do so that both part are added and it ends like this?
{{ HTML::script('js/chosen.jquery.js') }}
{{ HTML::script('js/chosenScriptModal.js') }}
{{ HTML::script('js/scripts.js') }}
{{ HTML::script('js/lightbox.js') }}
P.S.: I do this because lightbox.js is always necessary, but chosen.jquery.js, chosenScriptModal.js, scripts.js may not always be needed.
Inside layouts.editarEspecie dont have a "section" - just have this:
{{ HTML::script('js/chosen.jquery.js') }}
{{ HTML::script('js/chosenScriptModal.js') }}
{{ HTML::script('js/scripts.js') }}
Then those 3 lines will always be included.
The thing is, I donĀ“t want those 3 lines to always go inside
layouts.editarEspecie, just when certain conditions are met (not shown
here)
ummmmm.. i know its like the fastest thing ever but you can always PHP if it
<?php
if (condition)
{
?>
{{ script here}}
<?php
}
else
{}
?>
Sorry for lazy explanation but i am sure you get the hint

Using Statamic Partial Variables within Entries Tags?

Trying to use partials in the Statamic CMS to keep some content areas as DRY as possible.
According to the documentation, I can pass variables to a partial like so:
{{ theme:partial src="sidebar" my_count="2" }}
In my partials/sidebar template, I have the following:
{{ my_count }}
{{ entries:listing folder="projects" }}
I am number {{ my_count }}
{{ /entries:listing }}
However, when the page loads, the variable inside the {{ entries:listing }} tag is not parsed.
2
I am number
I am number
I am number
I am number
I am number
Am I missing a step to get {{ my_count }} to output when called inside the entries tag pair?
NOTE: My ultimate goal is to pass the variable to a parameter, like so:
{{ entries:listing folder="projects" limit="{{ my_count }}" }}
...
{{ /entries:listing }}
The variable won't parse inside of the entries:listing tag pair, but you can use it as a parameter.
This example code works:
partials/temp.html
{{ entries:listing
folder = "blog"
limit = "{ limit }"
}}
{{ title }}
{{ /entries:listing }}
templates/temp.html
{{ theme:partial src="temp" limit="2" }}

How to tackle variables that may not be set when using blade template engine to build forms

Ok, so a long title, but apologies, he only way to describe it without getting the wrong kind of answer.
So, the scenario....
I am creating a site which has a search form of sorts in the header, and therefore on every page. I would like it to retain its previous variables when being used for user convenience, for my convenience I have built the form into the default layout, to save recreating many instances of it.
default.blade.php (Heres the form, with unnecessary markup removed)
{{ Form::open(array('url' => '/search')) }}
{{ Form::select('model', Photo::getAvailableModels(true), $model) }}
{{ Form::select('colour', Photo::getAvailableColours(true), $colour) }}
{{ Form::submit('Go') }}
{{ Form::close() }}
The $model & $colour are variables I am capturing during the post. The problem is, I am getting unset variable errors from Blade on any pages where the user hasn't posted to, so I am literally having to preset them in almost every route or controller across my entire site, just to prevent the errors.
In essence, the system works fine as long as the user is posting, if someone visits the site using a direct link its basically useless.
Obviously I can not have the search form be set to the previously searched results, but that would be bad practice from a usability point of view.
Am I missing something here, surely there has to be a simple solution to this. Thanks for any help.
Create a view composer for your highly used variables:
View::composer(['store.index', 'products.*'], function($view)
{
$model = Input::get('model') ?: 'modelX';
$colour = Input::get('colour') ?: 'colourY';
$view->with('model', $model);
$view->with('colour', $colour);
});
Laravel will send those variables to your views automatically, every time someone hit one of them.
You can put that in your routes file, filters file or, like, me, create a app/composers.php and load by adding
require app_path().'/composers.php';
To your app/start/global.php.
You can check whether variables are set or not with PHP isset():
{{ Form::open(array('url' => '/search')) }}
#if (isset($model) && isset($colour))
{{ Form::select('model', Photo::getAvailableModels(true), $model) }}
{{ Form::select('colour', Photo::getAvailableColours(true), $colour) }}
#else
{{ Form::select('model', Photo::getAvailableModels(true)) }}
{{ Form::select('colour', Photo::getAvailableColours(true)) }}
#endif
{{ Form::submit('Go') }}
{{ Form::close() }}
Or, if your form fields are optional and you need to check separately:
{{ Form::open(array('url' => '/search')) }}
#if (isset($model))
{{ Form::select('model', Photo::getAvailableModels(true), $model) }}
#else
{{ Form::select('model', Photo::getAvailableModels(true)) }}
#endif
#if (isset($colour))
{{ Form::select('colour', Photo::getAvailableColours(true), $colour) }}
#else
{{ Form::select('colour', Photo::getAvailableColours(true)) }}
#endif
{{ Form::submit('Go') }}
{{ Form::close() }}

Categories