Laravel input checkbox if selected on edit - laravel-5

I have an array that I am setting up as checkboxes
<?php $buslist = array('Brooklyn','Lakewood'); ?>
#foreach ($buslist as $buses)
{{Form::label('brooklyn_1',$buses)}}
{{Form::checkbox('BusList2[]', $buses,false, ['id'=> $buses]) }}
#endforeach
Then I switch it to an string with a , using implode
But when I try to edit my information, none of the checkboxes are selected and the information gets lost on update.
What code can i put into the blade checkboxes, if that bus is in my string list?

Checkboxes are finicky creatures. In your form you actually have to do something like this
{!! Form::hidden('new-group-user-member', 0) !!}
{!! Form::checkbox('new-group-user-member', true, NULL) !!} Member
When a checkbox is not checked nothing gets sent to the server so you never know when to change the value in the backend. If you add a hidden form field with the same name and a value of 0 it will get sent with the form even though the checkbox is not checked.

In third parameter use in_array($buses, $buslist) instead of false. Your blade code will look like this:
{{Form::checkbox('BusList2[]', $buses,in_array($buses, $buslist), ['id'=> $buses]) }}

Related

Best practice for storing checkbox and textarea in Laravel

I have already seen a few different approaches for storing
textarea and checkbox and then showing them on edit view and show view...
but I am still not satisfied and would like to hear from You, how do you handle the unchecked checkbox and
especially the textarea security on show and edit with some wysiwyg plugin like ckeditor or tinymce?
For example for checkboxes I am using this setter;
public function setActiveAttribute($value): void
{
$this->attributes['active'] = $value ? 1 : 0;
}
Is this the best way to do it?
And for textarea on submit I allow everything and then on edit view I show it like this:
{!! Form::textarea($key, htmlentities($value), ['class' => 'form-control ckeditor-field']) !!}
and on show view I'm using a Purifier (link below) and I show it like this
{!! \Purifier::clean($value) !!}
So I just wanted to know what are the best practices? Is this fine what I'm doing?
https://github.com/mewebstudio/Purifier

How to save multiple checkboxes in Laravel?

I have created a blade (using Laravel Collective) with a multipe checkboxes:
#foreach($subsc as $subsc)
<div>
{{Form::checkbox('checkbox['. $subsc->Scheme->Scheme_id .']', '1')}}
{!! Form::label('SchemeName', $subsc->Scheme->Scheme_Name.$subsc->Scheme->Scheme_id, ['class' => 'control-label']) !!}
</div>
#endforeach
Now I want to save each checked box in a table as the scheme_id. How do I do that?
I guess you could use an array as name to make it easier and try something like this.
Form::checkbox('schemeIDS[]', $subsc->Scheme->Scheme_id, true);
// Parameters checkbox: name, value, checked
In the controller function use
$schemeIDS = $request->get('schemeIDS'); // get all the checked values as array
foreach($schemeIDS as $schemeID)
{
// insert into the database
}

Finding a blade variable inside string

I'm passing a $emailtemplate variable into a view.
This $emailtemplate is a model with the following properties
(string) 'from_name', (string) 'from_email', (string) 'subject', (string) 'body', (string) 'email_type', (integer) 'status'
User's can create new instances of the $emailtemplate via a FORM on the site, where they can populate each of the fields above.
An example of the body as populated by users into the FORM would be exactly the following line (yes users will be writing HTML code into the form to be stored in the body property of the $emailtemplate):
<strong>Dear {{$user->first_name}} </strong>
I have created a view which will allow users to 'Preview' the email. The variables $emailtemplate and $user are both passed to the view (for the case of the preview the $user = Auth::user())
resources/views/emails/preview.blade.php:
{!! $emailtemplate->body !}}
This 'preview' view correctly renders the strong styling however does not identify the blade variable reference as it is stored within a literal string.
i.e. the HTML that is rendered is exactly
Dear {{$user->first_name}}
What is going wrong here? I want to allow Users to develop their own email templates via a form. I am passing both the $emailtemplate and $user into the view however because the $emailtemplate->body is a string datatype Laravel does not recognise the use of the blade variable between the parenthesis.
How can I fix this?
Thanks for your help
You can't do this since Laravel wont Re-render the blade output, to see and replace any variables that is injected by another variable!
Take a look at https://github.com/TerrePorter/StringBladeCompiler
So, you should evaluate the user defined template first, e.g.
<strong>Dear {{$user->first_name}} </strong>
will become:
<strong>Dear Foo </strong>
This evaluated <strong>Dear Foo </strong> will be stored in $evaluatedTemplate
Then you can use this in your main blade template:
{!! $evaluatedTemplate !!}
Also see:
https://github.com/Flynsarmy/laravel-db-blade-compiler

