Button open a new Blade page in Laravel 5.1 - forms

I have only seen how to open a Blade page with link, how do I use a button to do the same?

a button like this?
<input type="button"
onClick="parent.location='https://google.com'"
value="Google"
formtarget="_blank">
Using Form facade:
{!! Form::button('Google', [
'onClick' => "parent.location='https://google.com'",
'formtarget' => 'fromtarget'
])
!!}
And Using Form facade with url():
{!! Form::button('Delete account', [
'onClick' => "parent.location='" . url('delete/1') . "'",
'formtarget' => 'fromtarget'
])
!!}

You can do it with either a button or input element.
Here's an example with input:
<input type="submit" onClick="location.href = '{{ url('some/route') }}'">

Related

Get checkbox checked after laravel validation error

I need to keep the checked checkboxes in the form as checked after I get the validation errors in the Controller.
Actually I find quite horrendous my implementation of the checkboxes in my form.
Could you please provide some advise? Thanks
<div class="form-group">
{!! Form::label('call', 'Llamar:', ['class' => 'control-label']) !!}
{!! Form::checkbox('call', isset($category)?(bool)$category->call:true, ['class' => 'form-control']) !!}
</div>
For the checkbox part, I did not use Laravel Forms. I used normal html "input" tag instead, and used "old" to check for previous value
<input type="checkbox" name="call" value="1" {{(old('call') == "1") ? 'checked': ''}}>Call
Try using old to check the previous value and set the value to checked or not based on that.
You can do it via the form builder much easier like this:
{!! Form::checkbox('call', 1, false, ['class' => 'form-control']) !!}
call = your checkbox name
1 = your checkbox value if it's checked (change to whatever you want)
false = don't load it checked on first page load (setting true will load it always checked)
['class' => 'form-control'] = it will add the class to it
That's all.
You don't need to worry about any kind of values, it will be set automatically.

Laravel Form update multiple records

