parameter value getting lost in moodle - php

I have a page in my moodle block which has some parameters passed in the url.This is what it looks like: http://localhost/blocks/learning_strategizer/viewlp.php?lp_id=1-2
This page(viewlp.php) is calling a form with parameters from lp_id:
$customdata=array(substr($lp_id,2));
$form = new viewlpstudent_form(null,$customdata);
This form is pulling out data based on the lp_id and taking in user choices. It has an action button obviously in the end.
When I click the action button, I need to fetch the user choices from the form as well as the URL parameter that I sent to the form (substr($lp_id,2))
But problem is: when I click the action button the parameter is lost and becoming null.
Is there anyway to fix this.

You need to create a hidden field in the form that will contain the lp_id parameter. So inside the definition method of the form add the following:
$_form->addElement('hidden', 'lp_id', $this->_customdata['lp_id']);
$_form->setType('lp_id', PARAM_INT); // or choose another PARAM_XXX type if not integer
And $customdata array should be associative to be able to properly get the parameter:
$customdata = array('lp_id' => substr($lp_id,2));
$form = new viewlpstudent_form(null, $customdata);

Related

Yii2 / Kartik Select 2 Tags - form field submitting data in extra array brackets

Hopefully this makes sense to someone.
I'm using Kartik-V Select2 widgets in a ActiveForm, with 'multiple' and 'tags' enabled. I'm using this elsewhere in my application and it is working fine there, and on submission the array data is sent as I would expect in the appropriate form field. From there I can easily implode the array and save to database in the controller action.
However in a different section of my app, with exactly the same configuration in the view, model and controller, the array is being posted as an additional form field (as can be seen watching the post event in the firefox console).
What I'm seeing in the firefox console is:
-----------------------------366976194315951562394252057249
Content-Disposition: form-data; name="Notes[notes_to]"
-----------------------------366976194315951562394252057249
Content-Disposition: form-data; name="Notes[notes_to][]"
admin
-----------------------------366976194315951562394252057249
Content-Disposition: form-data; name="Notes[notes_to][]"
kennettm
So where I would expect to receive the array in Notes[notes_to], it is actually coming through in Notes[notes_to][] but I can't for the life of me figure out how to access and handle these values in my controller, or how to get the array of tags submitted in Notes[notes_to] where I want it.
Thanks in advance.
***** EDIT *******
Thanks #Muhammed. I am expecting to received an array ['admin','kennettm'] (as with other areas of my app where I've used this setup), however in this instance that is not what is happening.
I've got the model and CRUD generated by Gii for the Notes section, so it does have its own MVC setup however the notes _form file in this case is rendered on the site index, so in this case the model is being loaded in the Site controller and is handled there. I can confirm that the same issue is occurring regardless of where I render the _form file, be it in the index or in the Notes section view.
I would expect that the data be returned in $notesmodel->notes_to as an array as mentioned above, which is then easily imploded and saved to db. However the data is not there on form submission, and "Public" is being saved every time. For the sake of testing, I removed the if statement, set the controller to implode the array and ensure that I add tags (so the array is sure to be there), however then I am receiving an error exception "implode(): Argument must be an array".
Snippet of my intended controller code is below. The controller checks to see if any tags have been added to the field. If tags are present, there should be an array which can be imploded and saved. Otherwise if no tags are present, it will revert back to a simple string "Public' and save that instead.
$notesmodel = new Notes();
if ($notesmodel->load(Yii::$app->request->post())) {
$notesmodel->notes_user = Yii::$app->user->identity->username;
$notesmodel->notes_created = date("Y-m-d H:i:s");
if (!empty($notesmodel->notes_to)) {
$notesmodel->notes_to = implode($notesmodel->notes_to);
} else {
$notesmodel->notes_to = 'Public';
}

Onchange in Symfony2 form

This is my input type
->add(‘year’, ChoiceType::CLASS, array(‘choices’ => $array, ‘attr’ => array(‘onchange’ => ‘this.form.submit()’)));
Onchange page is reloading and data is submitted. Then in controller I can access value like this:
$_POST[‘year’].
The thing is I would like to get $_POST in symfony’s way:
$form[‘year’]->getData();
I don’t know why only $_POST[‘year’] works and no result with $form[‘year’]->getData().
You can use for POST request :
$request->request->get('year');
For GET request:
$request->query->get('year');
For FILE queries:
$request->files.
You can get a single item from the form data like;
$year = $form->get('year')->getData();
In this example 'year' is the name given to the field you are asking for (as per your form builder)

Laravel 5 user defined settings only needs to be called once

For my personal website (online administration) I have an appointments page, with certain settings, which is just for me, so I don't need it to be secure.
For example, I can change my view (to show all appointments or to sort them by label, or status). I can also exclude a status to make sure it's not being shown. Everything works, no problem there.
My issue is this. I have a simple field in my user database called "view". When I go to my appointments page, I check the value of my "view" field and if it is "status" for example, in my controller I set "$view = status", to return my "status" view. This works, with the following simple check:
$getUserView = \Auth::user()->view;
if($getUserView){
$view = $getUserView;
}
In my view itself I have a dropdown to change the view. Now, when I go to my view, it shows the "status view" just fine. But when I want to change the view to "default" or "label" using my dropdown, it should change the view to what I selected. So basically what I want to achieve is, when I go to my appointments page for the first time, it should show the view that I have set in my database, but only that one time. I could set it in a session maybe for that but I am just not sure how to accomplish this. Any pointers would be helpful!
Edit:
Still struggling with this, because I am using GET for everything, also the dropdown. Example, when I change the value in the dropdown, a javascript simply calls the URL again, but with the status that was selected in the dropdown. So, for example, my default URL is the following:
http://example.com/appointments/status/default
Now, I select "completed" in the dropdown, the following URL is called:
http://example.com/appointments/status/completed
In my appointments controller I put the following:
$status = session()->get("status", \Auth::user()->status);
In my routes I have the following:
Route::get('appointments/status/{status}', array('as' => 'appointments', 'uses' => 'Appointments\AppointmentsController#index'));
Maybe I should change "{status}" in the route to "$status" and use the put method to set the "$status"? Not sure what the best method would be.
When using the get method on the Session, the second argument is intended for a default value, which will be returned if the session key is not found.
You could do something like this:
$user = \Auth::user();
$view = session()->get("appointments_view", $user->view);
That will get the view set in the session, and if that is not set, it'll return $user->view. Now, when the users picks another view in the dropdown, just do:
session()->put("appointments_view", $dropDownValue);

Yii mass assignment from $_GET not working as expected

I'm trying to perform a mass assignment of 2 variables I'm sending via GET to another model::controller (from project::actionCreate to client::actionCreate)
In the _form view for project::actionCreate I've got the following:
<?php echo " ".Chtml::link('+New client',array('client/create',array('Client' => array('redir'=>Yii::app()->controller->route,'redirId'=>$model->id))));?>
With the goal of creating an array "Client" with attributes "redir" and "redirId".
In client::actionCreate I want to do something like
if(isset($_GET['Client']))
{
$model->attributes=$_GET['Client'];
}
Now I noticed that my $_GET var puts client inside subarray 0, so I've tried this with
$_GET[0]['Client']
as well, but no luck. However if I manually assign the variables like this:
$model->redir = $_GET[0]['Client']['redir'];
$model->redirId = $_GET[0]['Client']['redirId'];
Then it works.
Any idea what is up? The goal is to allow someone to create a new client while creating/updating a project record, by sending them to client::actionCreate, but redirecting them back to their original project::actionCreate if they were linked there from my "+New Client" link.
I think the client array is put inside subarray 0 because you've added an array around the parameters. Try removing the array like the following:
<?php
Chtml::link('+New client',array('client/create', 'Client' => array('redir'=>Yii::app()->controller->route,'redirId'=>$model->id)));
?>
I don't know what your model looks like but if the fields aren't assigned they are probably not safe. You can make them safe by adding them to the rules part of your model. Or you could try the following, by specifying the false parameter it will be possible to assign values to unsafe attributes. (http://www.yiiframework.com/doc/api/1.1/CModel#setAttributes-detail)
$model->setAttributes($_GET['Client'], false);
I am not sure creating a link like you want is possible. I have asked something similar some time ago Yii link with [ as a parameter I just could never get the link to how I wanted it. In the end I just created the link the old fashion way, not using CHTML.

How do I remove unnecessary options from a select field in a Drupal form?

I'm using the better_exposed_filters module to create a set of exposed filters for a view. One of the filters is being displayed as a select field, and I would like the field to only display options that are actually associated with content in the database.
Currently, I am doing this using the hook_form_alter() method. For simplification, in the following example the field is called 'foo' and the content type with that field is called 'bar':
function my_module_form_alter(&$form, $form_state, $form_id) {
// Get all the values of foo that matter
$resource = db_query('select distinct field_foo_value from {content_type_bar}');
$foo = array();
while($row = db_fetch_object($resource)) {
$foo[$row->field_foo_value] = $row->field_foo_value;
}
$form['foo']['#options'] = $foo;
}
This works great -- the form displays only the options I want to display. Unfortunately, the view doesn't actually display anything initially and I also get the following error message:
An illegal choice has been detected. Please contact the site administrator.
After I filter options with the form once, everything seems to work fine.
Does anyone know how I can solve this problem? I'm open to an entirely different way of weeding out filter options, if need be, or a way that I can figure out how to address that error.
Under your view argument there should be a section called "Validator options" with ""Action to take if argument does not validate under it. Depending on what you want shown, you should be able to display all values or display an empty page.
I found a solution that works, but it's somewhat hackish. I force the form to think that it's validated, and it doesn't complain anymore, with the following line at the bottom of the function:
$form['foo']['#validated'] = true;

Categories