Laravel 4 Former::checkbox() always checked - laravel

I'm having an issue with the Laravel 4 Package Former and the use of checkboxes. I'm trying to Former::populate() a user edit form within a blade template but my checkboxes are always checked.
Here is my current code:
{{ Former::checkbox('is_admin')->text('Is Admin?'); }}
I've tried:
{{ Former::checkbox('is_admin')->text('Is Admin?')->check(false); }}
and
{{ Former::checkbox('is_admin')->text('Is Admin?')->forceValue(false); }}
To no avail.
My database field 'is_admin' is a boolean field. I thought maybe Former didn't like 1s and 0s but it doesn't uncheck my checkboxes even if I set a getter to return false.
Any Ideas?
Thanks

Sounds like a bug with Former.. Keep in mind it looks like you can overrule whatever happened with populate with populateField.

Related

Acessing property from blade view laravel

Im trying to show some data in my view and ONLY the id property is getting changed to some random number when its passed to the view. The other properties are okay.
If I try to access a property from a view like this: {{ dd($var[1]) }}, it works and I can see the id like this:
But if I try to access passing the id directly: {{ dd($var[1]['id']) }} or {{ dd($var[1]->id) }}, I just get this:
And here are some more examples from what I get accessing it directly:
If I use gettype() is this var I get Integer back, but it is actually a string as you can see in the first printscreen.
Why is my blade view doing this? And how can I fix it?
I ended up using another id I was receiving in the same array, looks like blade was decoding the string or something like that. Thanks for the answers.
in your controller please select id as new_id and try to print
$var[1]->new_id or $var[1]['new_id']
it will fix your problem
if fixed give a green tick and a thumbs up else leave a commetn will try to update you

Laravel 5.2 syntax for getting a session variable from array

In my sessions I have added some things OK and can show them OK, however I added the user info as an array
session()->put('userinfo',Auth::user());
but I cannot seem to recall for display or use on of the userino items, name.
What is the syntax?
Found the solution:
{{ session()->get('userinfo')->name }}

How to keep the line breaks from user input but also sanitize in blade?

I try to render an data from user textarea input saved in my database.
I need to keep the line breaks use nl2br,
and also want to santize to prevent malicious script by using blade {{{ }}}.
But {{{ nl2br($output) }}} wont work, the br tag would also be sanitize.
Please give me some hint, thanks.
For Laravel 4 users:
{{ nl2br(e($message)) }}
e($x) is equivalent to {{{ $x }}}.
Laravel 5 users:
{!! nl2br(e($message)) !!}
e($x) is equivalent to {{ $x }}.
Sawny's answer is a great one that really leverages the power of the Blade syntax well, except I would take it a step further. You can use Blade::extend to create your own Blade # shortcodes so I use the following:
Blade::extend(function($value, $compiler)
{
$pattern = $compiler->createMatcher('nlbr');
return preg_replace($pattern, '$1<?php echo nl2br(e($2)); ?>', $value);
});
Now in your Blade template all have to do is something like this:
<div>#nlbr($sometext)</div>
EDIT: I realized someone coming across this may very well wonder, "Where do I put the Blade::extend function?"
To be honest, it can go in a lot of places (and it depends on if you're using Laravel 4 or 5 as to the 'best' approach).
A simple place to put it is in the routes.php or global.php files as they will get picked up with the least effort. These are however, not the best files to put them in and you would be best off learning to create Laravel Providers.
simply use this remove br no need to write
this function nl2br
it work fine
{!!($mylogin->title) !!}

Setting the HTML Id of an element with laravel

I have to following drop down list
{{ Form::select('selected_site', $select_sites, Input::get('selected_site')) }}
it works fine and I see the name is "selected_site" but how do I set the HTMl Id of the element? I find nothing on this in documentation.
Thanks!
There's one more argument:
{{
Form::select(
'selected_site',
$select_sites,
Input::get('selected_site'),
array('id' => 'yourid')
)
}}
In this last one you can add anything else you need, 'class', 'data-whatever', etc.
There are 2 ways to do this.
You can setup a Label using the Laravel Form builder and use the same name on the control associated with it. As per the documentation at http://laravel.com/docs/html#labels (take a look at the note under this section) After creating a label, any form element you create with a name matching the label name will automatically receive an ID matching the label name as well.
As answered by #antonio-carlos-ribeiro, the 3rd parameter of the control takes a list of options on which you can setup the additional attributes for the control.
Hope this helps...

Pluralization is not working on form's validation message in Symfony 2.3

I am using Symfony 2.3.
I have created simple order form using $this->createFormBuilder in my controller. I am validating one Doctrine field with:
/**
* #Assert\Length(min=3)
*/
protected $name;
However, upon validation fail, I get a duplicated error message near the field:
This value is too short. It should have 3 character or more.|This value is too short. It should have 3 characters or more.
My template for this element is as simple as:
{{ form_errors(form.name) }}
{{ form_label(form.name) }}
{{ form_widget(form.name) }}
Everything else seems to bet working as expected, except for that validation error message being duplicated. As if pluralization would not be working. Anyone has a suggestion on why this would be failing?
EDIT
It seems that SonataAdminBundle is overriding form_errors block. How to remove that override from non-sonata controller?
Maybe the sonata's template is not in sync with the latest changes from the original template.
Can you create a PR or an issue on github, thanks
The problem was that I had set this in my config.yml:
# Twig Configuration
twig:
debug: %kernel.debug%
strict_variables: %kernel.debug%
form:
resources:
- 'SonataAdminBundle:Form:silex_form_div_layout.html.twig'
- 'SonataFormatterBundle:Form:formatter.html.twig'
The silex_form_div_layout.html.twig one overrides form_errors Twig block, and removing it fixes the problem.

Resources