Form two Textfields in one row - php

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.

Related

Update Database with Laravel

I am trying to update information in my database with Laravel. Not sure what I am doing wrong but I can't seem to find where the problem is. Here is the code for my edit page (This is the page where I would edit information taken from my DB).
{{ Form::open(['url'=>'portfolio/update']) }}
<div>
{{ Form::label('portfolio_title', 'Portfolio Title:') }}
{{ Form::text('portfolio_title',$work->portfolio_title) }}
{{ $errors->first('portfolio_title','<span class="error">:message</span>') }}
</div>
<div>
{{ Form::label('portfolio_description', 'Portfolio Description') }}<br>
{{ Form::textarea('portfolio_description', $work->portfolio_description, ['size' => '50x5']) }}
{{ $errors->first('portfolio_description','<span class="error">:message</span>') }}
</div>
<div>
{{ Form::label('portfolio_content', 'Portfolio Content') }}<br>
{{ Form::textarea('portfolio_content', $work->portfolio_content, ['size' => '50x5']) }}
{{ $errors->first('portfolio_content','<span class="error">:message</span>') }}
</div>
{{ Form::hidden('id',$work->id) }}
<div>
{{ Form::submit('Update Work') }}
</div>
{{ Form::close() }}
I have a controller called PortfolioController that will save info to database and what not.
public function edit($work_title){
$work = Portfolio::wherePortfolio_title($work_title)->first();
return View::make('portfolio/edit', ['work' => $work]);
}
public function update(){
$id = Input::get('id');
$input = Input::except('id');
if( !$this->portfolio->fill($input)->isValid()){
return Redirect::back()->withInput()->withErrors($this->portfolio->errors);
}
$work = Portfolio::find($id);
$work->portfolio_title = Input::get('id');
$work->save();
}
Here is my route that I am working with:
Route::resource('portfolio','PortfolioController');
Route::post('portfolio/update','PortfolioController#update');
I am able to get the form populated with the correct information but when i change something like the title and click update, the page reloads but does not save in the DB. Sometimes I will get an MethodNotAllowedHttpException error. This has been pretty frustrating for me so any help will be greatly appreciated.
Why don't you just actually use your resource route?
First, remove the portfolio/update route. You don't need it.
Then change your Form::open to this:
{{ Form::open(['route' => ['portfolio.update', $work->portfolio_title], 'method' => 'put']) }}
This way you target the update method in your RESTful controller.
Finally change that to use the portfolio_title as identifier and you should be able to remove the hidden id field from your form.
public function update($work_title){}
Please take a look at:
RESTful controllers
Opening a form

Twig/Symfony not rendering form_widget

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 ?

Laravel 4 : Passing data from database to view

I wish to make a select box containing some categories of items. This select box should have the data from database.
so first i created my blade
admin.blade.php :
<h2>Add Category</h2>
{{ HTML::ul($errors->all(), array('class'=>'errors')) }}
{{ Form::open(array( 'url'=>'admin/category/add', $title="Admin Control Panel")) }}
<p>
{{ Form::label('Category Name:') }}
{{ Form::text('category_name', Input::old('category_name')) }}
</p>
<p>
{{ Form::select('parent_category', array('' => 'Select Category', '0 ' => 'Main Category',)) }}
{{ Form::label('Parent Category:') }}
</p>
<p>
{{ Form::submit('Submit', array('name' => 'save')) }}
</p>
{{ Form::close() }}
and then i created my controller:
class CategoryController extends BaseController
{
public function add()
{
$category_list = CategoryModel::select();
return View::make('admin')->with('category_list', $category_list);
}
i need to display this array in my select box. but i don't know how to do that. please suggest a proper way!
thanks!
return View::make('admin')->with('category_list', $category_list);
What this is saying is create a response and return it to the client. The response is going to be generated using the blade template admin, and we're going to pass the contents of the variable $category_list to the view which will be able to reference it internally as $category_list (the first string parameter to the with function).
Therefore, inside your view if you want to access your category list you can do this:
{{ $category_list->title }}
Assuming, of course, that $category_list here is a single model with an attribute title. If $category_list is a collection of models (or an array), you can just use foreach:
#foreach($category_list as $category)
{{ $category->title }}
#endforeach
If what you're doing is trying to build a select list based off of the entries in your database for a given model, you can simply do this:
{{ Form::select('category', CategoryModel::list('title', 'id')) }}
Of course, you can generate that array in your controller and pass it down as you are doing now, too.

How can I use Laravel form model binding with bootstrap?

I am trying to display a bootstrap formatted form bound to my User model. However, the form-model binding seems to only work out when I use input fields that are not formatted with bootstrap styles.
Here is one without the bootstrap class, which perfectly displays what I have in my db when I open my form with:
{{ Form::model($user) }}
{{ Form::label('name', 'Vorname:')}}
{{ Form::text('name')}}
Here is the second input of this form, now with the bootstrap class. This one does not display the db content.
{{ Form::label('sirname', 'Nachname:')}}
{{ Form::text('sirname', '', array('class'=>'form-control'))}}
How can I fix this? Is there a way to use form-model binding and styling the inputs with bootstrap classes at the same time?
Any help would be appreciated.
Thanks!
Try the following:
{{ Form::model($user, array('route' => 'xyz')) }}
{{ Form::label('name', 'Vorname:')}}
{{ Form::text('name', null, array('class' => 'form-control'))}}
{{ Form::label('sirname', 'Nachname:')}}
{{ Form::text('sirname', null, array('class' => 'form-control')) }}
{{ Form::close() }}
if you use form model, do not forget to close it.

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