Laravel 6 - How to create a table with dynamic rows using LaravelCollective? - html

I want to make a dynamic table using LaravelCollective, where you can add/remove rows.
I already tried to follow a few tutorials, but I keep failing. I don't know what I'm doing wrong.
Html Code for the table/form
{{ Form::open(['action' => 'TransactionsINController#store', 'method' => 'POST']) }}
<section>
<div class="panel panel-header">
<div class="form-group">
{{ Form::label('supplier_name', 'Supplier Name') }}
{{ Form::select('supplier_name', $supplierList->pluck('name', 'id'),
null, ['class' => 'form-control', 'placeholder' => 'Pick one supplier...']) }}
</div>
<div class="form-group">
{{ Form::label('transaction_in_date', 'Transcation Date') }} <br>
{{ Form::date('transaction_in_date', \Carbon\Carbon::now()->format('d M Y')) }}
</div>
</div>
<div class="panel panel-footer">
<table class="table table-hover table-bordered" id="">
<thead align="center">
<tr>
<th>Device Type</th>
<th>Spec</th>
<th>Price</th>
<th><a href="#" class="btn btn-success addRow">
<i class="fa fa-plus"></i>
</a>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div class="form-group">
{{ Form::select('device_type_name[]', $deviceTypeList->pluck('name_tipe_device', 'id'),
null, ['class' => 'form-control', 'placeholder' => 'Pick Device Type...', 'name' => 'device_type_name[]']) }}
</div>
</td>
<td>
<div class="form-group">
{{ Form::textarea('device_spec[]', '',
['class' => 'form-control', 'placeholder' => 'Device Spec', 'rows' => 5, 'cols' => 45, 'name' => 'device_spec[]']) }}
</div>
</td>
<td>
<div class="form-group">
{{ Form::number('device_price[]', 'value', ['name' => 'device_price[]']) }}
</div>
</td>
<td>
<a href="#" class="btn btn-danger remove">
<i class="fa fa-times"></i>
</a>
</td>
</tr>
</tbody>
</table>
</div>
</section>
{{ Form::button('<i class="far fa-save"></i> Submit', ['type' => 'submit', 'class' => 'btn btn-info'] ) }}
{{ Form::close() }}
The Script i use to add/remove Rows
<script type="text/javascript">
$('.addRow').on('click', function(){ \\Uncaught ReferenceError: happen in this line.
addRow();
});
function addRow(){
var tr = '<tr>'+
'<td>'+
'<div class="form-group">'+
'{{ Form::select('device_type_name[]', $deviceTypeList->pluck('nama_tipe_device', 'id'),
null, ['class' => 'form-control', 'placeholder' => 'Pick Device Type...', 'name' => 'device_type_name[]']) }}'+
'</div>'+
'</td>'+
'<td>'+
'<div class="form-group">'+
'{{ Form::textarea('device_spec[]', '',
['class' => 'form-control', 'placeholder' => 'Device Spec', 'rows' => 5, 'cols' => 45, 'name' => 'device_spec[]']) }}'+
'</div>'+
'</td>'+
'<td>'+
'<div class="form-group">'+
'{{ Form::number('device_price[]', 'value', ['name' => 'device_price[]']) }}'+
'</div>'+
'</td>'+
'<td>'+
'<a href="#" class="btn btn-danger remove">'+
'<i class="fa fa-times"></i>'+
'</a>'+
'</td>'+
'</tr>';
$('tbody').append(tr);
}
</script>
When I press the + button to add more rows it doesn't do anything and when I inspected the code in the browser, I found this error:
Uncaught ReferenceError: $ is not defined

$ refers to jQuery, have you added it to your project and is it available in this scope?
Note: it needs to be present before your script
jQuery may not have loaded when that script runs, you can try wrapping your script with $(document).ready({ }) so it runs it when jQuery has loaded

Related

Submit multiple forms created by foreach loop with one button

