I am working on Laravel. I have a blade view page on which multiple forms (of same model) are created. Now, I want, that when I click submit button, an array of all the forms should be returned to controller. But, it returns data of only one form instead of array. How can I achieve this, Can anyone help me?
It's not a real Laravel issue.
The trick is using different forms with each a submit button. Then, it's simple. Check the submitted button.
if ($request->isMethod('post')) {
if ($request->has('submit_button_form_1')) {
// Handle form
}
elseif ($request->has('submit_button_form_2')) {
// Handle form
}
elseif ($request->has('submit_button_form_3')) {
// Handle form
}
}
First of all I dont think you can send multiple forms, that is just way it is.
You have two options.
First: Make one form with all fields from all forms, and then in controller you just take what field you want, just like you would do from arrays so same thing.
Like:
$all_fields= Request::all();
and then
$all_fields['something'], $all_fields['something_else'],
Second: Using ajax, you can manipulate and send values of fields, fields that you need.
What you can do is you need to loop the form field with naming structure as first_name1, first_name2 and so on. Then, after submitting the form, you can validate using the loop in the same way and get the values. You can check my sample code here. It count the number of loops to be made.
You can pass the counter from the view to the controller in hidden field. Then, you can loop for validation and for taking the inputs.
Check my sample code:
//determine number of rows in database
$number_of_loop = Input::get('number_of_loop');
$arrayList = []; //defining the array variable
//declaring the validations rules
for($i = 1; $i < $number_of_loop; $i++){
$rules = array(
'currency'.$i => 'required',
'iso_code'.$i => 'required',
'symbol'.$i => 'required',
'status'.$i => 'required'
);
$validator = Validator::make(Input::all(), $rules);
if($validator->fails()){
return Redirect::back()->withErrors($validator)->withInput();
}
}
for ($i=1; $i <= $number_of_loop; $i++) {
$new_currency = new stdClass();
$currency = Input::get('currency' . $i);
$iso_code = Input::get('iso_code'.$i);
$symbol = Input::get('symbol'.$i);
$conversion_rate = Input::get('conversion_rate'.$i);
$status = Input::get('status'.$i);
$new_currency->currency = $currency;
$new_currency->iso_code = $iso_code;
$new_currency->symbol = $symbol;
$new_currency->conversion_rate = $conversion_rate;
$new_currency->enable = $status;
array_push($arrayList, $new_currency);
}
Update1
For storing all the records, create an array and a new stdclass. Store all the variable in stdclass attributes. and at last, push the new stdclass to the array. Then, you can access all the inputs of form. Check above updated code sample
If you’re using Laravel 5.2 then you can validate array data. You’ll need to put all of your inputs in one form as you just can’t send multiple forms. If each form had different URLs for the action attribute, how would that work?
To send data as an array, you can suffix the name attribute with []:
{!! Form::open() !!}
{!! Form::text('name[]') !!}
{!! Form::text('name[]') !!}
{!! Form::text('name[]') !!}
{!! Form::close() !!}
When this form is submitted, name will be an array with three elements.
To validate, use a wildcard in your validation rule name:
return [
'name.*' => 'required|max:255',
];
You can then access the array of names in your controller through the Request object:
$names = $request->get('names', []);
foreach ($names as $name) {
User::create(['name' => $name]);
}
I don’t know your data structure as you haven’t included it in your question, but this should be enough for you to apply to your given scenario.
Related
I've a form with 120 fields to insert into the DB. The form is inserting fine and the approach I used is below:
I'm fetching all the fields from the view as below in the controller and passing the array($postdata) to the model file to insert.
**View**
$postdata = array(
'firstname' => $this->input->post('firstname'), //1st field
'lastname' => $this->input->post('lastname'), // 2nd field
'age' => $this->input->post('age'),
....
....
'test' => $this->input->post('test') // 120th field.
);
$this->Form_Model->insertdata($postdata);
**Model:**
function insertdata($data = array()) {
$sql_query = $this->db->insert('form_insert', $data);
redirect('Form');
}
My question is Is there any better way to insert. This approach feels bit repetitive.
If you simply want to get an array of all the data submitted, you can do it like this:
$postdata = $this->input->post();
This means, all the data submitted from the form will be there in this array.
And if you want to remove any particular element from this array, you can use unset().
Say for example, you may have named your submit button as "submit_btn" like this:
<input type="submit" name="submit_btn" />
then this value would be there in the above returned array. You can remove it like this:
$postdata = $this->input->post();
unset( $postdata['submit_btn']);
Btw, I have a couple of suggestions. The logic part is done in a Controller(you referred it by mistake as View). A View is simply for the displaying. And the Model is for the database communication.
Also, it would always be better to do some validations on the input that you received from the User through form submissions. We may don't even know what data they are sending!
And move that redirect() you used in the Model to the Controller from where you were trying to call that insertdata() method. In that Model, you just return a value (true or false or maybe something else) and do the business logic inside the Controller
You were kind of mixing up everything. That's why I thought to give you some pointers to help you.
Hope it helps :)
I'm creating a form in moodle using moodleform class. I have many fields in the form. What I'm wanting to do is when the user fills in the first field, I want to get the data that he/she entered, search the relevant DB table for a field that matches that input, then populate the other fields for that record.
Please note that the user hasn't pressed any submit button yet. I've been trying to find a function that gets the entered data but all efforts have been in vain. What I found was a get_data() method but I don't even know how to use that correctly. I've been reading the moodle docs but nothing is helping. I'm not a beginner to coding but neither am I an expert.
Here's a code snippet:
class requestcourse_form extends moodleform
{
function definition()
{
global $CFG, $currentsess, $DB, $USER, $currentrecord;
$mform =& $this->_form; // Don't forget the underscore!
// Form header
$mform->addElement('header', 'mainheader','<span style="font-size:22px">'.get_string('courserequestform','block_usp_mcrs'). '</span>');
// Course Code field.
$coursecodearray = array();
$coursecodearray[0] = get_string('choosecoursecode', 'block_usp_mcrs');
$allcoursecodes = $DB->get_records_select('block_usp_mcrs_courses', 'id > 0', array(), 'id', 'id, course_code');
foreach ($allcoursecodes as $id => $coursecodeobject) {
$coursecodearray[$id] = $coursecodeobject->course_code;
}
$coursecode = $mform->addElement('select', 'coursecode', get_string('coursecode', 'block_usp_mcrs'), $coursecodearray);
$mform->addRule('coursecode', get_string('required'), 'required', null, 'client');
$mform->setType('coursecode', PARAM_RAW);
// Course Name field. TODO: Course Name to pick automatically after entering Course Code
$coursenamearray = array();
$coursenamearray[0] = get_string('choosecoursename', 'block_usp_mcrs');
$allcoursenames = $DB->get_records_select('block_usp_mcrs_courses', 'id > 0', array(), 'id', 'id, course_name');
foreach ($allcoursenames as $id => $coursenameobject) {
$coursenamearray[$id] = $coursenameobject->course_name;
}
$mform->addElement('select', 'coursename', get_string('coursename', 'block_usp_mcrs'), $coursenamearray);
$mform->addRule('coursename', get_string('required'), 'required', null, 'client');
$mform->setType('coursename', PARAM_RAW);
Any help would be appreciated.
You have to achieve this using javascript, as Moodle has no functionality to fill data in a nested way.
Add AMD module js file where you are calling your mform for display purpose.
In the file where you render your mform
$mform->render();
add below line to call you amd js file.
$PAGE->requires->js_call_amd('local_acestructure/registration', 'init');
In your amd js file make httprequest / ajax call to fetch data based on your course_code select drop down change.
Thank you.
I followed the post on setting up an editable field in a grid here http://webtips.krajee.com/setup-editable-column-grid-view-manipulate-records/
Everything seems to work - I click on the field, change it's value and it shows the updated value in the grid. However - next time I refresh, the value reverts to the original value.
I've done a little debugging, and it would appear that the save in the controller is failing.
My table has a two key fields (key1 and key2) and a value field. I am trying to update this value field.
My view code is as follows:
$gridColumns = [
'AnotherField',
[
'class' => 'kartik\grid\EditableColumn',
'attribute'=>'value',
'label' => 'some value',
'editableOptions'=> [
'header' => 'profile',
'format' => Editable::FORMAT_BUTTON,
]
];
...
...
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => $gridColumns
]) ?>
and the controller code is as follows:
<snip>
//default code from gii-generated controller
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
// orig post said to use this: $dataProvider = $searchModel->search(Yii::$app->request->getQueryParams());
// Validate if there is an editable input saved via AJAX
if (Yii::$app->request->post('hasEditable')) {
$model = new MyTable;
$post = [];
$posted = current($_POST['MyTable']);
$post['MyTable'] = $posted;
// Load model like any single model validation
if ($model->load($post)) {
// When doing $result = $model->save(); I get a return value of false
$model->save();
$out = Json::encode(['output'=>$output, 'message'=>'']);
}
// Return AJAX JSON encoded response and exit
echo $out;
return;
}
// Non-AJAX - render the grid by default
</snip>
How does the controller know which record to update as I am only passing in the 'value' field, not the keys?
Do I somehow need to add these key1 and key2 fields to the view within the editable widget in order to get these values passed to the controller (so that the load and save know which record to update) or is there some other way that the controller handles this?
In the example on this page http://demos.krajee.com/grid-demo (you can see the column definition too) you can see with firebug that the following values are sent:
Book[5][buy_amount] 1
_csrf MEQ0NnEzNjFhd2dBB2wBCHN0cGVCA3lfZHF1TDReRlAAcQNUPUFjBQ==
editableIndex 5
editableKey 6
hasEditable 1
I believe the 5 in Book[5] is probably matching the index of the record.
This part does that from what I see
$post = [];
$posted = current($_POST['Book']);
$post['Book'] = $posted;
I have never used the widget so I might be wrong.
I have found the solution with some experimentation. I had to unserialize the $editableKey value and extract my key field values from it. I then call 'findModel' to load in the record to be updated.
The updated controller code is as follows:
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
// Validate if there is an editable input saved via AJAX
if (Yii::$app->request->post('hasEditable')) {
$keys = unserialize(Yii::$app->request->post('editableKey'));
$model = $this->findModel($keys['key1'], $keys['key2']);
// Store a default JSON response as desired by editable
$out = Json::encode(['output'=>'', 'message'=>'']);
// Fetch the first entry in posted data (there should only be one
// entry anyway in this array for an editable submission)
$values = current($_POST['MyTable']);
// Load model like any single model validation
if ($model->key1) {
// Update the Profile with the new value passed in
$model->value = $values['value'];
$model->save();
...
Thanks for the advice
I was also looking for a solution of updating table using Editable column. Looking at the ajax post I got similar like this output:
Book[5][buy_amount] 1
_csrf MEQ0NnEzNjFhd2dBB2wBCHN0cGVCA3lfZHF1TDReRlAAcQNUPUFjBQ==
editableIndex 5
editableKey 6
hasEditable 1
Here I found the value of editableKey is the value of id of the table.
So to update specific row on gridview column I used the editableKey and update based on it.
In controller :
if (Yii::$app->request->post('hasEditable')) {
$_id=$_POST['editableKey'];
$model = $this->findModel($_id);
$post = [];
$posted = current($_POST['MyTable']);
$post['MyTable'] = $posted;
// Load model like any single model validation
if ($model->load($post)) {
// When doing $result = $model->save(); I get a return value of false
$model->save();
if (isset($posted['buy_amount']))
{
$output = $model->buy_amount;
}
$out = Json::encode(['output'=>$output, 'message'=>'']);
}
// Return AJAX JSON encoded response and exit
echo $out;
return;
}
I just get the post data using $_id=$_POST['editableKey'];
and set my $model as $model = $this->findModel($_id);
now you can update multiple editable column just adding condition like this
if (isset($posted['buy_amount']))
{
$output = $model->buy_amount;
}
Thanks!
i am using zend forms in my zf2 application which has two text boxes. Once i submit the data, i take that and i filter the data array which i use to display it on the table. Once the data is filtered and displayed on the table, the form data is reset.
Is there a way to retain the values in the form even after submission ?
Thats the code i have, on a button click i get into this page and the action is as shown below. So this Page has a table which will get populated based on the two text boxes that i have. When i click the submit button, the data in the textboxes disappear and the data gets populated in the table.
So what i want to do here is, retain the entered values in the textboxes. Can i do that ? I know HTTP is a stateless protocol. I know i can do it using front end client side technologies, just wanted to know if there is a work around to do this.
public function searchAction()
{
//search form instantiation
$form = new SampleForm();
// get the post request
$request = $this->getRequest();
//instantiation of select for querying
$select = new Select();
// get parameters
$artist = $request->getPost('artist');
$title = $request->getPost('title');
//prepare search criteria array
$search = array('artist' => $artist, 'title' => $title);
//remove empty criteria from the search criteria list
foreach ($search as $key => $value) {
if (empty($value)) {
unset($search[$key]);
}
}
$result = $this->getAlbumTable()->fetchAll($select->where($search));
return array(
'albums' => $result,
'form' => $form,
);
}
I finally figured out what i was doing. I just had to do this.
if ($request->isPost()) {
$album = new Album();
$form->setInputFilter($album->getInputFilter());
$form->setData($request->getPost());
}
I'm trying to pull data from a table, populate a select input with that data, then post that information (and other info) to another table. When the view is rendered, it correctly populates the select with the data required, however when I submit the form, I receive an undefined variable error.
Undefined variable: secondarygenre
View
{{ Form::select('genre', $secondarygenre, null, array('class' => 'form-control', 'id' => 'genre')) }}
Controller
//Data is passed to the form in the view
public function addSubmission() {
$secondarygenre = Genre::lists('friendlyName', 'id');
return View::make('add')
->with('secondarygenre', $secondarygenre);
}
//Form is submitted
public function successfulSubmission() {
$track = new Track();
$track->genre_id = Input::get('genre');
$track->save();
}
If it's populating the select input with data, I know that it's the variable is not undefined.
I apologise if I've missed something, this is my first project with Laravel (or any MVC framework).
Any help would be greatly appreciated!
In the post, you have to also return the view with the variable, it looks like you're only doing that with the get, but you need to use ->with('secondarygenre', $sg) once for each type of request.