I'm trying to build some custom grid forms which looks like
form.blade.php
<table>
<tr>
<td>
{{ Form::text('name[]', null, array('placeholder' => 'name') }}
</td>
</tr>
<tr>
<td>
{{ Form::text('aerobic[]', null, array('placeholder' => 'aerobic') }}
</td>
</tr>
<tr>
<td>
{{ Form::text('core_test[]', null, array('placeholder' => 'core test') }}
</td>
</tr>
</table>
Validation rules
$validator = Validator::make(Input::all(), array(
'name[]' => 'required|alpha',
'aerobic[]' => 'required|alpha',
'core_test[]' => 'required|alpha'
));
How do I validate the inputs or what other good ways would be to build this?
I get the following errors when I submit the form
htmlentities() expects parameter 1 to be string, array given
(View: /Applications/XAMPP/xamppfiles/htdocs/hp/v1.1/app/views
/files/templates/apa/form.blade.php)
I've also tried this
$validator = Validator::make(Input::all(), array(
'name' => 'required|alpha',
'aerobic' => 'required|alpha',
'core_test' => 'required|alpha'
));
And I get this error from alpha validation rule
preg_match() expects parameter 2 to be string, array given
I've spend a lot of time trying to figure out how to solve this problem...Laravel does not expect a text input to be an array. so you either use plain html for your array inputs, or use custom macros to create your own form class(I assume you're using LaravelCollective).
Take off the [] in the name of each of your inputs. PHP is interpreting those inputs as arrays instead of strings.
Example:
{{ Form::text('name', null, array('placeholder' => 'Name')) }}
Related
How to pass data collection to mail? I don't know the proper term but set of data I guess. I want to display a list of users in my pdf. Every time I use foreach in blade, it fails to send the mail. Ex. I want to display the users John Doe, Jane Doe, Peter Parker from model Signature to my pdf mail. my reference for sending mail with pdf.
In my controller
public function sendEmail(Request $request)
{
$sig = Signature::all();
$users = StudentApplicant::whereIn("id", $request->ids)->get();
foreach ($users as $key => $user) {
$data = [
'studnum' => $user->users->stud_num,
'fname' => $user->users->first_name,
'mname' => $user->users->middle_name,
'lname' => $user->users->last_name,
'gwa' => $user->gwa,
'award' => $user->award_applied,
'award_name' => $user->award->name,
'sy' => $user->school_year
];
$details = ['email' => $user->users->email];
SendEmailJob::dispatch($details, $data, $data['studnum'], $sig);
}
StudentApplicant::whereIn("id", $request->ids)->update(['certificate_status' => 1]);
return response()->json(['success' => 'Send email successfully. Refresh the page']);
}
cert blade file
<table class="table2">
<tr>
#foreach ($sig->take(3) as $item)
<td width="21%">
<b>{{ $item->name }}</b><br>
{{ $item->position }}
</td>
#endforeach
</tr>
</table>
<table class="table2">
<tr>
#foreach ($sig as $item)
#if ($loop->last)
<td>
<b>{{ $item->name }}</b><br>
{{ $item->position }}
</td>
#endif
#endforeach
</tr>
</table>
I retrieved form input in three ways in codeigniter? I am unsure which one is correct. I have given this line
$this->load->view('userview',$data);
Is this correct? What is the right way to input the data from the form? When should I use an array?
I also want to know if the record was added successfully. After submitting the form, which function would I have to use and where would I put it?
view folder file name userview.php
userview.php
<form name="f1" action="" method="post"/>
<table width="500" border="1">
<tr>
<td>UserName</td>
<td>:</td>
<td><input type="text" name="username" value=""/></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input type="password" name="password" value=""/></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input type="text" name="email" value=""/></td>
</tr>
<tr>
<td colspan="3"><input type="submit" name="submit" value="Save"/></td>
</tr>
</table>
</form>
First one-created Array and stored into variable:
public function index()
{
$data = array();
if($this->input->post('submit') != NULL ){
$postData = $this->input->post();
$data['response'] = $postData;
}
$this->load->view('userview',$data);
}
Second one: retrieved input variable within array
public function index()
{
$data['response']=array('username' => $this->input->post('username'),
'password' => $this->input->post('password'),
'email' => $this->input->post('email'));
$this->load->view('userview',$data);
}
Third one: created one method within index function
public function index()
{
$this->load->view('userview');
$this->getvalue();
}
public function getvalue()
{
if($this->input->post('submit')!==null)
{
$data['response']=array('username' => $this->input->post('username'),
'password' => $this->input->post('password'),
'email' => $this->input->post('email'));
$this->load->view('viewuser',$data);
}
}
All are OK, Personally I use the following when I have multiple fields:
$UserDetails = $this->input->post(['username', 'email', 'password']);
This would return a key => value pair array that contains the 3 fields I need only.
In your first example, you might be returning extra fields that you don't need.
Your second example is very verbose for my taste but it's OK.
3rd example is also very verbose for my taste but it's OK.
I would use form validation instead of manually checking if the form is posted.
$data = [];
$form_validation = array(
['field' => 'username', 'label' => 'Username', 'rules' => 'trim|required'],
['field' => 'email', 'label' => 'Email', 'rules' => 'trim|required|email'],
['field' => 'password', 'label' => 'Password', 'rules' => 'trim|required'],
);
$this->form_validation->set_rules($form_validation);
if ( $this->form_validation->run() === false ) {
// set some error messages here
}else{
// get data here
$data['UserDetails'] = $this->input->post(['username', 'email', 'password']);
}
// pass data to view ??
$this->load->view('view', $data)
Here you go:
Form Validation: https://www.codeigniter.com/userguide3/libraries/form_validation.html
Input Class: https://www.codeigniter.com/userguide3/libraries/input.html
Form Helper: https://www.codeigniter.com/userguide3/helpers/form_helper.html
Good luck : )
I am trying to create a Collection and pass it to blade. My PHP code look like this
$collection1 = collect(['name' => 'Alex', 'id' => '1']);
$collection2 = collect(['name' => 'John', 'id' => '2']);
$collection3 = collect(['name' => 'Andy', 'id' => '3']);
$people_col = new Collection();
$people_col->push($collection1);
$people_col->push($collection2);
$people_col->push($collection3);
return view('test',[
'people_col' => $people_col
]);
In my blade I loop through people_col to get properties of items:
#foreach ($people_col as $people)
<tr>
<td>{{ $people->name }}</td>
<td>{{ $people->id }}</td>
</tr>
#endforeach
However I got this error:
Property [name] does not exist on this collection instance
Any idea?
Thanks
You are creating a collection of collections where you should have created a collection of objects instead.
With your current implementation you should be able to get the values using array access method {{ $people['name'] }} or using get method on collection {{ $people->get('name') }}
If you created a collection of objects like below and return it instead of what you are doing now
$people_col = collect([
(object) ['name' => 'Alex', 'id' => '1'],
(object) ['name' => 'John', 'id' => '2'],
(object) ['name' => 'Andy', 'id' => '3']
]);
return view('test', [
'people_col' => $people_col
]);
Then in your view you should be able to access people object like you have done in your code.
#foreach ($people_col as $people)
<tr>
<td>{{ $people->name }}</td>
<td>{{ $people->id }}</td>
</tr>
#endforeach
Try to access to the property name this way: $people['name']
#foreach ($people_col as $people)
<tr>
<td>{{ $people['name'] }}</td>
<td>{{ $people['id'] }}</td>
</tr>
#endforeach
No matter which validation rule i brake as long as i have an array notation in input name like this
<div class="form-group">
{!! Form::label('titile', '* Eventname: ', ['class' => 'control-label']) !!}
{!! Form::text('title[]', null, ['class' => 'form-control', 'required']) !!}
</div>
i get this error:
I have tried to use simple plain html input like this
<input type="text" name="title[]" />
and even like this
{!! Form::text('title', null, ['name' => 'title[]','class' => 'form-control', 'required']) !!}
But nothing works.
Only if i make the input field without array notation [] the validation works properly...
my validation rule is this
$this->validate($request, [
'title' => 'required|min:2',
]);
I don't know what else to do, if anyone had similar problem please help.
UPDATE:
i have tried it like this now with only one form input:
<div class="form-group">
{!! Form::label('title', '* Eventname: ', ['class' => 'control-label']) !!}
{!! Form::text('title', null, ['name' => 'title[]','class' => 'form-control', 'required', 'placeholder' => 'z.B. Deutscher Filmpreis']) !!}
</div>
-
public function rules()
{
$rules = [
];
foreach($this->get('title') as $key => $val)
{
$rules['title.'.$key] = 'numeric';
}
return $rules;
}
Write your validation in individual request file. This ('title' => 'required|min:2') validation does not work for array input. Try this technique for dynamic field validation.
public function rules()
{
$rules = [
];
foreach($this->request->get('title') as $key => $val)
{
$rules['title.'.$key] = 'required|min:2';
}
return $rules;
}
Very Good example at laravel news site.
https://laravel-news.com/2015/11/laravel-5-2-a-look-at-whats-coming/
OK i finally solved this. In Laravel 5.3 something is changed and you can't put empty array brackets for field name.
You must declare indices inside...for example:
this doesn't work
{!! Form::text('title[]', null, ['class' => 'form-control', 'required']) !!}
but this works
{!! Form::text('title[0]', null, ['class' => 'form-control', 'required']) !!}
UPDATE
So after solving this now i know that if you put empty [ ] brackets this will work if you have multiple input fields with same name...but if you put empty brackets and you have an option to add new fields dynamically like i did...then that single field with empty array brackets will fail because you actually don't have an array...and you must put [0] some indices inside...
and then it works
I'm working with Laravel 5 and Bootgrid Pagination. I'm planning to use it later so I'm learning it and playing around with it but I can't make it work. In my browser console I get
Uncaught TypeError: Cannot read property 'length' of undefined
I don't know if it is the data format that I'm passing.
Here's the html
<table id="grid-data" class="table table-condensed table-hover table-striped" data-toggle="bootgrid" data-ajax="true" data-url="api/deliverytracker">
<thead>
<tr>
<th data-column-id="id" data-identifier="true">ID</th>
<th data-column-id="sender">Sender</th>
<th data-column-id="received">Recieved</th>
</tr>
</thead>
</table>
in my route for api/deliverytracker
Route::any('api/deliverytracker', 'DeliveryTrackerController#deliveryIndexApi');
Then my function in my DeliveryTrackerController
public function deliveryIndexApi()
{
$data = array(
array('id' => '1', 'sender' => '123#test.de', 'received' => '2014-05-30T22:15:00'),
array('id' => '2', 'sender' => '123#test.de', 'received' => '2014-05-30T22:15:00'),
);
return json_encode($data);
}
Did I missed something? I tried to console log my $data which turns out
[{"id":"1","sender":"123#test.de","received":"2014-05-30T22:15:00"},{"id":"2","sender":"123#test.de","received":"2014-05-30T22:15:00"}]
Thanks!
You need to return the JSON correctly .
$data = array(
"current" => 1,
"rowCount" => 10,
"rows" => array(
array('id' => '1', 'sender' => '123#test.de', 'received' => '2014-05-30T22:15:00'),
array('id' => '2', 'sender' => '123#test.de', 'received' => '2014-05-30T22:15:00')
),
"total" => 1123
);
check official documentation http://www.jquery-bootgrid.com/Examples#data