Laravel 4 Blade Form Formatting (Hidden Input) - php

I have an "artist" resource, and on the show route page I have a form. I am using Laravel Blade syntax. As part of this form, I am trying to send the page ID to the back end of a Laravel 4 project. I am doing it this way:
{{Form::hidden('artist-id',null, array(
'id' => '{{$artist->id}}',
));}}
However, when using this, the browser throws an error, because it is not rendering the $artist->id variable within the brackets before the outside brackets of the input form.
Is there a way around this or another way to pass back this variable for the resource? Thank you!

So anything inside the blade tags {{ }} is treated as standard PHP. Using further {{ }} inside existing blade tags is just going to throw a big error.
Because anything inside the blade tags is treated as PHP you can simply do the following
{{ Form::hidden('artist-id', null, ['id' => $artist->id]) }}
Although posting that input isn't going to give you the value you want, because the value supplied is null. The id attribute is the html ID given to the html attribute. You need the following to set the value on the input, which will then be posted in with your form data
{{ Form::hidden('artist-id', $artist->id) }}

You don't need to nest your blade call like that, this should work:
{{ Form::hidden('artist-id', null, array('id' => $artist->id)) }}

Related

laravel 5.6 insert an href into message redirect

I have a problem to insert a redirect link into a message in a redirect laravel command.
This is what I woould to have:
return redirect('validation')->with('warning','to correct mistake, click here');
and after click on the here, I have to load another page with the url mapped on here.
Something like this, but this solution not work:
return redirect('validation')->with('warning','to correct mistake, click here');
I think you using the this in you view to show the warning.
{{ session('warning') }}
You need to use these brackets in your view to show the warning and to allow the html to be processed by the browser.
{!! session('warning') !!}
These brackets {{ }} escape html tags and will render html as plain text to prevent XSS attacks.
If you want to inject html from the backend you must use {!! $html !!} which will render the html as html.
You can checkout the Displaying Date -- Laravel Docs for a better explanation.

Twig variable inside other twig variable?

So, I've been working on a website for a reggae backing band and on the 'booking' page I have a form. After submitting the form I first (ofcourse) check if all required fields are filled in, if something else went wrong or if it succeeded.
But, I have a couple of language files (nl, de, fr and en). On the HTML page I first check if an error was set inside the PHP and if there is an error, I want to output it in the right language.
So in case of an error, the HTML would have to look like this
{{ lang.booking.{{ form_error }} }}
because I am sending the error type in the PHP as well, but this doesn't seem to be possible (obviously).
Can someone help me out or tell me how to get around this problem?
Thanks in advance!
You can use the attribute function to access a dynamic attribute of a variable.
{{ attribute(lang.booking, form_error) }}
You can use translation on constraint messages as you can learn in this article: http://symfony.com/doc/current/validation/translations.html

Symfony2 Forms GET submit field type - remove 'name' param

I'm using submit form type in my FormType class (I want it to be customized for css classes and button label).
Since it's a search, I decided to go with GET, not default POST. What I noticed in Symfony2, is that:
submit field gets a "name" parameter
and when form is submitted, one of params in url is an empty "submit" (i.e.: http://url....?phrase=searchphrase&submit)
I tried to remove auto-genarated HTML "name" attr from the "button / submit" HTML tag in controller, in form type class, and even in Twig template, e.g. by overriding "name" - nothing seems to work for Symfony2, and the name="submit" for this button is always generated.
It there a way to remove this HTML attr from submit button, or am I forced to only render whole submit button by myself, and remove it from Form Type class?
Template for the search form is very basic, nothing extraordinary for Symfony2:
{{ form_start(form) }}
<div>
{{ form_widget(form.target) }}
</div>
<div>
{{ form_widget(form.phrase) }}
{{ form_widget(form.submit) }}
</div>
{{ form_errors(form) }}
{{ form_end(form) }}
And for the form type, I use standard submit:
$builder->add('submit', 'submit', array('label' => 'search', attr(name => null) ....)
As you see, here name attr is explicilty set to null. Symfony2 generate it any way, and give it a "submit" value to this 'name' attr.
The only problem is: can I make somehow Symfony2 to NOT generate "name" attr in the HTML Button/Submit tag? If Symfony2 cannot do that, it seems that the only way is to simply remove "submity" from Form Type class, and put HTML for this button directly in the template by myself, which I'm trying to not do (but if it's not possible, I will have to).
To me, it seems like it's a problem with Symfony2. Submit type is quite new thing in forms here, and I can imagine that auto-generated "name" attr follows the same rules as other form types in Symfony2 - although it is really not needed in HTML forms!
SOLUTION: So, I ended up with rendering it all by myself, and removed it from Form Type. Still I think Symfony2 shouldn't generate 'name' for this particular tag - I saw never ever "name" attr being assigned to SUBMIT button HTML tag in any form on the web, it's obviously not needed and not desired.
I think there are three solutions:
Have a form without the submit button and create the button outside of the form. Then submit the form with JS.
Override the default twig template responsible for generating HTML content of the submit button.
Generate the HTML content by yourself and not use the name there.

How to dynamically generate HTML with Blade

In Laravel 5 and Blade, I'm using a form with a textarea to store text in a mysql database and then display it back out to the user. I want to be able to preserve the carriage returns. I tried the simple approach of using the nl2br()
function which replaces the \r\n with tags:
public function setBodyAttribute($data){
$this->attributes['body'] = nl2br($data);
}
It seems that Blade won't allow html to be displayed as HTML.
{{ $example->body }}
What is the best way to get around this?
Since you mentioned Laravel 5, you're looking for:
{!! $example->body !!}
In Laravel 4, {{ $data }} would echo data as is, whereas {{{ $data }}} would echo data after running it through htmlentities.
However, Laravel 5 has changed it so that {{ $data }} will echo data after running it through htmlentities, and the new syntax {!! $data !!} will echo data as is.
Documentation here.

array filed values not working in Laravel blade template system?

I have 2 email fields and I am using
{{ Form::text('email[]', Input::old('email'),array('class' => 'large-2', 'placeholder' => 'email address','id'=>'email')) }}`
if I use [] to get multiple values for same variable, it is giving error in view page if posted back some data. for example if some fields are mandatory and if user fail to fill those, page will be redirected to same view page from where it was launched.
In such cases it is showing error.
How to fix this issue?
One text field can contain only one data. So, you need another text field to achieve that. if you don't want to show the multiple emails, you can use hidden fields.
{{ Form::hidden('email[]', Input::old('email1'))
{{ Form::hidden('email[]', Input::old('email2'))
Or you can use select. Laravel allow array data if use select field.
For example:
{{ Form::select('size', array('L' => 'Large', 'S' => 'Small')) }}
http://laravel.com/docs/html#drop-down-lists

Categories