How cast or convert array value string to boolean in php - php

I'm Working with Laravel ACL.In every Role have group of permissions. I represent the all permissions using checkbox. When I submit form it's return a permissions array like this.
{
read: "true",
create: "true",
delete: "false",
update: "false"
}
array value show as double quotes string ,but i want it as boolean
How can i cast array values string to boolean.
#foreach($role->permissions as $key=>$value)
<td>
<input type="hidden" name="permission[{{$key}}]" class="role" value="false" {{ $value==0 ? 'checked' : '' }}>
<input type="checkbox" name="permission[{{$key}}]" class="role" value="true" {{ $value==1 ? 'checked' : '' }}>
</td>
#endforeach
I want to array value like this.
{
read: true,
create: true,
delete: false,
update: false
}

HTML double quotes values. If your values are always going to be "true" or "false" then you can specifically check for "true".
// change your json into an array
$values = json_decode($yourValues, true) ;
// loop through and check for "true"
foreach($values as $key=>$value) {
$values[$key] = ($value == "true") ;
}
// if you need it as json again
$yourValues = json_encode($values) ;

For you case,
#foreach($role->permissions as $key=>$value)
<td>
<input type="hidden" name="permission[{{$key}}]" class="role" value="false" {{ (boolean)$value ? 'checked' : '' }}>
<input type="checkbox" name="permission[{{$key}}]" class="role" value="true" {{ (boolean)$value? 'checked' : '' }}>
</td>
#endforeach
Try like this
####edit####
$data=$_POST[];
foreach($data as $record){
$record=(boolean)$record;
//now $record is boolean
}

try like this
#foreach($role->permissions as $key=>$value)
<td>
<input type="hidden" name="permission[{{$key}}]" class="role" value=false {{ $value=='false' ? 'checked' : '' }}>
<input type="checkbox" name="permission[{{$key}}]" class="role" value=true {{ $value=='true' ? 'checked' : '' }}>
</td>
#endforeach

Related

String getting cut when converted from php to html [duplicate]

This question already has answers here:
How to set HTML value attribute (with spaces)
(6 answers)
Closed 11 months ago.
I have the following string.
$onclick= " return false;";
And I'm trying to insert it in my HTML code like this.
<input type="checkbox" id="grabados" name="grabados[]" value="no" onclick="return false;">
with this code
<input type="checkbox" id="grabados" name="grabados[]" value="{{ (is_null($partida['grabado']) ? 'no' : 'si') }}" {{ (is_null($partida['grabado']) ? '' : ' checked') }} {{ $partida['tipo'] == "otro" ? "onClick = ".$onclick : ""}}>
instead I'm getting this
<input type="checkbox" id="grabados" name="grabados[]" value="no" onclick="return" false;>
When it gets to the space the string is cut
I have tried escape sequences, HTML functions, string functions, everything with no results.
A little help will be appreciated.
This should get closed as a typo shortly, as you've forgotten to delimit the attribute value. In the meantime, here's an example of how to do this while still having readable code:
<input
type="checkbox"
id="grabados"
name="grabados[]"
#if (is_null($partida['grabado']))
value="no"
#else
value="si"
checked="checked"
#endif
#if ($partida['tipo'] === "otro")
onClick="{{ $onclick }}"
#endif
/>
Also note, if you're creating multiple of these, you are repeating your id attribute, which is not allowed.
Try This
<input type="checkbox" id="grabados" name="grabados[]" value="{{ (is_null($partida['grabado']) ? 'no' : 'si') }}" {{ (is_null($partida['grabado']) ? '' : ' checked') }} {{ $partida['tipo'] == 'otro' ? "onClick = '".$onclick."'" : ""}}>

I have got two associative arrays of different lengths. I want to know how to compare values and apply to checkboxes?

