Laravel Form: Input Button Not Showing HTML Icon - forms

I'm creating a button with Form at Laravel:
{!! Form::submit('<i class="fa fa-trash"></i>', ["class"=>"btn btn-default"]) !!}
The button text becomes:
<i class="fa fa-trash"></i>
I see only this string, not the icon: how to fix it?

Try to use Form::button instead of Form::submit:
{{ Form::button('<i class="fa fa-trash"></i>', ['type' => 'submit', 'class' => 'btn btn-default'] ) }}
It will create a button tag with type submit instead of an input tag.
This way the html of the icon should be rendered inside the tag content and should be visible.
Instead, by using an input tag like you are doing, the html string of the icon would be printend inside the value attribute of the input tag, and here it couldn't be rendered as valid html

Related

Get checkbox checked after laravel validation error

I need to keep the checked checkboxes in the form as checked after I get the validation errors in the Controller.
Actually I find quite horrendous my implementation of the checkboxes in my form.
Could you please provide some advise? Thanks
<div class="form-group">
{!! Form::label('call', 'Llamar:', ['class' => 'control-label']) !!}
{!! Form::checkbox('call', isset($category)?(bool)$category->call:true, ['class' => 'form-control']) !!}
</div>
For the checkbox part, I did not use Laravel Forms. I used normal html "input" tag instead, and used "old" to check for previous value
<input type="checkbox" name="call" value="1" {{(old('call') == "1") ? 'checked': ''}}>Call
Try using old to check the previous value and set the value to checked or not based on that.
You can do it via the form builder much easier like this:
{!! Form::checkbox('call', 1, false, ['class' => 'form-control']) !!}
call = your checkbox name
1 = your checkbox value if it's checked (change to whatever you want)
false = don't load it checked on first page load (setting true will load it always checked)
['class' => 'form-control'] = it will add the class to it
That's all.
You don't need to worry about any kind of values, it will be set automatically.

Laravel Form helper not show data after validation error

i'm having problem with forms helper in laravel 5.3.
I have a foreach that shows my data in a form.
Everythins work fine, until i submit a form and validation fails.
Therefore, when validation fails, the input text in my form not show the previus data, but only the data which were submitted.
Here the code:
#foreach($cars as $car)
{!! Form::model($car, ['route' => ['cars.update', $car->id], 'method' => 'PUT']) !!}
<div class="col-md-2">
{{ Form::label('name', $car->name) }}
{{ Form::text('name', $car->name, ['class' => 'editbox-normal']) }}
</div>
<div class="col-md-2">
{{ Form::label('phone', 'Phone') }}
{{ Form::text('phone', null, ['class' => 'editbox-normal']) }}
</div>
<div class="col-md-2">
{{ Form::label('license_plate', 'License plate') }}
{{ Form::text('license_plate', null, ['class' => 'editbox-normal']) }}
</div>
<div class="col-md-2">
{{ Form::label('total_km', 'KM') }}
{{ Form::text('total_km', null, ['class' => 'editbox-normal']) }}
</div>
<div class="col-md-1 col-md-offset-3 flex-space-around">
{{ Form::submit('Submit', ['class' => 'btn btn-edit']) }}
</div>
{!! Form::close() !!}
#endforeach
If i submit one of those forms and validation fails, for example name is required and name input is empty, laravel flash $error, but every input of each form displays the data of the submitted form. In this example, every name input will be empty, and phone, license_plate and total_km will have the same value.
Instead, if i use pure html code for the input, everythinks work fine.
In addition, if you notice, the fist label has the value set to $car->name, the same of the text input, but in the label it is shows correctly, in the input not.
Anyone knows why?
Thanks in advance.
This is actually intended behaviour. This way, when you e.g. forget to enter your name, the rest of the data you entered isn't lost.
In your use case, however, this leads to unintended results (because you have multiple forms on the same page). I would advise splitting the forms up to different pages :)

Button open a new Blade page in Laravel 5.1

I have only seen how to open a Blade page with link, how do I use a button to do the same?
a button like this?
<input type="button"
onClick="parent.location='https://google.com'"
value="Google"
formtarget="_blank">
Using Form facade:
{!! Form::button('Google', [
'onClick' => "parent.location='https://google.com'",
'formtarget' => 'fromtarget'
])
!!}
And Using Form facade with url():
{!! Form::button('Delete account', [
'onClick' => "parent.location='" . url('delete/1') . "'",
'formtarget' => 'fromtarget'
])
!!}
You can do it with either a button or input element.
Here's an example with input:
<input type="submit" onClick="location.href = '{{ url('some/route') }}'">

Zend form and input button

I'm trying to add button input to the form element. I'd like the rendered code to be <input type="button" ..."> I tried setting attribs field like so:
$this->addElement('submit', 'cancel', array(
'ignore' => true,
'label' => 'Anuluj',
'attribs'=>array('class' => 'class_name', 'type' => 'button') )
);
but i still got <input type="submit" ..."> instead <input type="button" ...">
Setting class attribute works, but setting the type doesn't. Any Idea to get type="button"?
Use button as the first parameter instead, which will give you a <button> HTML element instead (functionally the same as <input type="button" ..>).
Please use the below code for button
$this->addElement('button', 'submitted');
$submitElement = $this->getElement('submitted');
$submitElement->setAttrib('class',"btn-primary btn");
$submitElement->setAttrib('value', 'submitted');
$submitElement->setLabel('SUBMIT');
The Html code is,
<button name="submitted" id="submitted" type="button" class="btn-primary btn">SUBMIT</button>

Creating a submit button

I can create the regular submit button in the form api but what if I want to do something like this
$form['required_text'] = array(
'#markup' => '<button name="submit" value="submit" type="submit" class="primary-submit submit" id="edit-submit">Submit - markup
<img src="/img/arrow.png">
</button>',
);
This does not send the form. What do I need to do or does it need to be an input field?
You might wahnt to use an image_button type instead. Also at the moment you're not really using the form API properly, you can just add markup to the form like you're doing but it doesn't register the element in the form and thus won't run submit/validate handlers. Something like this would work:
$form['required_text'] = array(
'#type' => 'image_button',
'#value' => 'submit',
'#src' => '/img/arrow.png'
);
With that element you'll get a an <input type="image" /> with the correct image loaded in to it's source.
Hope that helps