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)
Related
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);
I am trying to validate a form field in laravel mb_plans based on 2 others form fields activity and options, if both activity and options have there respective value then validate mb_plans
I've tried required_if and required_with, but can't able to use AND condition.
'mb_plans' => 'required_if:activity,MB|required_with:options,0',
if you want to validate when activity value is MB and options is 0 then use :
'mb_plans' => 'required_if:activity,==,MB|required_if:options,==,0',
try below:
'mb_plans' => 'required_with_all:activity,options',
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.
I've got a hidden field inside a form, which I create using Drupal's form API like this:
$form['position'] = array(
'#type' => 'hidden',
'#default_value' => '57.149953,-2.104053',
'#attributes' => array(
'id' => 'geoposition',
)
);
When I load the page in which the form is rendered, I have a little bit of JavaScript that edits the hidden field like so:
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(getPosition, noPosition, 10);
}
function getPosition(position) {
var pos = document.getElementsByName("position");
pos.value = position.coords.latitude + "," + position.coords.longitude;
alert(pos.value);
}
Now, the last statement in this snippet actually outputs the correct value of the user's current position. However, when I submit the form, the value passed onto the server for manipulation is the default value, and not the updated value. The value to use after the form has been submitted is picked up like this:
$map_field .= 'src="http://maps.google.co.uk/maps?q='.$form_state['values']['position'].'&output=embed"></iframe>';
Any ideas?
Thanks,
This is what I wud do:
1) Disable to js for now.
2) Use xdebug to see the value before and after the form has been submitted. This should help to see if something is going wrong.
Cheers,
vishal
Rather than using #default_value, consider using #value - because that is the value that gets posted.
Eventually solved it by using the getElementById() method instead of getElementsByName() method. For some reason the name wasn't working. Thanks all for your help.
This is a CakePHP / General PHP question.
In my application I use a query string like /login?continue=/admin/posts
This query string is used to redirect users to the URL in the query, but it doesn't work so it seems as though the app can't see the string...
This has got me wondering as basically when you arrive at the page with the string it's a GET request where as when you login, it becomes a POST or XML request (if using AJAX). Do I need to add the query string manually to the form for the POST to see it?
Either in the form action or a hidden input? Or am I barking up the wrong tree?
I'm currently grabbing the query like so:
if(isset($this->params['url']['continue']))
{
$pathtoredirect = $this->params['url']['continue'];
}
else
{
$pathtoredirect = $this->Auth->redirect();
}
But that's within the POST request so perhaps the query is lost... and adding it to a hidden input would not solve the problem with the current code so I would either change the code to look at the hidden field or pass the query with the action on the form?
e.g. <form action="/login?continue=/admin/posts" method="post">
Am I correct in thinking this? And would anyone be able to offer solutions or pros and cons of the two methods I mention?
In short I'm asking how to add the query string to my form action value
It currently looks like:
php echo $this->Form->create('User',
array(
'id' => 'loginform',
'type' => 'post',
'url' => array
(
'admin'=>false,
'controller' => 'users',
'action' => 'login'
)
)
);
So how would I add the query string to the form?
Thanks
When receiving a POST request you can receive both POST and GET variables through the superglobals $_POST and $_GET.
You can either send your paramater in $_GET by including it in the form's action attribute or send it in $_POST by creating an <input type="hidden"> tag within the form
A slash (/) is a reserved character. Encode it with %2F.
http://blooberry.com/indexdot/html/topics/urlencoding.htm#whatwhy
The solution was to do this:
<?php echo $this->Form->create('User',
array('id' => 'loginform', 'type' => 'post',
'url' => array('admin'=>false,'controller'=>'users','action'=>'login','url'=>$this->params['url']['continue']))); ?>
as I have a route already setup to handle the additional URL parameter:
Router::connect('/auth/login', array('controller'=>'users','action'=>'login'));
Router::connect('/auth/login?continue=:url',
array('controller'=>'users','action'=>'login'),
array(
'url' => '[A-Za-z0-9/\._-]+',
'pass' => array('url')
));
If anyone sees any issues with the way I do this though, please feel free to comment.