I have a list of users, and for each user of the list there is a select box where the selected value role value is shown.
I was thinking a way on how allow to send all the modifications for each user with the submit button to a method of the controller which should update the role. I have no idea.
This is the form, which works correctly:
{!!Form::open(['url' => ['admin/users/edit'], 'class' => 'form-horizontal', 'method' => 'POST']) !!}
#foreach($users as $user)
<div class="form-group">
<div class="list-user-element">{!! $user->name !!}</div>
<div class="list-user-email">{!! $user->email !!}</div>
<div>
{!! Form::select('role', $roles, $user->role->label, ['class' => 'form-control']); !!}
</div>
</div>
#endforeach
<div class="form-group">
<div class="col-sm-offset-2 col-sm-5">
{!! Form::submit('Update', array('class' => 'btn btn-warning')) !!}
</div>
</div>
{!! Form::close() !!}
I don't know how to prepare the controller, neither which kind of info is passed to the controller.
Route::post('admin/users/edit', 'AdminController#showRegisteredUsersRoles');
And here it gives error:
public function editRegisteredUsersRoles(Request $request)
{
dd($request);
}
You could do the form like this
{!! Form::select('users[' . $user->id . '].role', $roles, $user->role->label, ['class' => 'form-control']); !!}
And then in the controller
public function editRegisteredUsersRoles(Request $request)
{
foreach ($request->users as $user_id => $role) {
//...
}
}
I'm not 100% sure of what your problem is, are you saying that there aren't all of the users' roles in the Request when you dd($request) ?
I think the problem lies in the fact that you are looping through the users throwing down select boxes with the same name. Generally, when you send a POST request, you access the information by saying $_POST[<Form's name>], so maybe there is a problem with duplication here. Perhaps you should try something like:
{!! Form::select('role' . $user->id, $roles, $user->role->label, ['class' => 'form-control']); !!}
What I changed above is simply tacking on the user's id to the name 'role' in order to make each of those unique.

When I submit a form on Laravel I get error message: MethodNotAllowedHttpException in RouteCollection.php line 219?

When I submit my form I receive the following error: 'MethodNotAllowedHttpException in RouteCollection.php line 219'.
How do I solve this? I have looked at various forums and other threads where this has been answered but none have worked for me.
My route file is as follows:
<?php
/*
This is the file in which the rules for how users will use the application are kept
*/
Route::get('/',function() {
return view('welcome');
});
Route::auth();
Route::get('/home', 'HomeController#index');
Route::resource('/questionnaires', 'QuestionnairesController');
Route::resource('/questions', 'QuestionsController');
Route::resource('/answers', 'AnswersController');
\
My create.blade.php file along with the form looks like this:
#extends('layouts.master')
#section('title', 'Create Questionnaire | SurveySays!')
#section('content')
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<h1>Create Questionnaire</h1>
<h3>Create your questionnaire using the form below. Give it a title, a small description and write your ethical considerations:</h3>
#if($errors->any())
<div class="alert alert-danger">
#foreach($errors->all() as $error)
<p>{{ $error }}</p>
#endforeach
</div>
#endif
{!! Form::open(array('url' => '/questionnaires/create')) !!}
<div class="container-fluid">
<div class="form-group">
{!! Form::label('title', 'Title:') !!}
{!! Form::text('title',null,['id' => 'title','class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('description', 'Description:') !!}
{!! Form::textarea('description',null,['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('ethics', 'Ethical considerations:') !!}
{!! Form::textarea('ethics',null,['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Create', array('class' => 'btn btn-success form-control')) !!}
</div>
</div>
</div>
{!! Form::close() !!}
#endsection
Thanks :)
You should use store method instead. Also, use route, not URL (you don't want to hardcode it):
{!! Form::open(array('route' => 'questionnaires.store') !!}
create method is used to display form to a user. And store used to validate and persist user input data into DB.
More about RESTful resource controllers here.
I see that you didn't setup any POST routes, that means that you cannot send a HTTP POST request to your Laravel site.
If you add this, it will be fine.
Route::post('/questionnaires/create', 'QuestionnairesController#create');
This will setup a route (HTTP POST request) to the QuestionnairesController with the function create().
Hope this works!
Because create only allows GET requests to show the form.
https://laravel.com/docs/5.2/controllers#restful-resource-controllers
Add 'method' => 'post' to your Form::open() array and point it at just /questionnaires.

Zend Form Rendering and Decorators (Use correctly with bootstrap)

The Bootstrap Example Code
http://getbootstrap.com/css/#forms
Copying a simple email input element from getbootstrap.com suggests we format the HTML in the following way:
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input id="exampleInputEmail1" class="form-control" type="email" placeholder="Email">
</div>
Above we have a <label> tag that closes straight after it's text content, "Email address".
I would now like to create the same form group using Zend Framework.
module/MyApp/src/MyModule/Form/MyForm.php
namespace MyModule\Form;
use Zend\Form\Form;
class MyModuleForm extends Form {
public function __construct($name = null)
{
$this->add(array(
'name' => 'email_address',
'type' => 'Email',
'options' => array(
'label' => 'Email address',
),
'attributes' => array(
'class' => 'form-control',
'placeholder' => 'Email'
)
));
The Zend Framework generated code
<div class="form-group">
<label>
<span>Email address</span>
<input class="form-control" type="email" placeholder="Email" id="exampleInputEmail1">
</label>
</div>
In the above HTML you can see that Zend has not closed the <label> tag. Instead the <label> ecapsulates its children.
How do I change the way the Zend rendering works?
I assume you are using ZF2 FormRow view helper to render your form element in your view script, e.g. $this->formRow($this->form->get('email_address'));
To render it differently you need to use the following view helpers
FormLabel
FormText
FormElementErrors
If for example you wanted to render as a definition list you would use something like
<dl class="zend_form">
<dt><?php echo $this->formLabel($this->form->get('email_address')); ?></dt>
<dd><?php echo $this->formText($this->form->get('email_address')); ?>
<?php echo $this->formElementErrors($this->form->get('email_address')); ?></dd>
</dl>
I hope this points you in the right direction

Laravel Form: Input Button Not Showing HTML Icon

I'm creating a button with Form at Laravel:
{!! Form::submit('<i class="fa fa-trash"></i>', ["class"=>"btn btn-default"]) !!}
The button text becomes:
<i class="fa fa-trash"></i>
I see only this string, not the icon: how to fix it?
Try to use Form::button instead of Form::submit:
{{ Form::button('<i class="fa fa-trash"></i>', ['type' => 'submit', 'class' => 'btn btn-default'] ) }}
It will create a button tag with type submit instead of an input tag.
This way the html of the icon should be rendered inside the tag content and should be visible.
Instead, by using an input tag like you are doing, the html string of the icon would be printend inside the value attribute of the input tag, and here it couldn't be rendered as valid html