Is there an easier way to output old form data with Twig in this example? Inside this ternary operator, it automatically escapes the value="" string which is undesired. I still want the old email value escaped.
<input type="text" name="email" id="email"{{ request.post('email') ? (' value="' ~ request.post('email')|e ~ '"')|raw : '' }}>
I'm not sure what request.post is, but assuming it behaves like Symfony's app.request.request.get (returning null if no such post param exists) you can simply do:
<input value="{{ request.post("email") }}" />
It'll be the value if it exists, or an empty value if it doesn't. If you are a purist and do not wish to see an empty value attribute, you can do this:
<input{% if request.post("email") %} value="{{ request.post("email") }}"{% endif %} />
Related
what I'm trying to do is a filter radio button. When a user apply the filter based on a specific year, it goes into the controller to filter data results based on that year and return back to the view with new values. However when the page refreshes, the value filtered by the year is reflected, just that the radio button is unchecked and the default All radio button is checked.
What I did was store the radio button's id in a Session at the controller based on value of which radio button has been clicked. If the id of the radio button matches the id stored in the session, it should checked that radio button.
Index.blade.php
<label>
<input type="radio" name="year" id="tab" value="_all" class="flat-red filter" checked="checked"> All
</label>
#foreach ($years as $year)
<label>
<input type="radio" name="year" id="id{{$year->year}}" value="{{ $year->year }}" class="flat-red filter" checked = {{ Session::get('radio') == "id{{$year->year}}" ? 'checked' : 'false' }}> {{$year->year}}
</label>
#endforeach
<button type="submit" class="btn btn-primary btn-sm pull-right">Apply Filter</button>
StatisticsController.php
session(['radio' => 'id'.request('year')]);
return view('statistics.index', $counts, compact('years'));
Error that I am getting
syntax error, unexpected ')', expecting :: (T_PAAMAYIM_NEKUDOTAYIM)
Method(s) I have tried,
1. Using JS to see which radio button has been click and store it in the local session.
What am I doing wrong or missed out?
Everything inside of {{ }} is PHP. The {{ }} gets replaced by the Blade Compiler with something like this:
<?php echo e(...); ?>
So basically just use regular PHP syntax between {{ }}:
{{ Session::get('radio') == "id{$year->year}" ? 'checked' : 'false' }}
Having {{ }} inside of {{ }} is going to break the compiling of that echo statement.
A solution could be to read that session variable in your controller and inject it into your template.
Don't put more logic into the template, that's not what a template engine is made for - better decouple such logic. This helps you to keep your code maintainable. A blade template does not need to know anything about a session
I have two DB tables as item and price.
In view file, foreach of items I have form element as
<input type="text" name="dc_{{$item->id}}" value="{{$price->dc_*}}">
I want * in value as dc_{{$item->id}} for an example like dc_4 or dc_7
I am unable to use {{ $price-> dc_{{$item->id}} }} for value as it is wrong syntax.
What's an alternative way to implement such logic?
You can do
<input type="text" name="dc_{{ $item->id }}" value="{{ $price->{ 'dc_' . $item->id } }}">
I've form with checkboxes:
{{ form_ajax('onAjaxList') }}
{% for soc in socs %}
<input type="checkbox" name="Filter[soc_id]" value="{{soc.id}}" id="
{{soc.name}}">
{% endfor %}
{{ endform() }}
And i tried get values from controller:
$options = post('Filter',[]);
but in $options i get only last checkbox value and i need full array checkboxes. How to do it?
Your input name should be like name="Filter[soc_id][]" instead of name="Filter[soc_id]" so change your input from
<input type="checkbox" name="Filter[soc_id]" value="{{soc.id}}" id="
{{soc.name}}">
TO this
<input type="checkbox" name="Filter[soc_id][]" value="{{soc.id}}" id="
{{soc.name}}">
When you are dealing with multiple checkbox you always need to use array.
so adding [] square brackets after name will make it array and add data index will start from 0 index.
if you need more control you can also pass name="Filter[soc_id][0]" , name="Filter[soc_id][1]", name="Filter[soc_id][2]" ... manually if needed to use saved record's id, now array key will be your record's id.
In your case your code should look like this
{{ form_ajax('onAjaxList') }}
{% for soc in socs %}
<input type="checkbox" name="Filter[soc_id][]" value="{{soc.id}}" id="
{{soc.name}}">
{% endfor %}
{{ endform() }}
and from server side if you want to use filter
you can use this (October CMS, laravel)
$options = post('Filter',[]);
$query->whereIn('soc_id', $options['soc_id']);
this will do the job, filtering based on soc_id.
if you have any doubts please comment.
I am using select2 for selection of users, I am populating select2 according to its value in database, I am doing it like this :
<input class="form-control" data-tag-select="director" remote-url="{{ route("query::applies")}}" name="agency_director" type="hidden" value="{{ $team->name }}">
In the value simply i am adding the variable {{ $team->name }} which have the value but nothing comes up.
What am i missing ?
I have a loop of checkboxes with multiple attributes. One content may have many attributes, so user can check more than one attribute.
If some validation errors occurs in this form I need to pre-fetch already checked attribute.
#foreach($attributes as $entry)
<div class="check-line">
<input type="checkbox" id="cat4" class='icheck-me'
name="attribute[<?php echo $entry->id; ?>]"
data-skin="square"
data-color="blue"
value="{{$entry->id}}"
<?php
if(Input::old('attribute['.$entry->id.']')== $entry->id) {
echo 'checked="checked"';
}
?>
/>
<label class='inline' for="cat4">
<strong>{{$entry->name}}</strong>
</label>
</div>
#endforeach
I tried above but no luck.. any ideas?
From Laravel docs on Requests:
"When working on forms with "array" inputs, you may use dot notation to access the arrays:"
$input = Input::get('products.0.name');
So you should be able to do this fpr Input::old() as well:
<?php
if(Input::old('attribute.' . $ii) == $entry->id) {
echo 'checked="checked"';
}
?>
A slightly more catch-all answer to this would be to do an array search rather than just checking the current index. As such, I see the code looking like this:
#foreach($attributes as $entry)
<div class="check-line">
<input type="checkbox" id="cat4_{{{ $entry->id }}}" class="icheck-me"
name="attribute[]"
data-skin="square"
data-color="blue"
value="{{{ $entry->id }}}"
#if (in_array($entry->id, Input::old('attribute')))
checked
#endif
/>
<label class="inline" for="cat4_{{{ $entry->id }}}">
<strong>{{{ $entry->name }}}</strong>
</label>
</div>
#endforeach
So first off I've made the code more Blade-compliant:
Any echos are now {{{ and }}} rather than the previous mix of <?php echo ___; ?> and {{/}} (I went for {{{ rather than {{ as it's not HTML being echoed and it's better to be safer)
The <?php if () { is now a Blade #if ()
The name attribute is now just a standard array (doesn't include indices, as it doesn't need to), however the id attribute previously gave all checkboxes the same ID. While browsers will let you do this, it's technically illegal HTML, so I've appended the entry ID to each checkbox.
Finally, the if condition no longer checks that the value of the input with the current index matches the current entry's ID, but instead searches to see if the current entry's ID is anywhere in the returned array. This protects against the possibility of your entries being returned in a different order to the previous time they were on the page.
We now don't have a reliance on the $ii variable, so it can be removed too.
Caveats:
In doing this I've made the code a little nicer, but the code is no longer identical. Without knowing exactly why you use the $ii variable in order to provide keys to your attribute array I can't say that using my code will work correctly. However, assuming it was just there to help with this old input thing, then it's fine.
Also, my change of {{ to {{{ may have consequence I don't know about. I'd have thought for the $entry->id stuff it'd be fine, but maybe $entry->name in the <label> need to be unescaped HTML for a reason. Feel free to change this back.