Limit maximum character input on Laravel form element - forms

So I have this form and I want to limit the user input on the text boxes to only 12, so here is the code
<div class="form-group">
{{ Form::label('Bname', 'Breed Name:') }}
{{ Form::text('bname', null, 12, ['class' => 'form-control']) }}
</div>

<div class="form-group">
{{ Form::label('Bname','Breed Name:')}}
{{Form::text('bname',null,12,['class' => 'form-control', 'maxlength' => '12'])}}
</div>

Related

Livewire How to set the width of jetstream dialog-modal?

I am using the dialog-modal of Jet-Stream, it works pretty fine but I don't understand how to fix its width.
Here below is the way I use it.
<x-dialog-modal wire:model="show_equipment_dialog" :maxWidth="'100%'">
<x-slot name="title"> {{ __('Equipment Dialog') }}</x-slot>
<x-slot name="content">
#livewire('equipment-lw', ['usage' => 'chooser'])
</x-slot>
<x-slot name="footer">
<x-secondary-button wire:click="closeEquipmentDialog">{{ __('Close the dialog') }}</x-secondary-button>
</x-slot>
</x-dialog-modal>
with this in dialog-modal
#props(['id' => null, 'maxWidth' => null,'usage'=>'database'])
<x-jet-modal :id="$id" :maxWidth="$maxWidth" {{ $attributes }}>
<div class=" px-6 py-4">
<div class="text-lg">
{{ $title }}{{$maxWidth}}
</div>
<div class="mt-4">
{{ $content }}
</div>
</div>
<div class="px-6 py-4 bg-gray-100 text-right">
{{ $footer }}
</div>
The maxWidth is passed to dialog-modal (I can see this thanks to the {{$maxWidth}} in the content slot) but has no effect on the x-jet-modal. What is the correct syntax?
Thank you for helping me.
In your modal just add maxWidth="size-you-want".
The available sizes are in resources\views\vendor\jetstream\components\modal.blade.php but you can add more. Those that are by default are sm, md, lg, xl, 2xl
<x-dialog-modal wire:model="show_equipment_dialog" maxWidth="md">
<x-slot name="title"> {{ __('Equipment Dialog') }}</x-slot>
<x-slot name="content">
#livewire('equipment-lw', ['usage' => 'chooser'])
</x-slot>
<x-slot name="footer">
<x-secondary-button wire:click="closeEquipmentDialog">{{ __('Close the dialog') }}</x-secondary-button>
</x-slot>
You can set the maxWidth of the modal in the vendor/jetstream/components "modal.blade.php" then find the switch statement of maxWidth
You can add the following in your modal component
$maxWidth = [
'sm' => 'sm:max-w-sm', 'md' => 'sm:max-w-md', 'lg' => 'sm:max-w-lg', 'xl' => 'sm:max-w-xl', '2xl' => 'sm:max-w-2xl', '3xl' => 'sm:max-w-3xl', '4xl' => 'sm:max-w-4xl', '5xl' => 'sm:max-w-5xl', '6xl' => 'sm:max-w-6xl', '7xl' => 'sm:max-w-7xl', 'full' => 'sm:max-w-full',
][$maxWidth ?? '2xl'];

Why does Symfony 3.4 form displays EntityType choices with label although 'label' => false was used?

I am working on an existing Symfony 3.4 based project and trying to add and render a new form. Although the 'label' => false option was used, the fields are rendered including a label. Why?
// Symfony
class SomeController extends Controller {
public function userListAction(Request $request) {
$users = $someService->getUsers();
$formBuilder = $this->createFormBuilder()
->add('users', EntityType::class, [
'label' => false, // also tested '' and 'someLabel'
'class' => 'AppBundle:User',
'choices' => $users,
'multiple' => true,
'expanded' => true,
]);
$variables = array(
'form' => $formBuilder->getForm()->createView(),
);
return $this->render('AppBundle:Pages:user_list.html.twig', $variables);
}
}
// Twig
{% extends 'AppBundle::layout.html.twig' %}
{% block page_content %}
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
{% endblock %}
This shows a list of checkboxes for all users including the username as label.
Where does Symfony get the information to use the username as label? As far as I know no custom form widget was defined for the User class. Is there any way to check this for sure? Maybe there is something hidden in the vendor bundles like FOSUserBundle?
Why is the 'label' => false option ignored?
Edit:
Different ways of rendering the form does not solve the problem:
{{ form_start(form) }}
{{ form_row(form) }}
{{ form_end(form) }}
Result:
<div id="form_users">
<div class="form-group">
<div class="checkbox">
<label for="form_users_547">
<input type="checkbox" id="form_users_547" name="form[users][]" value="547">
someUserName
</label>
</div>
</div>
</div>
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
Result:
<div id="form_users">
<div class="checkbox">
<label for="form_users_547">
<input type="checkbox" id="form_users_547" name="form[users][]" value="547">
someUserName
</label>
</div>
</div>
{{ form_start(form) }}
{% for userFormView in form.users %}
{{ form_row(userFormView) }}
{% endfor %}
{{ form_end(form) }}
Result:
Basically the same as before with form_row
You need to use ‘choice_label’ => ‘YOUR PROPERTY PATH’ in the field options.
Pretty match is written in the docs: https://symfony.com/doc/current/reference/forms/types/entity.html#choice-label
If the entity class cast to string then is used if is not it will throw an exception. It looks like your entity User cast to the user name and that’s why it works.
You should try to use {{ form_row(form) }} which should render the whole field correctly.

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 :)

