how to modify parameters like (style , class ..) in cakephp input? - html

in cakephp
echo $this->Form->input('email', array('type' => 'email'));
will render
<div class="input email">
<label for="UserEmail">Email</label>
<input type="email" name="data[User][email]" value="" id="UserEmail" />
how to make this like that
<input type="email" name="data[User][email]" value="" id="UserEmail" class="input_class" style="some:style;" />

Just add a "class" and/or "style" argument to your options array.
echo $this->Form->input('email', array('type' => 'email', 'class' => 'input_class', 'style' => 'some:style' ));
See the the FormHelper documentation for a list of all options.

if you need only input without lable you can also try in this way
echo $this->Form->input('email', array('type' => 'email','div'=>false, 'class' => 'input_class', 'style' => 'some:style' ));

Related

how to insert multiple row data based on checkbox selection in Laravel

I'm stuck with multiple row insertion in laravel. I keep on getting an error which says:
Trying to access array offset on value of type null.
blade.php file:
<tbody>
#forelse($GIV as $GIV)
<tr>
<td> <input type="checkbox" name="status[{{$GIV->id}}]" id="status" value="Issued" ></td>
<td> <input type="text" class="form-control" id="id" name="item_id[{{$GIV->id}}]" hidden="" value="{{$GIV->id}}">{{$GIV->id}}</td>
<td> <input type="text" class="form-control" id="pr_no" name="pr_no[{{$GIV->id}}]" hidden="" value="{{$GIV->pr_no}}">{{$GIV->pr_no}}</td>
<td> <input type="text" class="form-control" id="dep_name" name="dep_name[{{$GIV->id}}]" hidden="" value="{{$GIV->dep_name}}">{{$GIV->dep_name}}</td>
<td> <input type="hidden" class="form-control" id="item_name" name="item_name[{{$GIV->id}}]" value="{{$GIV->item_name}}">{{$GIV->item_name}}</td>
<td> <input type="text" class="form-control" id="description" name="description[{{$GIV->id}}]" hidden="" value="{{$GIV->description}}" >{{$GIV->description}}</td>
<td> <input type="number" class="form-control" id="item_qty" name="item_qty[{{$GIV->id}}]" hidden="" value="{{$GIV->item_qty}}">{{$GIV->item_qty}}</td>
<td> <input type="text" class="form-control" id="unit_measure" name="unit_measure[{{$GIV->id}}]" hidden="" value="{{$GIV->unit_measure}}">{{$GIV->unit_measure}}</td>
<td> <input type="number" class="form-control" id="authorized_qty" name="authorized_qty[{{$GIV->id}}]" hidden="" value="{{$GIV->authorized_qty}}">{{$GIV->authorized_qty}}</td>
</tr>
#empty
<tr><td colspan="16"><center>No Data Found</center</td></tr>
#endforelse
</tbody>
Controller file:
public function addDataGIV(Request $request)
{
$data =request()->all;
foreach(request()->status as $status) {
DB::table('g_i_v_s')->insert(
[
'item_id' =>$data['item_id'][$status],
'item_name' =>$data['item_name'][$status],
'description' =>$data['description'][$status],
'item_qty' =>$data['item_qty'][$status],
'unit_measure' =>$data['unit_measure'][$status],
'authorized_qty'=>$data['authorized_qty'][$status],
'dep_name' =>$data['dep_name'][$status],
'pr_no' =>$data['pr_no'][$status],
]
);
}
}
When a user selects the checkbox and clicks the submit button, I want only the selected checkbox row including the checkbox value to be stored into the database, without any errors.
You're actually quite close, but you're accessing your Array indices wrong; $status, if defined properly, would be Issued, which is not what you want. Also, request()->all is null, but request()->all() (with the ()) would be an Array of your form input; hence the error when trying to use $data[...][...]; later.
This code should work (Note: you don't need $data at all here):
foreach(request()->input('status', []) as $index => $status) {
DB::table('g_i_v_s')->insert([
'item_id' => request()->input("item_id.{$index}"),
'item_name' => request()->input("item_name.{$index}"),
'description' => request()->input("description.{$index}"),
'item_qty' => request()->input("item_qty.{$index}"),
'unit_measure' => request()->input("unit_measure.{$index}"),
'authorized_qty' => request()->input("authorized_qty.{$index}"),
'dep_name' => request()->input("dep_name.{$index}"),
'pr_no' => request()->input("pr_no.{$index}"),
]);
}
Alternative syntax would be request->input('item_id')[$index], like:
foreach(request()->input('status', []) as $index => $status) {
DB::table('g_i_v_s')->insert([
'item_id' => request()->input('item_id')[$index],
'item_name' => request()->input('item_name')[$index],
'description' => request()->input('description')[$index],
'item_qty' => request()->input('item_qty')[$index],
'unit_measure' => request()->input('unit_measure')[$index],
'authorized_qty' => request()->input('authorized_qty')[$index],
'dep_name' => request()->input('dep_name')[$index],
'pr_no' => request()->input('pr_no')[$index],
]);
}
Basically, when looping over request()->input('status', []), you have access to $index, and $status (which is not referenced, but that's ok). $index will be equivalent to whatever $giv->id is on the front-end, and each field exists as an Array Index with that value, so looping and referencing is super easy.

Input type file activeform Yii2 generate another input type hidden

I have some problem when using activeform type file. When i try to send my input in registration controller the validation from model always return it false because there is input type file that empty. After i checking from html inspect elements, it turns out that the form I created on the register page generates a new input on it with the type hidden and the same name. I still can't understand why it can generate another input type hidden and input post in controller read from that input type hidden
this is my activeform:
<?php $form = ActiveForm::begin(['id' => 'form-signup', 'action' => '/site/register/userregister', 'options' => ['enctype' => 'multipart/form-data'], 'method' => 'post' ]); ?>
<?= $form->field($model,'email')->textInput()->input('email', ['placeholder' => "Email"])->label(false) ?>
<?= $form->field($model,'photofile',
['options' => ['class' => 'form-group']])
->fileInput([])
->label('Photo file<br/><span style="font-size:12px;font-style:italic;">(Max. size 1 MB, Allowed type: .jpg, .jpeg, .png)</span>') ?>
<?= $form->field($model,'resumefile',
['options' => ['class' => 'form-group']])
->fileInput([])
->label('Resume file<br/><span style="font-size:12px;font-style:italic;">(Max. size 1 MB, Allowed type: .doc, .docx, .pdf)') ?>
<div class="form-group">
<?= Html::submitButton('Sign Up', ['class' => 'btn btn-sign', 'type' => 'submit']) ?>
</div>
<h6 class="mt-3">Have an account? <a class="font-weight-bold" href="<?= Url::to(['login/userlogin']) ?>">Sign In</a></h6>
<?php ActiveForm::end(); ?>
this my controller :
public function actionUserregister()
{
$model = new SignupModel();
if (Yii::$app->request->isPost) {
$input = Yii::$app->request->post('SignupModel');
echo "<pre>";var_dump($input);exit;
}
}
this is function in to validate rule input:
public function rules()
{
return [
['photofile', 'required'],
['photofile', 'file', 'extensions' => 'jpg,jpeg,png', 'maxSize' => 1024 * 1024 * 1],
['resumefile', 'required'],
['resumefile', 'file', 'extensions' => 'pdf,doc,docx', 'maxSize' => 1024 * 1024 * 1 ],
];
}
and this is what i see in inspect elements:
<div class="form-group field-signupmodel-photofile required">
<label class="control-label" for="signupmodel-photofile">Photo file</label>
<input type="hidden" name="SignupModel[photofile]" value> //here is the problem
<input type="file" id="signupmodel-photofile" name="SignupModel[photofile]" aria-required="true">
<p class="help-block help-block-error"></p>
</div>
<div class="form-group field-signupmodel-resumefile required">
<label class="control-label" for="signupmodel-resumefile">Resume file</label>
<input type="hidden" name="SignupModel[resumefile]" value> //here is the problem
<input type="file" id="signupmodel-resumefile" name="SignupModel[resumefile]" aria-required="true">
<p class="help-block help-block-error"></p>
</div>
what's wrong with my code above?

Custom data attribute need Dynamic Values using Yii2

Using Yii2 Framework, Get data from ArrayHelper::map(), I'm not able to get dynamic data
<input class="services-checkbox" name="SubscriptionPackage[services][]" value="1" data-serviceid="id" type="checkbox">
<input class="services-checkbox" name="SubscriptionPackage[services][]" value="2" data-serviceid="id" type="checkbox">
<input class="services-checkbox" name="SubscriptionPackage[services][]" value="3" data-serviceid="id" type="checkbox">
<?= $form->field($model, 'services')->checkboxList(ArrayHelper::map($activeServiceModels, 'id', 'name'), ['itemOptions' => ['class' => 'services-checkbox','data-serviceid'=>'id']])->label('Select Service(s):'); ?>
I need Output
<input class="services-checkbox" name="SubscriptionPackage[services][]" value="1" data-serviceid="1" type="checkbox">
<input class="services-checkbox" name="SubscriptionPackage[services][]" value="1" data-serviceid="2" type="checkbox">
<input class="services-checkbox" name="SubscriptionPackage[services][]" value="1" data-serviceid="3" type="checkbox">
update your code like below
<?=
$form->field($model, 'services')->checkboxList(ArrayHelper::map($activeServiceModels, 'id', 'name'), ['class'=>"btn-checkbox",
'item' => function($index, $label, $name, $checked, $value) {
return "<label class='col-md-4'><input type='checkbox' data-serviceid="1" {$checked} name='{$name}' value='{$value}'>{$label}</label>";
}
]);
?>
add your class 'class'=>"btn-checkbox" into the array.
and you can debug more inside the item callback function for customization.
You can use item callable function to render each checkbox item, eg:
$form->field($model, 'name')->checkboxList(ArrayHelper::map($models, 'id', 'name'), [
'itemOptions' => ['class' => 'services-checkbox','data-serviceid'=>'id'],
'item' => function ($index, $label, $name, $checked, $value) {
return Html::checkbox($name, $checked, ['data-serviceid' => $value, 'value' => 1]) . Html::label($label);
}
])->label('Select Service(s):');

Yii2 kartik datetime picker widget doesn´t function?

I wanted put yii2 kartik datetimepicker into a reservation room form, but datepicker doesn´t function.
Im sure , i have use kartik/datetime/DateTimePicker; at the top of code.
This is my code for reservation form. Its not complete, but i want test posting date.
<div class="form-group">
<label> Booked room from </label>
<?php
echo DateTimePicker::widget([
'name' => 'date_in_modal_1',
'options' => ['placeholder' => 'Date and time...'],
'pluginOptions' => ['autoclose' => true]
]);
?>
</div>
<div class="form-group">
<label> Booked room to </label>
<?php
echo DateTimePicker::widget([
'name' => 'date_in_modal_2',
'options' => ['placeholder' => 'Date and time...'],
'pluginOptions' => ['autoclose' => true]
]);
?>
</div>
<input type="hidden" name="_csrf" value="<?=Yii::$app->request->getCsrfToken()?>" />
<button type="submit" class="btn btn-primary">Add</button>
I using the same code in index view and this work. The inputs shows, but if i click, the form with date pick and time pick doesn´t show. Can someone help me with this problem?
Thanks u all.

Cant access image data in cakephp 2.0 through custom form

I have created one function to add users. The parameters are username,email,password and profile image.
<form name="User" method="post" action="http://192.168.1.100/filmtastic/api/users/adduser" ENCTYPE="multipart/form-data">
<table>
<tr><td><label>username:</label></td><td><input type="text" name="username"></td></tr>
<tr><td><label>password:</label></td><td><input type="text" name="password"></td></tr>
<tr><td><label>email:</label></td><td><input type="text" name="email"></td></tr>
<tr><td><label>Image:</label></td><td><input type="file" name="image"></td></tr>
<tr><td colspan="2" align="center"><input type="submit" name="submit" value="Submit" /></td></tr></table>
</form>
Now in my UserController i have:
public function api_adduser() {
$this->layout = false;
$this->request->data['User']= $this->request->data;
if($this->request->data['User'] != array()) {
pr($this->request->data); die();
}
}
here i debug the data which is passed by that HTML form and optput is like
Array
(
[username] => jack roy
[password] => jack
[email] => jack#yahoo.com
[submit] => Submit
[User] => Array
(
[username] => jack roy
[password] => jack
[email] => jack#yahoo.com
[submit] => Submit
)
)
The problem is that it will not show me the image array. can you tell me how i will get that image array which suppose to look like
Array
(
[image] => Array
(
[name] => 06_01_2009_0692878001231258160_nanzig.jpg
[type] => image/jpeg
[tmp_name] => C:\wamp\tmp\php368F.tmp
[error] => 0
[size] => 81167
)
)
Maybe you simply forgot to set the form mime type to multipart/form-data. Here is my demo view, called add.ctp
<?php
echo $this->Form->create('User', array('enctype' => 'multipart/form-data'));
echo $this->Form->input('name');
echo $this->Form->password('password');
echo $this->Form->input('email');
echo $this->Form->file('image');
echo $this->Form->submit();
echo $this->Form->end();
Here is the UserController
class UserController extends AppController {
public $helpers = array('Html', 'Form');
public function add() {
pr($this->request->data);
}
}
And here is how the result looks like when you submit the form
Array
(
[User] => Array
(
[name] => admin
[password] => admin
[email] => asdf#qwerty.com
[image] => Array
(
[name] => cakephp-book.odt
[type] => application/vnd.oasis.opendocument.text
[tmp_name] => /tmp/phpKniokr
[error] => 0
[size] => 170247
)
)
)
Here is a dump of the generated HTML which you may want to use on your external page (may I ask why???). Note how many things the FormHelper adds... I wonder why you build an external view if your application is still accessing your server directly...
<form action="/test/cakedemo/user/add" enctype="multipart/form-data" id="UserAddForm"
method="post" accept-charset="utf-8">
<div style="display:none;">
<input type="hidden" name="_method" value="POST" />
</div>
<div class="input text">
<label for="UserName">Name</label><input name="data[User][name]"
id="UserName" />
</div><input name="data[User][password]" type="password" id="UserPassword" />
<div class="input text">
<label for="UserEmail">Email</label><input name="data[User][email]" type="text"
id="UserEmail" />
</div><input type="file" name="data[User][image]" id="UserImage" />
<div class="submit">
<input type="submit" value="Submit" />
</div>
</form>