I've read a lot of laravel tutorial to do this but none has worked for me.
This is my form
<div class="row increment control-group">
{{-- Request Item --}}
<div class="form-group col-md-4">
<input type="text" class="form-control" name="request_item[]" placeholder="Item" />
</div>
{{-- Request Description --}}
<div class="form-group col-md-4">
<input type="text" class="form-control" name="request_description[]" placeholder="Item Description" />
</div>
<div class="form-group col-md-4">
<button class="btn btn-success" id="btn-item" type="button">Add</button>
</div>
</div>
This is my controller
$request->validate([
'request_no' => 'required|max:255',
'request_date' => 'required|date', //unique:(tablename)
'request_item' => 'required|max:255',
'request_description' => 'max:255',
'request_by' => 'required|max:255',
'request_status' => 'required|max:255',
'request_scan' => 'mimes:pdf',
'created_by' => 'max:255',
'updated_by' => 'max:255',
]);
//For file uploading
$name="";
if($request->hasfile('filename')){
$file = $request->file('filename');
$name = time()."_".$file->getClientOriginalName();
$file->move(public_path(). '/images/', $name);
}
$itemArray = Input::get('request_item');
$count = count($itemArray);
for($i = 0; $i < $count; $i++){
$request= new \Trisco\IT_Request;
$request->request_no=$request->get('request_no');
$request->request_date=$request->get('request_date');
$request->request_item=$itemArray[$i];
$request->reqeust_description='';
$request->request_by=$request->get('request_by');
$request->request_status=$request->get('request_status');
$passport->request_scan = $name; //file
$request->added_by=$request->get('username');
$request->updated_by=$request->get('username');
$request->save();
}
The request_item & request_description are both string only.
Can anyone help me?
Thanks in advance.
how about you loop on your array?
$itemArray = Input::get('request_item');
foreach ($itemArray as $value) {
$request->request_item=$value;
//your code here
//request->save()
}
What is the error?
check also the spelling --
$request->reqeust_description='';
try this >>
$request->request_item= implode(',', $request['request_item']);
Related
I'm making a form so that users can request to create a new account to the admin, but every time I try to send data, it always fails, even though I've checked every part of my code, can anyone help me?
the form code :
<form method="POST" action="{{ url('/request-akun/make-request') }}">
#csrf
<div class="form-group col-md-12">
<label for="name">Nama</label>
<input type="text" name="name" class="form-control" id="name">
</div>
<div class="form-group col-md-12">
<label for="email">Email</label>
<input type="text" name="email" class="form-control" id="email">
</div>
<div class="form-group col-md-12">
<label for="password">Password</label>
<input type="text" name="password" class="form-control"
id="password">
</div>
<div class="form-group col-md-12">
<label for="password_confirm">Konfirmasi Password</label>
<input name="password_confirm" type="text" class="form-control"
id="password">
</div>
<button type="submit" class="btn btn-primary pull-right"
style="margin-right: 15px;">Request</button>
</form>
this is the controller :
public function requestAccount(Request $request){
$request->validate([
'name' => 'required|string|max:255',
'ra_instansi_id' => 'required',
'email' => 'required|string|email|max:255',
'password' => 'required',
'password_confirm' => 'required'
]);
if ($request->get('password') == $request->get('password_confirm')){
$data = new requestAkun();
$data->name = $request->get('name');
$data->ra_user_id = Auth::id();
$data->ra_instansi_id = Auth::user()->instansiID;
$data->email = $request->get('email');
$data->password = $request->get('password');
$data->status = "pending";
$data->save();
return Redirect::to('/request-akun/request-lists')->with('success','Request Berhasil dikirim');
} else {
return Redirect::to('/request-akun/request-lists')->with('error','Password tidak sama');
}
}
this is the model code:
class requestAkun extends Model
{
use HasFactory, Uuids;
protected $table = 'request_akun';
public $timestamps = true;
protected $fillable = [
'name',
'ra_instansi_id',
'ra_user_id',
'email',
'password',
'status'
];
}
this is the route :
Route::prefix('request-akun')->group(function(){
Route::post('/make-request',[userController::class,'requestAccount'])->name('make-request');
});
Error is you don't have ra_instansi_id input in your form.
Not showing any validation error message in your form so better show errors so you check which validation failed.
3.Instead of checking password match in if condition you can change input name password_confirm to password_confirmation so you can change like below
<input name="password_confirmation" type="text" class="form-control"
id="password_confirmation">
and in your validation
$validator=Validator::make($request->all(),[
'name' => 'required|string|max:255',
'ra_instansi_id' => 'required',
'email' => 'required|string|email|max:255',
'password' => 'required|confirmed',
'password_confirmation' => 'required'
]);
dd($validator->errors());
I want to store the selected checks in a single variable and send it to the database, I am only using migrations, others that other data is sent in the migration.
Pd: I get this error by the array:
ErrorException
Array to string conversion
My template.
<form action="{{ route('save') }}" method="POST">
#csrf
<div class="row">
<div class="col-md-6 mt-5">
<select name="Sede" class="form-select" aria-label="Default select example">
<option selected name="Sede">Campus Sede</option>
<option value="Cancún">Cancún</option>
<option value="Chihuahua">Chihuahua</option>
</select>
</div>
<div class="col-md-6 mt-5">
<select name="Alcance" class="form-select" aria-label="Default select example">
<option selected name>Alcance</option>
<option value="Local">Local</option>
<option value="Nacional">Nacional</option>
</select>
</div>
<div class="col-md-6 mt-5">
<label for="">Nombre del Proyecto</label>
<input type="text" name="Nombre" class="form-control col-12">
</div>
<div class="col-md-6 mt-5">
<label for="">Cierre de Inscripciones</label>
<input data-date-format="yyyy/mm/dd" id="datepicker" name="Cierre" class="form-control col-12">
</div>
<div class="col-md-6 mt-5">
<input type="checkbox" name="Areas[]"id="ingenieria" value="Ingeniería" />
<label for="ingenieria">Ingeniería</label>
<input type="checkbox" name="Areas[]"id="humanidades" value="Humanidades" />
<label for="humanidades">Humanidades</label>
<input type="checkbox" name="Areas[]"id="negocios" value="Negocios" />
<label for="negocios">Negocios</label>
<input type="checkbox" name="Areas[]" id="salud" value="Salud" />
<label for="salud">Salud</label>
</div>
<div class="col-md-12 mt-5">
<label for="">Cual es el departamento donde impactara directamente el proyecto?</label>
<input type="text" name="P1" class="form-control col-12">
</div>
<div class="row form-group mt-5">
<button type="submit" class="btn btn-success col-md-2 offset-5">Guardar</button>
</div>
</form>
My controller:
public function save(Request $request){
$validator = $this->validate($request, [
'Sede'=> 'required|string|max:255',
'Alcance'=> 'required|string|max:255',
'Nombre'=> 'required|string|max:255',
'Cierre'=> 'required|date',
'Areas'=> 'required',
'P1'=> 'required|string|max:255',
'P2'=> 'required|string|max:255',
'P3'=> 'required|string|max:255',
'P4'=> 'required|string|max:255'
]);
$data = request()->except('_token');
Project::insert($data);
return back()->with('datosGuardados', 'Datos almacenados, Correctamente');
}
My Model:
class Project extends Model
{
protected $fillable = ['Sede','Alcance','Nombre','Cierre','Areas','P1','P2','P3','P4'];
protected $casts = [
'Areas' => 'array'
];
}
My migration:
public function up()
{
Schema::create('projects', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('Sede');
$table->string('Alcance');
$table->string('Nombre');
$table->date('Cierre');
$table->string('Areas');
$table->string('P1');
$table->string('P2');
$table->string('P3');
$table->string('P4');
$table->timestamps();
});
}
enter image description here
As you see the error is due to the checkbox that I don't know how to send it as a single array and store it in the database, I would appreciate any help. I was investigating and I did not find anything
$validator = $this->validate($request, [
'Sede'=> 'required|string|max:255',
'Alcance'=> 'required|string|max:255',
'Nombre'=> 'required|string|max:255',
'Cierre'=> 'required|date',
'Areas'=> 'required',
'P1'=> 'required|string|max:255',
'P2'=> 'required|string|max:255',
'P3'=> 'required|string|max:255',
'P4'=> 'required|string|max:255'
]);
// Add this line
$area = [];
foreach ($request->Areas as $key => $value) {
$area[$key] = $value;
}
// And then save in database with $area created using foreach.
Use this 'Areas.*'=> 'required', for validating the checkbox generally, moreover to insert the data under Areas, you can insert it via converting by json_encode($request->Areas);
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?
I'm trying to create a picture for every contact.
this is the store() method in my controller:
public function store()
{
$attributes = request()->validate([
'user_id' => ['required'],
'avatar' => ['required',],
'ime' => ['required', 'min:3'],
'prezime' => ['required', 'min:3'],
'broj' => ['required', 'min:3'],
]);
Kontakt::create($attributes);
return redirect('/imenikk');
}
this is the create.blade.php:
<form method="POST" action="/imenikk">
{{ csrf_field() }}
<div class="container text-center">
<label for="ime">Ime</label>
<input type="text" class="form-control text-center" name="ime"
placeholder="Ime">
<label for="prezime">Prezime</label>
<input type="text" class="form-control text-center" name="prezime"
placeholder="Prezime">
<label for="broj">Broj</label>
<input type="text" class="form-control text-center" name="broj"
placeholder="Broj">
<br>
<input type="file" name="avatar" accept="image/*">
<br>
<input type="hidden" name="user_id" value='{{$user_id}}'>
<button type="submit" class="btn btn-primary">Dodaj
</div>
</form>
And this is on show.blade.php:
<img src="data:image/jpg;base64,{{ chunk_split(base64_encode($imenikk->avatar)) }}" height="500" width="500">
<img src ="data:image/jpeg;base64,{{base64_encode($imenikk->avatar)}}" height="200" width="200">
Neither of these 2 are working.. I get this but the image doesn't load:
<img src="data:image/jpg;base64,SU1HXzYxODQuSlBH" height="500" width="500">
This is how my db looks when I create a contact with picture:
Also I have managed to decode this
img src="data:image/jpg;base64,SU1HXzYxODQuSlBH
and I get the image name when I decode it yet the page still doesn't display the image.
It seems your base64 code is wrong.
Try to get the real base64 code from image file, and store in database.
public function store()
{
$attributes = request()->validate([
'user_id' => ['required'],
'avatar' => ['required',],
'ime' => ['required', 'min:3'],
'prezime' => ['required', 'min:3'],
'broj' => ['required', 'min:3'],
]);
$file = request()->file('avatar');
$imagedata = file_get_contents($file->getRealPath());;
$base64 = base64_encode($imagedata);
$attributes['avatar'] = $base64;
Kontakt::create($attributes);
return redirect('/imenikk');
}
then store it in your field,
and display it like this:
<img src={{ "data:image/jpg;base64,".$imenikk->avatar }} height="500" width="500">
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.