Side by side form fields with Laravel and Former? - php

I'm using Former with Laravel to build a form, and I'm trying to get two fields to be next to each other (in this case, first and last name) and then an email field below it. I've tried adding using Former::col_md_6_text, and adding ->addClass('col-md-6') and neither seemed to do much of anything. Below is the code I'm using in the form:
{{ Former::text('first_name')
->label("First Name")
->placeholder('First Name')
->required();
}}
{{ Former::text('last_name')
->label("First Name")
->placeholder('Last Name')
->required();
}}
{{ Former::text('email')
->label("Your Email")
->placeholder('Your Email')
->required();
}}
Any ideas?

This is how I've done it in the past - I dont think Former could wrap the field in another div
<div class="col-md-6">
{{ Former::text('first_name')
->label("First Name")
->placeholder('First Name')
->required();
}}
</div>
<div class="col-md-6">
{{ Former::text('last_name')
->label("First Name")
->placeholder('Last Name')
->required();
}}
</div>
There is an open github issue on this exact thing. There are some workarounds in there - but at the moment doesnt seem to be a natual option.

Related

Symfony 4.4 functional test delete form not working

I have an multi-delete functionality (kind of like PHPMyAdmin, checkboxes you can check to delete multiple items). The functionality itself works, but I'm implementing it in my functional test. However, somehow it doesn't work.
This is the code of my service (where multi-delete is handled, this is in a function that's called within controllers)
$form = $this->formFactory->createBuilder()->setMethod('DELETE')->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->deleteKeys($request);
return new RedirectResponse($this->session->get('referer'));
}
The function renders a template with the form, a button and a list of all items being deleted. This is the template:
{{ form_start(form) }}
<div class="row">
<div class="col-md-12">
<div class="box">
<div class="box-body">
{{ form_widget(form) }}
<p>{{ 'standard.Are you sure you want to delete the following items'|trans }}?</p>
<p>
{% for item in items %}
<strong class="d-block">{{ item.displayProp }}</strong>
<input type="hidden" name="items[]" value="{{ item.id }}">
{% endfor %}
</p>
</div>
<div class="box-footer">
<input class="btn btn-danger" type="submit" name="delete_items" value="{{ 'standard.Delete'|trans }}">
<a class="btn btn-default" href="{{ app.session.get('referer') }}">{{ 'standard.Cancel'|trans }}</a>
</div>
</div>
</div>
</div>
{{ form_end(form) }}
So, testing it via my browser, it works completely fine. Then, lets look at the code in my test. The code to get to the confirm page works, it shows the right items selected to delete. But it contains a form:
$form = $crawler->selectButton('delete_items')->form(null, 'DELETE');
$crawler = $this->client->submit($form);
So, it selects the form based on the delete_items button name (which exists), it doesn't throw an error or something, it submits the form, but after it, getting the HTML of $crawler still displays the "deleted" items. Again, if I test the functionality in the browser in the module itself, it works, but in the functional test it somehow doesn't.
Also I tried using $this->client->followRedirect() which says the request isn't redirected. But it's just a normal delete form without validation so I don't see why it shouldn't work.
EDIT:
Apparently somehow the form isn't submitted, var_dump($form->isSubmitted()); says false. I'm not sure why it isn't submitted though since it does get submitted if I test it in the browser though.

How to set autofocus on Laravel input field using Form::text whilst still setting the class

I can't seem to set autofocus on a input field in Laravel 5.4, whilst also setting the class of the element.
What I've tried:
{{ Form::text('email', Input::old('email'), ['class'=>'field-Login'], array('autofocus'=>'autofocus'))}}
{{ Form::text('email', Input::old('email'), array('autofocus'=>'autofocus'), ['class'=>'field-Login'])}}
{{ Form::text('email', Input::old('email'), array('autofocus'=>'autofocus',['class'=>'field-Login']))}}
You need to combine your attributes. For example:
echo Form::text('email', 'default#value.com', ['class' => 'class123', 'autofocus']);

How do I give a variable to my blade and save them to a variable?

My problem is a little bit complicated to explain. I'm doing a blog and did something like a topic section. I have a topic table and a thread table. In my thread table is a 'topic' attribute. No I want that if I'm doing a new thread, I also want to save the topic, the user currently is in right now.
My send button with the variable is this:
<a href="{{ action('Test\\TestController#add', [$thread->thema]) }}">
<div class="btn btn-primary">Thread hinzufügen</div>
</a>
My add-route:
Route::get('/add/{thread}', 'Test\\TestController#add');
My controller function just send's me to the thread creating form.
My creating thread - form :
{!! Former::horizontal_open()->action(action('Test\\TestController#store')) !!}
{!! Former::text('thread')->label('Title:')->autofocus() !!}
{!! Former::textarea('content')->label('Content')->rows(10) !!}
{!! Former::large_primary_submit('Add Thread') !!}
{!! Former::close() !!}
Well, after I pressed the submit button, the thread get saved, but without the topic! :/
According to the following route:
Route::get('/add/{thread}', 'Test\\TestController#add');
You'll get the $thread->thema inside your TestController#add method so your method should be able to recieve that param/variable, for example:
public function add($thread)
{
// Now you may pass the $thread to form and keep the value in a hidden
// text box, to pass to the for the form, add the $thread using with:
return view('FormView')->with('thread', $thread);
}
In the form, create a hidden input:
<input type="hidden" name="thread" value="{{ old('thread', $thread) }}" />
Or maybe this (if it works, not sure about the former tho):
{!! Former::hidden('thread', old('thread', $thread))->label('Title:')->autofocus() !!}