In my blade template, I use a foreach to create one form per iteration. Currently, each form has a submit button, but I want to use one submit button to submit all the form, because now I cannot submit all forms at the same time. How can I fix this?
#if(count($task_criterias) > 0)
#foreach($task_criterias as $task_criteria)
<td class="card">
<div>Description:{{$task_criteria->criteriadescription}}</div>
<div>Maximum Mark:{{$task_criteria->maximummark}}</div>
{!! Form::open([
'action' => 'CriteriaMarksController#store',
'method' => 'POST',
'name' => "form"
]) !!}
<div class="form-group" hidden >
{{ Form::label('criteria_id', 'criteria_id') }}
{{ Form::text('criteria_id', $task_criteria->id, ['class'=>'form-control']) }}
</div>
<div>
{{ Form::label('selfmark','Mark ') }}
{{ Form::number('selfmark', '',['placeholder'=>'', 'class' => 'col-lg-3 control-label']) }}
</div>
{!! Form::close() !!}
</td>
#endforeach
<input type="button" class="btn btn-info " value="Submit" onclick="submitForms()" />
<script>
submitForms = function() {
$("form").each(function(){
$.ajax({
method:'POST',
url:'/criteria_marks/post',
data: $(this).serialize(),
success: function(r){
//...
}
});
});
}
</script>
#endif
There can be only one post submit per request. One of the easiest solution is to perfom multiple requests using ajax:
<script>
submitForms = function() {
$("form").each(function(){
$.ajax({
method:'POST',
url:'* route to CriteriaMarksController#store *',
data: $(this).serialize(),
success: function(r){
//...
}
});
});
}
</script>
Or you can make one big form:
{!! Form::open([
'action' => 'CriteriaMarksController#store',
'method' => 'POST',
'name' => "form"
]) !!}
#foreach($task_criterias as $key => $task_criteria)
<td class="card">
<div>Description:{{$task_criteria->criteriadescription}}</div>
<div>Maximum Mark:{{$task_criteria->maximummark}}</div>
<div>
{{ Form::label('selfmark','Mark ') }}
{{ Form::number('selfmark['.$task_criteria->id.']', '',
['placeholder'=>'', 'class' => 'col-lg-3 control-label']) }}
</div>
</td>
#endforeach
<input type="submit" class="btn btn-info " value="Submit" />
{!! Form::close() !!}
This will form associative array selfmark in post with id => value pairs
Not sure how to do it with the Form facade.
What you have now is:
<input name="criteria_id" value="{{ $task_criteria->id }}" />
<input name="selfmark" value="{{ $task_criteria->id }}" />
But what you need to have is:
<input name="criteria_id[]" value="{{ $task_criteria->id }}" />
<input name="selfmark[]" />
Note the [] in the input names.

Keep the option selected in search form - Laravel

I have a search form that searches for responsible, date and device name. When someone searches for a resposible with a date, I want that search data to be maintained in the result
The entire search form
{{ Form::open(['route' => 'reviews.index', 'method' => 'GET', 'class' => 'form-inline pull-right']) }}
#csrf
{{ Form::text('device_name', null, ['class' => 'form-control', 'placeholder' => 'Nombre Equipo'])}}
<select name="name">
<option></option>
<option>OK</option>
<option>NOK</option>
</select>
{{ Form::date('created_at', null, ['class' => 'form-control', 'placeholder' => 'Creacion'])}}
<select name="responsable">
<option ></option>
#foreach($users as $user)
<option>{!! $user->name !!}</option>
#endforeach
</select>
<button style="border: none;padding: 4px 20px;background-color: #d61628;color: white">Buscar</button>
<button style="border: none;padding: 4px 20px;background-color: #d61628;color: white">Reset</button>
{{ Form::close() }}
And the index method in Controller
public function index(Request $request)
{
$responsable = $request->get('responsable');
$created_at = $request->get('created_at');
$device_name = $request->get('device_name');
$name = $request->get('name');
$devices = Device::all();
$reviews = Review::orderBy('id', 'DESC')
->responsable($responsable)
->created_at($created_at)
->device_name($device_name)
->name($name)
->paginate(30);
$users = User::all();
return view('reviews.index', compact('devices', 'reviews', 'users'));
}
Thanks
You may use old() function for re-populate the form with the last submitted data.
See https://laravel.com/docs/5.6/requests#old-input
So, in your blade code it would be something like this:
{{ Form::text('device_name', old('device_name'), ['class' => 'form-control', 'placeholder' => 'Nombre Equipo'])}}
//...
{{ Form::date('created_at', old('created_at'), ['class' => 'form-control', 'placeholder' => 'Creacion'])}}
For select, it's different, basically you compare each rendered option in the loop and if it's the one submitted you mark it as selected with html.
<select name="responsable">
<option></option>
#foreach($users as $user)
<option value="{{ $user->name }}" {{ old('responsable') == $user->name ? 'selected' : '' }}>{!! $user->name !!}</option>
#endforeach
</select>
Also, consider that old() also accepts a second parameter for default value in case you don't want it to be just null.

