I have a condition, if active is checked then it'll display a text form.
This is my view form code:
<div class="form-group">
<label>Active:</label>
{{ Form::checkbox('active', 0,null, array('id' =>'active_check','class' => 'active_check')) }}
</div>
<div id ="join_date" class="form-group" style="display: none;">
<label>Join Date:</label>
<div class="input-group">
{!! Form::text('join_date', null, array('class' => 'form-control datetimepicker' )) !!}
</div>
</div>
Here is my jQuery toggle and check condition code so far:
$('#active_check').click(function() {
$("#join_date").toggle(this.checked);
$('#join_date').find('input[type="text"], textarea').val('');
});
var activeCheck = $('checkbox[name="active_check"]');
function checkActive(select) {
if (select.val() == 0) {
$('#join_date').hide();
} else {
$('#join_date').show();
}
}
checkActive(activeCheck);
The issue is when it is first loaded the join_date form doesn't hide. I need to toggle the checkbox to hide it. Any idea?
Try this simple logic:
if($('#active_check').is(':not(":checked")')) {//test if active_check is not checked
$('#join_date').hide();//hide it
}
$('#active_check').click(function() {
$("#join_date").toggle();//toggle it
});
I'm Having a bit of a issue with Depdrop from Kartik-v and Dynamic Form from Wbraganca. The problem is when I load the update form, the data isn't loaded into the second dynamic field. Until i add a new row then the data is shown.
.
It doesn't give any error. All I want is for the data to be shown on the second row and so on.
Code in the form
<?php $catList=ArrayHelper::map(Hs::find()->all(), 'hscode', 'hscode' );?>
<?= $form->field($modelsItems, "[{$i}]hscode")->dropDownList($catList, ['id'=>'cat-id','prompt'=>'Select...']);?>
</div>
<div class="col-sm-6" style="width: 200px">
<?= $form->field($modelsItems, "[{$i}]hsproduct")->widget(Depdrop::classname(), [
'options'=>['id'=>'product-id'],
'pluginOptions'=>[
'placeholder' => FALSE,
'initialize' => true,
'depends'=>['cat-id'], // the id for cat attribute
'url'=> Url::to(['hs/subcat']),
]
]);
?>
</div>
<div class="col-sm-6" style="width: 170px">
<?php $companylist=ArrayHelper::map(Company::find()->all(), 'company', 'company' );?>
<?= $form->field($modelsItems, "[{$i}]company")->dropDownList($companylist, ['id'=>'company-id','prompt'=>'Select...']);?>
</div>
<div class="col-sm-6" style="width: 170px">
<?= $form->field($modelsItems, "[{$i}]variety")->widget(DepDrop::classname(), [
'options'=>['id'=>'cp-varitey'],
'pluginOptions'=>[
'placeholder' => FALSE,
'initialize' => true,
'depends'=>['company-id'], // the id for cat attribute
'url'=> Url::to(['company/subcat2'])
]
])->label('Variety');
?>
</div>
For those who having this same problem i solve it by changing the id. The first ID was cat-id but when you create a new dynamic field the id then change to cat-0--id for some reason. The way i solve it was by changing the id to this
'id'=>"cat-".$i."--id" and should do this for the rest of the id. My product id now is 'id'=>"product-".$i."--id".
I hope this helps some one. Thanks
I using form type in symfony 2.8.x.
I need form attribute text in view.
form type:
$builder->add( 'category_id', ChoiceType::class,
array( 'label'=>'Category',
'constraints'=>array( new Regex(array("pattern"=>"'([^0-9]*)$'si",
"message"=>"Required field!"))
),
'choices'=>$categoriesRepo->getAllActive(),
'choices_as_values'=>true,
'choice_label' => 'getName',
'choice_value' => 'getId',
'attr'=>array( 'class'=>'form-control',
'help'=>'Help message.',
)));
In view:
<?php echo $view->render("XXXBundle:XXX:form_element.html.php", array('form'=>$templateForm['category_id']))?>
form_element.html.php
<div class="form-group">
<?php echo $view['form']->label($form, null, array('required'=>false, 'label_attr'=>array('class'=>'col-md-3 control-label'))) ?>
<div class="col-md-4">
<?php echo $view['form']->widget($form, array('attr'=>array('help'=>false))) ?>
<span class="help-block error"><?php echo $view['form']->errors($form) ?></span>
<span class="help-block"> HELP_MESSAGE </span>
</div>
</div>
I need "help" attribute to HELP_MESSAGE in view.
There is a solution to this problem?
Thank you!
You are the first Symfony user I've ever seen who renders PHP files instead of twig. Congratulations.
The real business part of your code you haven't posted, so it's hard to give a definitive answer, but somewhere at the bottom of your controller's action function there will be a call to $this->render('XXXBundle:XXX:template.html.php');
The 'Symfony way' is to pass the help message in that:
$help_message = 'My Help Message';
$this->render('XXXBundle:XXX:template.html.php', array('help_message' => $help_message));
In your template file:
<?php echo($view['help_message']);?>
before make any judgment I read all the related questions related to my problem but none of them fixed it.
so here's my problem when I use the authentication facility of laravel 5.1 and want to register a user the csrf token generate twice one when I requesting to show my register form and one when I post the form data to auth/register post route and this cause my to receive a csrf token mismatch exception. here's my register form markup
<form method="POST" action="/auth/register" class="ui large form">
{!! csrf_field() !!}
<div class="two fields dirright alignright">
<div class="field" >
<div class="ui right icon input">
<i class="user icon"></i>
{!! Form::text(
'first_name',
Input::old('first_name'),
array(
'class' => 'dirright alignright fontfamily',
'placeholder' => 'نام'
)
) !!}
</div>
</div>
<div class="field" >
<div class="ui right icon input">
<i class="user icon"></i>
{!! Form::text(
'last_name',
Input::old('last_name'),
array(
'class' => 'dirright alignright fontfamily',
'placeholder' => 'نام خانوادگی'
)
) !!}
</div>
</div>
</div>
<div class="field">
<div class="ui left icon input latintext">
<i class="mail icon"></i>
{!! Form::email(
'email',
Input::old('email'),
array(
'class' => 'latintext',
'placeholder' => 'E-mail address'
)
) !!}
</div>
</div>
<div class="field">
<div class="ui left icon input latintext">
<i class="lock icon"></i>
{!! Form::password(
'password',
Input::old('password'),
array(
'class' => 'latintext',
'placeholder' => 'Password'
)
) !!}
</div>
</div>
<div class="ui fluid large primary submit button">ثبت نام</div>
<div class="ui error message alignright"></div>
</form>
Just add the csrf token as follows in the form :
<input type="hidden" name="_token" value="{{csrf_token()}}"/>
it worked for me.
Assume that your web server has already write access to session directory, in my case 'app/storage/framework/sessions/'.
Execute:
$ rm -f {your_web_app}/storage/framework/sessions/*
There are several possibilities...
1) If you have any spaces at all in front of your opening <?php tag, it can cause this error (especially if you're using AJAX). So just double-check to make sure that there's nothing before <?php in your files.
2) If you're trying to submit this form data via AJAX, the docs suggest passing the CSRF token like so:
Add this meta tag to your <head>:
<meta name="csrf-token" content="{{ csrf_token() }}">
And then do this in the AJAX call:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
If your using laravel 5.1 simply adding {{ csrf_field() }} would do the trick
The csrf token will be added automatically if you use the open and close tags for Form
{!! Form::open(['action' => '/auth/register', 'class' => 'ui large form']) !!}
-- Form stuff here --
{!! Form::close() !!}
i hope this will help
set meta-tag like follows
<meta name="csrf-token" content="{{ csrf_token() }}">
then request like follows
$.ajax({
data: {data1:'data1',data2:'data2'},
url: '/your/url/goes/here',
type: 'POST',
beforeSend: function (request) {
return request.setRequestHeader('X-CSRF-Token', $("meta[name='csrf-token']").attr('content'));
},
success: function(response){
console.log(response);
}
})
i'm a newbie to laravel framework and i just find it's so hard to understanding laravel form usability.... so here is some issue that i face into
First my dropdown should be having my categories name and at the very end of dropdown it will be my specific dropdown add new categories that when i selected will trigger my javascript... and im here is my form code
{{ Form::select('kategori',KategoriArtikel::lists('name','id'),Input::old('kategori'),array('class' => 'form-control', 'onChange' => 'changeFunc(value)')) }}
that will show right value from my database also with right selected value but it's missing my specific dropdown, so i tried to add like this
<?php
$tambah = array('tambah' => 'Tambah Kategori Baru');
$list = array_merge(KategoriArtikel::lists('name','id'),$tambah);
?>
{{ Form::select('kategori',array(KategoriArtikel::lists('name','id'),$tambah),Input::old('kategori'),array('class' => 'form-control', 'onChange' => 'changeFunc(value)')) }}
but it will generate 2 with different layout (which ugly) and not showing right selected value, so i tried other way
<?php
$tambah = array('tambah' => 'Tambah Kategori Baru');
$list = array_merge(KategoriArtikel::lists('name','id'),$tambah);
?>
{{ Form::select('kategori',$list,Input::old('kategori'),array('class' => 'form-control', 'onChange' => 'changeFunc(value)')) }}
and it not generate and my dropdown layout is what it supposed to be but now the is not from my table id anymore but just sequential number 0,1,2,3,etc
any solution to that?
Second problem is when i submit my form, it's not working, my field in table is int and when i submit it just not saving/updating so i tried to dd(Input::all()) and my dropdown is returning string of selected option value and it's not saved into my table... so why is that? it also not working for my boolean field that contain tinyint(1)... so basicly all value returning string.
its returning this
array(8) { ["_method"]=> string(3) "PUT" ["_token"]=> string(40) "IspUKdCETMe4Nn3pDI43GI7aJKQfXpupJvQAy1k6" ["simpan"]=> string(6) "simpan" ["judul"]=> string(18) "test "kegiatan" 21" ["kategori"]=> string(1) "9" ["kategori_baru"]=> string(0) "" ["status"]=> string(1) "0" ["content"]=> string(1012) "
I used--and I don't put my arm round your waist,' the Duchess said to herself. 'Of the mushroom,' said the Dormouse, and repeated her question. 'Why did they live at the sides of it, and then turned to the tarts on the twelfth?' Alice went on, 'that they'd let Dinah stop in the shade: however, the moment he was obliged to have wondered at this, but at the Caterpillar's making such a capital one for catching mice--oh, I beg your pardon!' cried Alice hastily, afraid that she had read about them in books, and she at once without waiting for turns, quarrelling all the children she knew, who might do something better with the time,' she said to herself; 'I should like to drop the jar for fear of killing somebody, so managed to swallow a morsel of the house, quite forgetting her promise. 'Treacle,' said the Gryphon: and it sat for a few minutes to see if she had grown up,' she said this, she looked down at her as she wandered about for it, you know--' (pointing with his head!' she said,.
" }
and as you can see ['kategori'] is supposed to be making my field kategori in my table to be 9 but it's not changing anything in my database
well i laravel simplicity but it's very confusing when working with database (can't find any documentation example related to databas retrieving data and so on)
edit:
this is my controller
public function update($id)
{
$artikel = Artikel::findOrFail($id);
if(Input::get('simpan')){
//dd(Input::all());
$validator = Validator::make($data = Input::all(), Artikel::$rules);
if ($validator->fails())
{
return Redirect::back()->withErrors($validator)->withInput();
}
$judul = Input::get('judul');
$artikel->update($data);
return Redirect::route('admin.artikels.index')->with('message', 'Artikel ' .$judul. ' Telah berhasil di ubah.');
}elseif(Input::get('batal')){
return $this->index();
}
}
and here is my view
#extends('admin._layouts.admin')
#section('content')
{{ Form::model($artikel, array('route' => array('admin.artikels.update',$artikel->id), 'method' => 'put')) }}
<div class="panel panel-default">
<!--button-->
<div class="panel-heading tooltip-demo">
{{ Form::submit('Simpan',array('class' => 'btn btn-primary', 'data-toggle' => 'tooltip',
'data-placement' => 'top','title' => 'Menyimpan artikel' )) }}
{{ Form::submit('Batal',array('class' => 'btn btn-default', 'data-toggle' => 'tooltip',
'data-placement' => 'top','title' => 'Batal menambah artikel dan kembali ke halaman kelola artikel' )) }}
</div>
<!--/button-->
<div class="panel-body">
<!--judul-->
<div class="col-lg-10">
<div class="form-group">
{{ Form::label('Judul Artikel') }}
{{ Form::text('judul',null,array('class' => 'form-control', 'placeholder' => 'Silahkan masukkan judul artikel'))}}
{{ $errors->first('judul', '<p class="error">:message</p>') }}
</div>
</div>
<!--/judul-->
<!--kategori-->
<div class="col-lg-4">
<div class="form-group">
{{ Form::label('Kategori') }}
<?php
$tambah = array('tambah' => 'Tambah Kategori Baru');
$list = array_merge(KategoriArtikel::lists('name','id'),$tambah);
?>
{{ Form::select('kategori',$list,'Pilih Kategori Artikel',array('class' => 'form-control', 'onChange' => 'changeFunc(value)')) }}
</div>
</div>
<!--/kategori-->
<!--kategori baru-->
<div class="col-lg-4" id="pilihan" style="display:none;">
<div class="form-group">
{{ Form::label('Kategori Baru') }}
{{ Form::text('kategori_baru',null,array('class' => 'form-control', 'placeholder' => 'Silahkan masukkan kategori baru',
'maxlength' => '30'))}}
</div>
</div>
<!--/kategori baru-->
<!--status-->
<div class="col-lg-4">
<div class="form-group">
{{ Form::label('Status') }}
{{ Form::select('status',array('0' => 'Tidak diterbikan', '1' => 'Terbitkan'),null, array('class' => 'form-control')) }}
</div>
</div>
<!--/status-->
<!--artikel pilihan-->
<div class="col-lg-5">
<div class="form-group">
{{ Form::label('Artikel Pilihan') }}
<div class="input-group">
<span class="input-group-addon">
{{ Form::checkbox('pilihan','1',true,array('id' => 'artikelpilihan')) }}
</span>
{{ Form::text('null','Tidak',array('class' => 'form-control', 'id' => 'artikeltext' ,'disabled' => 'true'))}}
</div>
</div>
</div>
<!--/artikel pilihan-->
<!--content-->
<div class="col-lg-12">
{{ Form::label('Isi Artikel') }}
{{ Form::textarea('content',null,array('style' => 'height:300px')) }}
{{ $errors->first('content', '<p class="error">:message</p>') }}
</div>
<!--/content-->
</div>
</div>
{{ Form::close() }}
{{ HTML::script('js/tinymce/tinymce.min.js') }}
<script type="text/javascript">
tinymce.init({
selector: "textarea",
theme: "modern",
skin: 'light',
plugins: [
"advlist autolink lists link image charmap print preview hr anchor pagebreak",
"searchreplace wordcount visualblocks visualchars code fullscreen",
"insertdatetime media nonbreaking save table contextmenu directionality",
"emoticons template paste textcolor colorpicker textpattern"
],
toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
toolbar2: "print preview media | forecolor backcolor emoticons | fontselect fontsizeselect",
image_advtab: true,
templates: [
{title: 'Test template 1', content: 'Test 1'},
{title: 'Test template 2', content: 'Test 2'}
],
file_browser_callback: RoxyFileBrowser
});
function RoxyFileBrowser(field_name, url, type, win) {
var roxyFileman = '../../../../public/js/tinymce/plugins/fileman/index.html?integration=tinymce4';
if (roxyFileman.indexOf("?") < 0) {
roxyFileman += "?type=" + type;
}
else {
roxyFileman += "&type=" + type;
}
roxyFileman += '&input=' + field_name + '&value=' + document.getElementById(field_name).value;
tinyMCE.activeEditor.windowManager.open({
file: roxyFileman,
title: 'File Manager',
width: 800,
height: 480,
resizable: "yes",
plugins: "media",
inline: "yes",
close_previous: "no"
}, { window: win, input: field_name });
return false;
}
</script>
#stop
If I have understood your problem correctly, you want to populate a dropdown based on values from database and also want to display "Select A Category" in the category dropdown. And currently your dropdown values are not being saved.
To populate the dropdown put the following code your controller's method through which you are loading the view file.
$data['kategori'] = KategoriArtikel::lists('name','id');
$data['kategori'][''] = 'Select a kategori';
return View::make('YOUR_VIEW_FILE', $data);
In your view file, simple use the following, (same rules apply for other select dropdowns):
{{ Form::select('kategori', $kategori , Input::old('kategori'), array('id' => 'category', 'class' => 'form-control', )) }}
The above code basically generates something like this:
<select id="category" class="form-control">
<option value="">Select a Kategori</option>
<option value="1">First Kategori</option>
<option value="2">Second Kategori</option>
</select>
The third parameter in Form::select() expects a default option value, if you provide a value it will compare with option values and display the matched one as selected.
And Your form's method shouldn't be 'method' => 'POST' not 'method' => 'put'
I am using the following code in cases such this you described. In my controller i have
public function create()
{
$category1= Category1::lists('category1','id');
$category2= Category2::lists('category2','id');
//you can have many select lists in an form
return
View::make('someobject.create',compact('category1','category2'));
}
and in my someobject.create view
{{Form::open(array('route'=>'someobject.store'))}}
<div>
{{Form::label('category1', 'category1')}}
{{Form::select('category1',array('' => 'Please select') + $category1) }}
<span class="errors">{{$errors->first('category1')}}</span>
{{Form::label('category2', 'category2')}}
{{Form::select('category2',array('' => 'Please select') + $category2) }}
<span class="errors">{{$errors->first('category2')}}</span>
</div>
<div>
{{Form::submit('Save',array('class' => 'btn'))}}
</div>