There are two associative arrays of different lengths, how to compare their values and apply to checkboxes? If the values match, I need the checkbox gets the attribute checked.
Now I have this:
Politics
Video (Checked)
Policy (Checked)
Video
I want
Video (Checked)
Policy (Checked)
My code at the monent:
#foreach($category as $categories)
<input type="checkbox" aria-label="{{$categories->title}}" value="{{$categories->title}}" name="category[{{$categories->id}}]"> {{$categories->title}}
#foreach($post->category as $selected)
#if($selected->title == $categories->title)
<input type="checkbox" aria-label="{{$categories->title}}" value="{{$categories->title}}" name="category[{{$categories->id}}]" checked> {{$categories->title}}
#endif
#endforeach
#endforeach
You can apply that condition inside element to apply checked property
#foreach($category as $categories)
<input type="checkbox" aria-label="{{$categories->title}}" value="{{$categories->title}}" name="category[{{$categories->id}}]"> {{$categories->title}}
#foreach($post->category as $selected)
<input type="checkbox" aria-label="{{$categories->title}}" value="{{$categories->title}}" name="category[{{$categories->id}}]" {{$selected->title == $categories->title ? 'checked' : ''}}> {{$categories->title}}
#endforeach
#endforeach
I have resolved this problem
Controller:
$checkedCategory = [];
foreach($post->category as $selected) {
for($i = 0; $i < count($category); $i++) {
if($selected->title == $category[$i]->title) {
$checkedCategory[$i] = $category[$i]->title;
}
}
}
$checkedCategory = array_values(array_unique($checkedCategory, SORT_REGULAR));
Template:
<fieldset>
<legend>Категории</legend>
#foreach($category as $categories)
#if(in_array($categories->title, $checkedCategory))
<input type="checkbox" aria-label="{{$categories->title}}" value="{{$categories->title}}" name="category[{{$categories->id}}]" checked> {{$categories->title}}
#else
<input type="checkbox" aria-label="{{$categories->title}}" value="{{$categories->title}}" name="category[{{$categories->id}}]"> {{$categories->title}}
#endif
#endforeach
</fieldset>

How to get old input with value 0 in laravel?

To redirect back with previous input, it is common to have return redirect()->back()->withInput();. Then, to retrieve the previous value for radio button:
<input name="question1" value="0" type="radio" id="q1-0" {{ old('question1') == 0?'checked':'' }}/>
<label for="q1-0" class="label-margin">0</label>
<input name="question1" value="1" type="radio" id="q1-1" {{ old('question1') == 1?'checked':'' }}/>
<label for="q1-1" class="label-margin">1</label>
<input name="question1" value="2" type="radio" id="q1-2" {{ old('question1') == 2?'checked':'' }}/>
<label for="q1-2" class="label-margin">2</label>
<input name="question1" value="3" type="radio" id="q1-3" {{ old('question1') == 3?'checked':'' }}/>
<label for="q1-3" class="label-margin">3</label>
It works fine except for the one with value 0. When there is no previous input for question 1, old('question1') also return 0, making the choice 0 always chosen when return, where I want to keep all choices not chosen.
Is there a way to leave choice 0 not blank when there is no previous input?
try this. :)
if (old('input_name') !== null && old('input_name') == 0) {
// input is satified
}
You can try by comparing using 3 "=" like this : {{ old('question1') === 0 ? 'checked' : '' }}.
This will make sure that old('question1') is really 0 and not false or null.
Do like this
<?php if( old('question1) == '0'){echo "checked";} ?>

Checkbox auto checked based on value from database

how can I auto check the checkbox based on the value from the database? I have the following code:
<input type="checkbox" name="CarModel" value="Yes" ($row[CarModel]==Audi? 'checked' : '') >
it should be like this
<input type="checkbox" name="CarModel" value="Yes" <?php echo ($row['CarModel'] == 'Audi' ) ? 'checked' : NULL ; ?> >

Laravel form checkbox checked flag not working correctly

I have a piece of template:
#foreach ( Permission::all() as $permission )
{{ Form::checkbox('permissions[]', $permission->id, $role->permissions->contains($permission->id) ) }} <label>{{ $permission->label }}</label>
{{ var_dump($role->permissions->contains($permission->id)) }}
#endforeach
this outputs the following html:
<input checked="checked" name="permissions[]" type="checkbox" value="1"> <label>View entity</label>
<small>boolean</small> true
<input checked="checked" name="permissions[]" type="checkbox" value="3"> <label>Edit Entity</label>
<small>boolean</small> true
<input checked="checked" name="permissions[]" type="checkbox" value="4"> <label>Delete entity</label>
<small>boolean</small> false
W T H is going on. anyone any ideas?
EDIT
even moreconfusion. If I pass in false to the Form::checkbox function. Only the first item is rendered without the checked parameter. I give up :(
Found the 'problem'. See this issue on github: https://github.com/laravel/framework/issues/5078 :
my model being parsed has the permissions() array. The false will be ignored and the checkbox will be shown checked. Even if the $permission->id is not related to the Role.
FormBuilder#getCheckboxCheckedState
return is_array($posted) ? in_array($value, $posted) : (bool) $posted;
this line will return true, even if the $permission->id is set. Or the 'checked state' parameter is given.
Perosnally I think that all those isChecked checks should only be performed if $checked is_null.
FormBuilder#checkable()
$checked = $this->getCheckedState($type, $name, $value, $checked);
should be
if ( is_null($checked) ) $checked = $this->getCheckedState($type, $name, $value, $checked);

Categories