Laravel4+Bootstrap: How to make fields red on validation error

Inside a Laravel4 + Bootstrap 2.3.1 I have a form properly working with validation.
There are three fields obligatory: Name - Email - Phone.
When nothing is inserted, or the email is not in a proper format, the error messages are displayed.
But besides this, I would like to make the fields red, to show better where the error is.
How can I do this in Laravel + Bootstrap?
This is the form with the three fields obligatory:
<form name="room" method="post" class="narrow_width">
<label><span><i class="icon-user"></i></span> Name <span class="color-red">*</span></label>
{{ Form::text('name',Input::old('name'), array('class' => 'span7 border-radius-none')) }}
<label><span><i class="icon-envelope-alt"></i></span> Email <span class="color-red">*</span></label>
{{ Form::text('email',Input::old('email'), array('class' => 'span7 border-radius-none')) }}
<label><span><i class="icon-phone"></i></span> Phone number <span class="color-red">*</span></label>
{{ Form::text('phone',Input::old('phone'), array('class' => 'span7 border-radius-none')) }}
<p><button type="submit" name="submit" class="btn-u">Send Message</button></p>
</form>
Thank you very much!
I don't think there's an easy way to do this with the laravel Form class. I personally use my own package https://github.com/AndreasHeiberg/theme for this. You can use it if you wan't but it's subject to change.
Anyway raw code to do this is the following:
<div class="control-group {{ $errors->has($id) ? 'error' : false }}">
<label for="{{ $id }}" class="control-label">{{ $text }} {{ $required ? '<span class="required-red">*</span>' : ''}}</label>
<div class="controls">
<input type="{{ $type }}" id="{{ $id }}" name="{{ $id }}" value="{{ $value }}">
#if ($helpText)
<span class='help-inline'>{{ $helpText }}</span>
#endif
#foreach($errors->get($id) as $message)
<span class='help-inline'>{{ $message }}</span>
#endforeach
</div>
</div>
This being the important part
<div class="control-group {{ $errors->has($id) ? 'error' : false }}">
You can wrap this up in a form macro http://laravel.com/docs/html#custom-macros use a helper function or my package to do this.
With my package you would just use:
+#formText('name')
You can easily use Form macros, there is a bunch available here for Bootstrap 3:
http://forums.laravel.io/viewtopic.php?id=11960
Hope this helps...
Thanks for your efforts, especially #AndHeiberg.
For reason of simplicity, i decide to renounce to Laravel-Bootstrap validation and use instead a jquery plugin: https://github.com/jzaefferer/jquery-validation
The main reason is that is extremely easy to use.
It's enough to call the plugin and insert 'required' into the tag.
And the plugin will do all the rest, highlighting the field in red and displaying an error message for that field.
If your intention is to make the error more obvious to where it is, you can wrap the message text that is returned from Laravel validation errors in a span and give styling to that.
{{ $errors->first('email', "<span class='error'>:message</span>")}}
where :message is a "placeholder" for your error message. And then you can give any styling to .error spans as you wish. Check this Laracast lesson for more details (after 3:10).
In bootstrap, you can create errorfield with the id "inputError":
<input type="text" class="form-control" id="inputError">

Symfony2 I cannot submit a valid form from the Twig template

I am trying to make a search by date form to work on Symfony 2.3. I have an entity with a few fields (5) the name of the entity is Schedule and two of this fields are datetime, for start date time and end Date time. I want to search by dates, but it is giving me headaches.
I have this action:
public function indexAction(Request $request)
{
//time form creation
$aSchedule = new Schedule();
$dateTimeForm = $this->createFormBuilder($aSchedule)
->add('startDateTime', 'datetime')
->add('endDateTime', 'datetime')
->add('search', 'submit')
->getForm();
//getting the formr using post
$dateTimeForm->handleRequest($request);
if ($dateTimeForm->isSubmitted()){
echo 'Submited';
}
if ($dateTimeForm->isValid()){
echo 'Is Valid';
}
}
I have show the form in template like this:
<form action="{{ path('osd_sch_homepage') }}" method="post"
{{ form_enctype(dateTimeForm) }} >
<div id="start-date-time">
{{ form_label(dateTimeForm.startDateTime) }}
{{ form_errors(dateTimeForm.startDateTime) }}
{{ form_widget(dateTimeForm.startDateTime) }}
</div>
<div id="end-date-time">
{{ form_label(dateTimeForm.endDateTime) }}
{{ form_errors(dateTimeForm.endDateTime) }}
{{ form_widget(dateTimeForm.endDateTime) }}
</div>
<div>
{{ form_widget(dateTimeForm.search) }}
</div>
</form>
Now in the action every time in send the form the "$dateTimeForm->isSubmitted()" works fine, but the "$dateTimeForm->isValid()" is not getting true, I mean is never going the "echo 'Is Valid';". what am I doing wrong?
Thank you in advanced.
Abel Guzman
It's probably not putting the auto generated CSRF token. Try putting {{ form_rest }} at the end.
Have you tried to debug your form errors?
foreach($form->getErrors() as $err){
echo $err->getMessage();
}

Categories