Linking edit button from table to Laravel Edit form

I have a table that displays the data from my database. at the end of each row I have an Edit/Update Button. I would like it when clicking on the edit button it reference the the Edit Form.
My edit form works. I can access the data when visiting computers/{id}/edit, The form displays the current data and I can edit the data and submit the updates and it updates in the database (mysql).
This is my index.blade.php, which displays the table with the update button
#extends('layout')
#section('content')
<h1>Inventory</h1>
<table class="table table-striped">
<thead>
<tr>
<th>Last Name</th>
<th>First Name</th>
<th>Department</th>
<th>Building</th>
<th>Room</th>
<th>Manufacturer</th>
<th>Device</th>
<th>Model</th>
<th>Service Tag</th>
<th>Mac Address</th>
<th>Status</th>
<th>Comments</th>
</tr>
</thead>
<tbody>
#foreach($inventories as $inventory)
<tr>
<td>{{$inventory->lastName}}</td>
<td>{{$inventory->firstName}}</td>
<td>{{$inventory->department}}</td>
<td>{{$inventory->building}}</td>
<td>{{$inventory->room}}</td>
<td>{{$inventory->manufacturer}}</td>
<td>{{$inventory->device}}</td>
<td>{{$inventory->model}}</td>
<td>{{$inventory->tag}}</td>
<td>{{$inventory->macAddress}}</td>
<td>{{$inventory->status}}</td>
<td>{{$inventory->comments}}</td>
<td>
{{--Need the button to open up my edit form--}}
<button formaction="computers/{id}/edit">{{ trans('computers.edit') }}</button>
{{--<input type="submit" name="update" id="update" value="Update" class="btn btn-primary">--}}
</td>
</tr>
#endforeach
</tbody>
</table>
#stop
This is my form.blade.php - which is a partial that I include in my create.blade.php and edit.blade.php and both of these pages work.
<div class="row">
<div class="col-md-6">
<div class="form-group">
{!! Form::label('lastName', 'Last Name:') !!}
{!! Form::text('lastName', null, ['class' => 'form-control' ]) !!}
</div>
<div class="form-group">
{!! Form::label('firstName', 'First Name:') !!}
{!! Form::text('firstName', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('departmen', 'Department:') !!}
{!! Form::text('department', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group" >
{!! Form::label('building', 'Building:') !!}
{!! Form::select('building', ['vanHall' => 'Vanderbilt Hal',
'wilf' => 'Wilf Hall',
'dag' => 'D Agostino Hall',
'furmanHall' => 'Furman Hall',
'wsn' => 'WSN',
'mercer' => 'Mercer',
'training' => 'Traing Room',
'storage' => 'Storage'
</div>
<div class="form-group">
{!! Form::label('room', 'Room:') !!}
{!! Form::text('room', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('manufacturer', 'Manufacturer:') !!}
{!! Form::text('manufacturer', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="col-md-6">
<div class="form-group">
{!! Form::label('device', 'Device:') !!}
{!! Form::select('device', ['desktop' => 'Desktop',
'laptop' => 'Laptop',
'classroom' => 'Classroom',
'printer' => 'Printer',
'mifi' => 'MiFi',
'panopto' => 'Panopto',
'Other' => 'Other',
], null, ['placeholder' => 'Select Device'])!!}
</div>
<div class="form-group">
{!! Form::label('model', 'Model:') !!}
{!! Form::text('model', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('tag', 'Service Tag:') !!}
{!! Form::text('tag', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('macAddress', 'Mac Address:') !!}
{!! Form::text('macAddress', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('status', 'Status:') !!}
{!! Form::select('status', ['active' => 'Active',
'inactive' => 'Inactive',
], null, ['placeholder' => 'Status'])!!}
</div>
<div class="form-group">
{!! Form::label('comments', 'Comments:') !!}
{!! Form::text('comments', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="col-md-12">
<hr>
<div class="form-group">
{!! Form::submit($submitButtonText, ['class' => 'btn btn-primary form-control']) !!}
{{--<button type="submit" class="btn btn-primary">Submit</button>--}}
</div>
</div>
Instead of using a button I would use an <a> tag.
<a href="{{ url('computers/'.$inventory->id.'/edit') }}>{{ trans('computers.edit') }}</a>
the url() function is a Laravel helper function
Also.. I'm sure there are enough examples of things like this, so make sure you google your question first.
Try this:
<button href="computers/{id}/edit">{{ trans('computers.edit') }}</button>
Or you could use form (Laravel Collective way):
{!! Form::open(['method' => 'Get', 'route' => ['computers.edit', $inventory->id]]) !!}
{!! Form::submit(trans('computers.edit')) !!}
{!! Form::close() !!}
sorry for the late reply
if you are creating the bigger project and if you hate being write the code in every
index.balde.php file for generarting the Edit,Show,Delete Buttons
just use my helper function
For Eg:
Laravel 5.7
Open Your Model it may be Computer or SomeModel
Just Paste the following Code into it
public static function tableActionButtons($fullUrl,$id,$titleValue,$buttonActions = ['show', 'edit', 'delete'],$buttonOptions='')
{
//Value of the post Method
$postMethod = 'POST';
//if the application is laravel then csrf is used
if (function_exists('csrf_token'))
{
$token = csrf_token();
}elseif (!function_exists('csrf_token'))
//else if the mcrypt id is used if the function exits
{
if (function_exists('mcrypt_create_iv'))
{
// if the mcrypt_create_iv id is used if the function exits the set the token
$token = bin2hex(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM));
}
else{
// elseopenssl_random_pseudo_bytes is used if the function exits the set the token
$token = bin2hex(openssl_random_pseudo_bytes(32));
}
}
//action button Value
//(url()->full()) will pass the current browser url to the function[only aplicable in laravel]
$urlWithId =$fullUrl.'/'.$id;
//Charset UsedByFrom
$charset = 'UTF-8';
// Start Delete Button Arguments
//title for delete functions
$deleteFunctionTitle = 'Delete';
//class name for the deletebutton
$deleteButtonClass = 'btn-delete btn btn-xs btn-danger';
//Icon for the delete Button
$deleteButtonIcon = 'fa fa-trash';
//text for the delete button
$deleteButtonText = 'Delete Button';
//dialog Which needs to be displayes while deleting the record
$deleteConfirmationDialog = 'Are You Sure t';
$deleteButtonTooltopPostion = 'top';
// End Delete Button Arguments
// Start Edit Button Arguments
//title for Edit functions
$editFunctionTitle = 'Edit';
$editButtonClass = 'btn-delete btn btn-xs btn-primary';
//Icon for the Edit Button
$editButtonIcon = 'fa fa-pencil';
//text for the Edit button
$editButtonText = 'Edit Button';
$editButtonTooltopPostion = 'top';
// End Edit Button Arguments
// Start Show Button Arguments
//title for Edit functions
$showFunctionTitle = 'Show';
$showButtonClass = 'btn-delete btn btn-xs btn-primary';
//Icon for the Show Button
$showButtonIcon = 'fa fa-eye';
//text for the Show button
$showButtonText = 'Show Button';
$showButtonTooltopPostion = 'top';
// End Show Button Arguments
//Start Arguments for DropDown Buttons
$dropDownButtonName = 'Actions';
//End Arguments for DropDown Buttons
$showButton = '';
$showButton .='
<a href="'.$fullUrl.'/'.$id.'"class="'.$showButtonClass.'"data-toggle="tooltip"data-placement="'.$showButtonTooltopPostion.'"title="'.$showFunctionTitle.'-'.$titleValue.'">
<i class="'.$showButtonIcon.'"></i> '.$showButtonText.'
</a>
';
$editButton ='';
$editButton .='
<a href="'.$urlWithId.'/edit'.'"class="'.$editButtonClass.'"data-toggle="tooltip"data-placement="'.$editButtonTooltopPostion.'" title="'.$editFunctionTitle.'-'.$titleValue.'">
<i class="'.$editButtonIcon.'"></i> '.$editButtonText.'
</a>
';
$deleteButton='';
$deleteButton .='
<form id="form-delete-row' . $id . '" method="'.$postMethod.'" action="'.$urlWithId.'" accept-charset="'.$charset.'"style="display: inline" onSubmit="return confirm("'.$deleteConfirmationDialog.'")">
<input name="_method" type="hidden" value="DELETE">
<input name="_token" type="hidden" value="'.$token.'">
<input name="_id" type="hidden" value="'.$id.'">
<button type="submit"class="'.$deleteButtonClass.'"data-toggle="tooltip"data-placement="'.$deleteButtonTooltopPostion.'" title="'.$deleteFunctionTitle.'-'.$titleValue.'">
<i class="'.$deleteButtonIcon.'"></i>'.$deleteButtonText.'
</button>
</form>
';
$actionButtons = '';
foreach ($buttonActions as $buttonAction)
{
if ($buttonAction == 'show')
{
$actionButtons .= $showButton;
}
if ($buttonAction == 'edit')
{
$actionButtons .= $editButton;
}
if ($buttonAction == 'delete')
{
$actionButtons .= $deleteButton;
}
}
if (empty($buttonOptions))
{
return $actionButtons;
}
elseif (!empty($buttonOptions))
{
if ($buttonOptions == 'group')
{
$buttonGroup = '<div class="btn-group" role="group" aria-label="">
'.$actionButtons.'
</div>';
return $buttonGroup;
}elseif($buttonOptions == 'dropdown')
{
$dropDownButton =
'<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
'.$dropDownButtonName.'
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
'.$actionButtons.'
</div>
</div>
';
return $dropDownButton;
}else
{
return 'only <code>group</code> and <code>dropdown</code> is Available ';
}
}
}
Now all are set then open your index.blade.php
and replace your code
<td>
{{--Need the button to open up my edit form--}}
<button formaction="computers/{id}/edit">{{ trans('computers.edit') }}</button>
<input type="submit" name="update" id="update" value="Update" class="btn btn-primary">
</td>
With This
{!! Computer::tableActionButtons(url()->full(),$inventory->id,$inventory->firstName,['edit',delete,delete],'dropdown'); !!}
If you find any bugs or any issues in buttons please comment in below section to improve my helper script
VERY CAREFUL NOTE THIS IS FOR LARAVEL EXPERTS IF YOU ARE NOW BEGGINER DONT USE IT
just Watch the tutorial at
https://appdividend.com/2018/09/06/laravel-5-7-crud-example-tutorial/
https://laracasts.com/series/laravel-from-scratch-2018
Hope it saved time

Symfony form how to add class to form group

Using:
->add('name', TextType::class, [
'label' => 'Name',
'required' => true,
'attr' => [
'class' => 'myclass'
],
])
I'm adding the class "myclass" to the <input> element, getting:
<div class="form-group">
<label ...>Name</label>
<input type="text" required="required" class="myclass" ...>
</div>
What if I wanted to add the "myclass" class not to the input itself, but to its <div class="form-group"> container, in order to get:
<div class="form-group myclass">
<label ...>Name</label>
<input type="text" required="required" ...>
</div>
What's the best way to accomplish that?
I actually found a way of doing it in PHP (Symfony 4.4.2). Class has to go into 'row_attr' instead of 'attr'. For the given example it'd be:
->add('name', TextType::class, [
'label' => 'Name',
'required' => true,
'row_attr' => [
'class' => 'myclass'
],
])
related docs: https://symfony.com/doc/current/reference/forms/types/text.html#row-attr
In my case I needed to override the form_row block:
{% block form_row -%}
<div class="form-group myclass{% if (not compound or force_error|default(false)) and not valid %} has-error{% endif %}">
{{- form_label(form) -}}
{{- form_widget(form) -}}
{{- form_errors(form) -}}
</div>
{%- endblock form_row %}
as suggested, the docs helps to understand why.
As #Yoshi pointed out, in the form definition itself you cannot add classes to the container div.
The right way to customize form rendering is to override the base block via Twig as explained here: http://symfony.com/doc/current/cookbook/form/form_customization.html#how-to-customize-an-individual-field (BTW I suggest you to read carefully the whole article to know everything you need to know about customizations).
In the overridden block you have also the widget data if you want e.g. to add a class with the field id:
{% block _product_name_widget %}
<div class="text_widget {{ id }}">
{{ block('form_widget_simple') }}
</div>
{% endblock %}
in this example above, {{ id }} will be replaced with the widget (field) id.