Laravel 4 - Showing edit form with OLD data input as well as DB information

Im making a edit form for my app and i was wondering if someone could tell me how to get the data from the database into my text field.
I can locate the record i need to edit based on the users click, and i can display the information if i do the following:
value="{{ $letter->subject }}"
BUT, the problem im having is that when i run it through the validation and there is an error, it comes back with the database information instead of the OLD data.
So my questions is. Is there a way to serve up the database information first and then when it goes through the validatior, validate the information the user has edited?
Currently to validate the text field and bring the data back incase of error, im using
Input::old('subject')
Is there a parameter for that old bit that allows me to put in the DB data?
Cheers,
Hey you could validate and return ->withInput() and then in your actual form, check if there is Input::old() and display it, otherwise display from the db.
example:
<input type="text" name="subject"
value="{{ (Input::old('subject')) ? Input::old('subject') : $letter->subject }}">
Or you could go the other way and define the variable and do a regular if statement, instead of the ternary one! Up to you to decide what you want to use!
All you need is form model binding http://laravel.com/docs/html#form-model-binding:
{{ Form::model($letter, ['route' => ['letters.update', $letter->id], 'method' => 'put']) }}
// your fields like:
{{ Form::text('someName', null, ['class' => 'someHTMLclass' ...]) }}
// no default values like Input::old or $letter->something!
{{ Form::close() }}
This way you form will be populated by the $letter data (passed from the controller for example).
Now, if you have on your countroller:
// in case of invalid data
return Redirect::back()->withInput();
then on the redirect your form will be repopulated with input values first, not the original model data.
Make it more simple and clean
<input type="text" name="subject" value="{{ (Input::old('subject')) ?: $letter->subject }}">
I'm not sure for Laravel 4 but in Laravel 5, function old takes second param, default value if no old data in session.
Please check this answer Best practice to show old value

Dropdownlist in a edit.blade.php form

please how can I put dropdown list in an edit form with the old selected value on default?
here is my example:
<div class="form-group">
{{ Form::label('Container', 'Container:') }}
{{ Form::select('Select_cont', $containers) }}
</div>
I don´t know where to put the code of the old new value selected in my view and what should it be.
Please don´t forget that when directing to edit.blade.php I wrote thisin the function of my controller
return View::make('audio.edit',array($container))
->with('containers', $containers)
thanks a lot for your help :)
Selected value is the 3rd param of Form::select, so:
{{ Form::select('Select_cont', $containers, $selectedPreviouslyKey) }}
How to get that key depends on your code and what's in the dropdown
Pass it to the view like this
return View::make('audio.edit',array('containers' => $containers, 'selectedPreviouslyKey' => $selectedKey));
or use compact() / with etc
The View::make call isn't entirely right. It should be:
return View::make('audio.edit')->with('containers', $containers);
or
return View::make('audio.edit')->withContainers($containers);
or
return View::make('audio.edit', array('containers' => $containers));
or
return View::make('audio.edit', compact('containers'));
Also: make sure $containers exists.
The documentation on the select tag. http://laravel.com/docs/html#drop-down-lists

Resources