format form elements in cakephp3

How do I align where the form element box appears as I want at a minimum each input box to right align, have the same font etc. I tried using templating but in the end I had no idea what it was talking about.
https://book.cakephp.org/3.0/en/views/helpers/form.html#customizing-the-templates-formhelper-uses
echo $this->Form->input('first_name',['label' => 'Tutor FirstName']);
echo $this->Form->input('last_name',['label' => 'Tutor LastName']);
echo $this->Form->input('email', ['label' => 'Email']);
echo $this->Form->input('mobile', ['label' => 'Mobile']);
echo $this->Form->input('home_phone',['label' => 'Home Phone']);
echo $this->Form->input('work_phone',['label' => 'Work Phone']);
For templating it's work like that.
In src/Controller/AppController or where you're need it
class AppController extends Controller
{
public $helpers = [
'Form' => [
'templates' => 'template_forms',
],
];
In src/config
Create a new file "template_forms.php"
<?php
$config = [
'checkboxFormGroup' => '<div class="col-xs-5"><div class="checkbox">{{label}}</div></div>',
'checkbox' => '<input type="checkbox" value="{{value}}" {{attrs}}>',
'checkboxWrapper' => '<div class="form-group"><div class="col-sm-offset-5 col-xs-7">{{label}} {{input}}</div></div>',
'inputContainer' => '<div class="form-group" {{required}} >{{content}}</div><div class="hr-line-dashed"></div>',
'input' => '<div class="col-xs-7 col-sm-10 col-lg-10"><input class="form-control input-sm" type="{{type}}" name="{{name}}" {{attrs}}></div>',
'label' => '<label {{attrs}} class="col-xs-5 col-sm-2 col-lg-2 control-label">{{text}}</label>',
'select' => '<div class="col-xs-7 col-sm-10 col-lg-10"><select class="form-control input-sm" {{attrs}} name={{name}}>{{content}}<select></div>',
'error' => '<p class="text-danger">{{content}}</p>',
'textarea' => '<div class="col-xs-7 col-sm-10 col-lg-10"><textarea class="form-control input-sm" name="{{name}}" {{attrs}}>{{value}}</textarea></div>',
'button' => '<div class="form-group"><div class="col-md-12 col-xs-12 col-sm-12 text-center"><button {{attrs}} type="submit">{{text}}</button></div></div>',
'inputContainerError' => '<div class="form-group has-error" {{required}}>{{content}}</div>{{error}}',
];
?>
This will overide all your forms in your app.

Add icon to Laravelcollective submit button

I'm trying to add a trash icon to submit button and I tried this :
{!! Form::submit('', ['class' => 'btn btn-warning btn-sm fa fa-trash']) !!}
but the icon won't show. How to solve this ? thanks in advance!
Try to use Form::button instead of Form::submit:
{{ Form::button('<i class="fa fa-trash"></i>', ['type' => 'submit', 'class' => 'btn btn-warning btn-sm'] ) }}
You can try too with:
Example to input type button:
{{ FORM::button('<i class="glyphicon glyphicon-trash" aria-hidden="true"></i> Text button',['class'=>'button button-red','type'=>'button','id'=>'id-button']) }}
there are not possible try it
laravel collective form given very competition
<button class='btn btn-primary' type='submit' value='submit'>
<i class='fa fa-save'> </i> Save

Yii2:Does login,signup & password reset forms work together in single view

I am using yii2 for webapp. I got stuck at a very unique point & need help
I am using javascript fadeIn & Fadeout effect to show & hide the form. So to enable that effect i have to put all the three forms in a single view & have to call respective controllers(actionlogin, actionSignup & actionPasswordReset) on button click.
I declared rules for the parameters of all the three forms as 'required' for validation but when i submit the form, that time it is not working further as it wants me to fill the other two form fields as well to submit as the other two forms are also in the same view.
I also put my javascript code that call animation it is here:
<script>
jQuery(document).ready(function() {
App.setPage("login"); //Set current page
App.init(); //Initialise plugins and elements
});
</script>
<script type="text/javascript">
function swapScreen(id) {
jQuery('.visible').removeClass('visible animated fadeInUp');
jQuery('#' + id).addClass('visible animated fadeInUp');
}
</script>
anyone knows how to do this
Code is:
<section id="login" class="visible">
<?php $form = ActiveForm::begin(['id' => 'login-form', 'options' => array('role' => 'form')]); ?>
<div class="form-group">
<?= $form->field($model, 'email_address',['template' => "{label}\n<i class='fa fa-envelope'></i>\n{input}\n{hint}\n{error}"]); ?>
</div>
<div class="form-group">
<?= $form->field($model, 'password', ['template' => "{label}\n<i class='fa fa-lock'></i>\n{input}\n{hint}\n{error}"])->passwordInput(); ?>
</div>
<div class="form-actions">
<?= $form->field($model, 'rememberMe',['template' => "{input} {label}"])->checkbox(); ?>
<?= Html::submitButton('Submit', ['class' => 'btn btn-danger', 'name' => 'login-button']) ?>
</div>
<?php $form = ActiveForm::end(); ?>
My signup page is
<section id="register" class="visible">
<?php $form = ActiveForm::begin(['id' => 'signup-form', 'options' => array('role' => 'form')]); ?>
<div class="form-group">
<?= $form->field($model, 'full_name',['template' => "{label}\n<i class='fa fa-font'></i>\n{input}\n{hint}\n{error}"]); ?>
</div>
<div class="form-group">
<?= $form->field($model, 'email',['template' => "{label}\n<i class='fa fa-envelope'></i>\n{input}\n{hint}\n{error}"]); ?>
</div>
<div class="form-group">
<?= $form->field($model, 'input_password',['template' => "{label}\n<i class='fa fa-lock'></i>\n{input}\n{hint}\n{error}"])->passwordInput() ?>
</div>
<div class="form-group">
<?= $form->field($model, 'input_confirm_password',['template' => "{label}\n<i class='fa fa-check-square-o'></i>\n{input}\n{hint}\n{error}"])->passwordInput() ?>
</div>
<div class="form-actions">
<?= $form->field($model, 'agreeTC',['template' => "{input}"])->checkbox(['class' => 'uniform']); ?>
<?= Html::submitButton('Signup', ['class' => 'btn btn-success', 'name' => 'signup-button']) ?>
</div>
& my request reset code is:
<section id="forgot" class="visible">
<?php $form = ActiveForm::begin(['id' => 'request-password-reset-form']); ?>
<div class="form-group">
<?= $form->field($model, 'reset_email',['template' => "{label}\n<i class='fa fa-envelope'></i>\n{input}\n{hint}\n{error}"]); ?>
</div>
<div class="form-actions">
<?= Html::submitButton('Send Me Reset Instructions', ['class' => 'btn btn-info', 'name' => 'reset-button']) ?>
</div>
<?php $form = ActiveForm::end(); ?>
Hola you could try this Make The Controller actionYourview() , instance 3 models and send to the view.
EXAMPLE controller
//your login,signup and passReset model dir
use frontend\models\login;
use frontend\models\signup;
use frontend\models\passReset;
//example if the Controller is Site (defaulOne)
class SiteController extends Controller
{
//your action
public function actionYourview(){
//1 Make your 3 models for separate
$loginModel = new login();
$signupModel = new signup();
$passResetModel = new passReset();
//this will solve your problem of the three forms as 'required'
if($loginModel->load(Yii::$app->request->post()) && $loginModel){ //if Login form is summit and validate
//do your login stuff
}else if($signupModel->load(Yii::$app->request->post())){ //if $signupModel form is summit
//do your signup stuff
}else if($passResetModel->load(Yii::$app->request->post())){ //if $passreset form is summit
// do yout pass reset stuff
}else{ // and else just render and make sure that you pass your 3 models
return $this->render('Yourview',[
'loginModelToView' =>$loginModel,
'signupModelToView' =>$signupModel,
'passResetModelToView' =>$passResetModel
]);
}
}
}
and the view just change the $model to they respective model
login
<section id="login" class="visible">
<?php $form = ActiveForm::begin(['id' => 'login-form', 'options' => array('role' => 'form')]); ?>
<div class="form-group">
<?= $form->field($loginModelToView, 'email_address',['template' => "{label}\n<i class='fa fa-envelope'></i>\n{input}\n{hint}\n{error}"]); ?>
</div>
<div class="form-group">
<?= $form->field($loginModelToView, 'password', ['template' => "{label}\n<i class='fa fa-lock'></i>\n{input}\n{hint}\n{error}"])->passwordInput(); ?>
</div>
<div class="form-actions">
<?= $form->field($loginModelToView, 'rememberMe',['template' => "{input} {label}"])->checkbox(); ?>
<?= Html::submitButton('Submit', ['class' => 'btn btn-danger', 'name' => 'login-button']) ?>
</div>
<?php $form = ActiveForm::end(); ?>
signup
<section id="register" class="visible">
<?php $form = ActiveForm::begin(['id' => 'signup-form', 'options' => array('role' => 'form')]); ?>
<div class="form-group">
<?= $form->field($signupModelToView, 'full_name',['template' => "{label}\n<i class='fa fa-font'></i>\n{input}\n{hint}\n{error}"]); ?>
</div>
<div class="form-group">
<?= $form->field($signupModelToView, 'email',['template' => "{label}\n<i class='fa fa-envelope'></i>\n{input}\n{hint}\n{error}"]); ?>
</div>
<div class="form-group">
<?= $form->field($signupModelToView, 'input_password',['template' => "{label}\n<i class='fa fa-lock'></i>\n{input}\n{hint}\n{error}"])->passwordInput() ?>
</div>
<div class="form-group">
<?= $form->field($signupModelToView, 'input_confirm_password',['template' => "{label}\n<i class='fa fa-check-square-o'></i>\n{input}\n{hint}\n{error}"])->passwordInput() ?>
</div>
<div class="form-actions">
<?= $form->field($signupModelToView, 'agreeTC',['template' => "{input}"])->checkbox(['class' => 'uniform']); ?>
<?= Html::submitButton('Signup', ['class' => 'btn btn-success', 'name' => 'signup-button']) ?>
</div>
finaly the reset
<section id="forgot" class="visible">
<?php $form = ActiveForm::begin(['id' => 'request-password-reset-form']); ?>
<div class="form-group">
<?= $form->field($passResetModelToView, 'reset_email',['template' => "{label}\n<i class='fa fa-envelope'></i>\n{input}\n{hint}\n{error}"]); ?>
</div>
<div class="form-actions">
<?= Html::submitButton('Send Me Reset Instructions', ['class' => 'btn btn-info', 'name' => 'reset-button']) ?>
</div>
<?php $form = ActiveForm::end(); ?>
hope that this is what your looking. good luck