yii2: how to send a model from a create form to the controller - forms

I have a dropdownlist in my form:
<?= Html::activeDropDownList($model, 'Id_tipoprograma',
ArrayHelper::map(Tipoprograma::find()->all(), 'Id_tipoprograma', 'Id_tipoprograma', 'Descripcion'))?>
i want to receive "Id_tipoprograma" in the controller, i tried with:
$idtp = $model->Id_tipoprograma;
But doesn't work. thanks.

First read docs here and here. Normally you should get form data in Yii::$app->request->post() or Yii::$app->request->get()

Related

Custom Form in Custom Joomla Component

I have a basic front-end form within a component that I created using ComponentCreator.
How and where do I direct the form "action"? Where can I handle this so that I am following the MVC design pattern? My form is within a view (default.php)
The form should simply insert to the database upon submission. I don't want to use a form building extension, I have tried these and they don't meet my requirements.
What version of Joomla? Assuming your not using AJAX then just direct to your controller.
<form id='MyForm' method='post' action='index.php?option=com_mycomponent&task=myoperation'>
<input type='text' name='myname'/>
...</form>
Then you will have a function in your controller (controller.php) called myoperation that will process all the things in your form (not forgetting the token of course)
JSession::checkToken('request') or jexit( JText::_( 'JINVALID_TOKEN' ) );
$jinput = JFactory::getApplication()->input;
$add_name = $jinput->get('myname', '', 'STRING');
//whatever you need to do...
//add to model if you wanted to add name to DB
$model = &$this->getModel();
$model->updateDB(...);
Then create function updateDB in model.
This is a rough example with Joomla 2.5 but should work with 3.x. Hope this helps.

Cakephp Controller isn´t processing form data

Hi I´m new with cakephp (v.1.3). I´m trying to do something simple.
I have two tables: fichas[id,... etc] and labos[id,laboratorio,ficha_id] so "labos" belongs to "fichas". (labos.laboratorio is ENUM field).
I would like to view a "ficha" given labos.id and labos.laboratorio so I´ve included the following code in "home.ctp"
<h3>Mostrar Ficha</h3>
<?php echo $this->Form->create('ficha',array('action'=>'localiza'));?>
<?php echo $this->Form->radio('laboratorio',array('A','B','C'),array('A','B','C')); ?>
<?php echo $this->Form->input('id',array('label'=>'Numero','type'=>'text')); ?>
<?php echo $this->Form->end("Mostrar");?>
Then in "fichas_controller.php" added the following:
function localiza(){
$laboratorio=$this->data['Ficha']['laboratorio'];
$id=$this->data['Ficha']['id'];
if(!$id){
$this->Session->setFlash('Por favor introduzca un valor valido');
$this->redirect(array('action'=>'index'));
}
$this->set('fichas',$this->Ficha->findID($id,$laboratorio));
}
Finally in the model "ficha.php" the following:
function findID($id=null,$laboratorio=null){
return $this->find('all',array('conditions'=>array('Labo.laboratorio'=>$laboratorio,'Labo.id'=>$id)));
}
Obviously the file views/fichas/localiza.ctp exists
The thing is when I press the submit button in the form it just reloads the home.ctp page. Looks like the controller´s code is not being executed because i´ve tried to force the error message that should load the index action changing the if condition to true but the same result. I´ve changed the name of the function in the model expecting an error to ocurr but I get the same result.
I have another two forms in the home.ctp page but calling another actions and models.
One of them its almost identical and it works fine.
I can´t figure out the error.
Thanks in advance for any help.
Marcelo.
The array key $this->data['Ficha'] likely doesn't exist. You've created the lowercase "ficha" form, this name should be capitalized, otherwise the data is available in $this->data['ficha']. So the form creation call would look like this:
<?php echo $this->Form->create('Ficha',array('action'=>'localiza'));?>
You can debug in two ways on Cake
Configure::write('debug', 2);
debug($this->data);
OR
The other PHP ways
print_r($this->data);
This way, you will know if you are passing the data->params properly.
Why is your model has first charcter in lower-case? It should be
Fichas
Labos
Then you can issue a direct find on the controller, if you only want.
$d = $this->Fichas->find('all', array();
You might have added the home.ctp file into another controller. Try adding the controller in the following line:
<?php echo $this->Form->create('ficha',array('controller' => 'fichas', 'action'=>'localiza'));?>
Hope it helps.

Zend Framework multiplay forms

I want get the follow thing working.
I've a page with a couple of text articles, each article has his own 'id' in the database. Below every article I want to make it possible to discuss about it. So I setup a discuss form witch I print with my article trough a 'foreach'.
In the form I added a Zend_Form_Element_Hidden. In the view I want to set the value of the hidden field with 'article_id', this likes me the best way to put it in the database?
In the foreach I try the follow thing but when I do this, the form is gone and I only get the element where I add the value.
My code in the view:
foreach ($this->paginator as $article):
echo $this->form->getElement('article')->setValue($article['id']);
endforeach;
I hope some one can make this a bit more clear for me :)
With kind regards,
Nicky
I am guessing you want to print the form inside the loop but only the element is being printed.
If that is your problem, the reason is because setValue() returns the element and not the form.
// Your Code
// This will only print the element and not the entire form
echo $this->form->getElement('article')->setValue($article['id']);
You will have to change your code to:
// Set the element value first
$this->form->getElement('article')->setValue($article['id']);
// Then render the form
echo $this->form;

ASP.NET MVC 2: Update Partial?

Is it possible to call a controller action that will update (refresh) a partial within the View with the updated model? If so, can someone point me to an example?
I'm making an ajax call. The call sends some json to the controller. The controller extracts the json and formats it into XML that then get's passed on to a SPROC. The results of the SPROC update the model. This is where I need to update the view... with the latest model results.
Yes, you simply need to have the action return the PartialView with its updated model. The calling code might look like this:
<div id="MyDiv"></div>
<%=Ajax.ActionLink("Update", "GetUpdatedPartialView",
new AjaxOptions{UpdateTargetId = "MyDiv"}) %>
When you click on the link, the HTML returned by your action will get placed into the div with ID "MyDiv".
Edit
I don't have my code with me, but if I remember right it's something like this:
var url = '<%=Url.Action("GetUpdatedPartialView")%>';
$.post(url, function(data) {$('#MyDiv').html(data);});
In the controller, you can just do something like:
if (Request.IsAjaxRequest()) {
return View(name_of_partial, updated_model);
}
On the front end, it's just a jQuery load, something like:
$("#target-div").load(url_of_action, data_to_send);

dojo.data is undefined Flitering select

I am having a few problems with Dojo Filtering Selects when using the Zend Framework Forms and need some help to find out what I have missed as this is driving me mad.
I am currently getting this errors in firebug:
dojo.data is undefined
dojo.data.ItemFileReadStore is not a constructor
Below is the code that I am using to create the filter select and provide the json data to the calling controller.
Zend_Form Element (Dojo Enabled)
$industry = new Zend_Dojo_Form_Element_FilteringSelect('industry');
$industry->setAutocomplete(true)
->setStoreId('industrystore')
->setStoreType('dojo.data.ItemFileReadStore')
->setStoreParams(array('url' => $baseUrl.'/dojo/industry'))
->setAttrib("searchAttr", "title")
->setRequired(true)
->removeDecorator('DtDdWrapper')
->removeDecorator('label')
->removeDecorator('HtmlTag');
Dojo Controller
public function industryAction(){
$db = Zend_Db::factory($this->config->database);
$result = $db->fetchAll("SELECT * FROM industries");
$data = new Zend_Dojo_Data('industryid', $result);
$this->_helper->autoCompleteDojo($data);
$db->closeConnection();
}
The annoying thing is all my other Dojo elements on this form and other forms work well it is just whenever I do Filtering Selects that I hit these problems, and this problem causes all the other elements in a form to fail too.
Thanks in advance.
The problem is actually with how Zend Framework initializes the dijits and data stores before the toolkit is fully loaded, in this case specifically the methods assigning the store to the dijit. I ran into this issue as well and found the best way to work around the issues was to either pass the data store from the controller to a JavaScript variable defined in the view or do what your did with a specific autocomplete action. Based on your example I would make the following changes.
In your form I would simplify the element:
$industry = new Zend_Dojo_Form_Element_FilteringSelect('industry');
$industry->setAutocomplete(true)
->setRequired(true)
->removeDecorator('DtDdWrapper')
->removeDecorator('label')
->removeDecorator('HtmlTag');
In your view you want to connect the store to your dijit and make sure that you have loaded the dojo.data.ItemFileReadStore module:
<?php $this->dojo()->onLoadCaptureStart()?>
function(){
dijit.byId('industry').store = new dojo.data.ItemFileReadStore({ url: '/controller/industry' });
}
<?php
$this->dojo()->onLoadCaptureEnd();
$this->dojo()->requireModule('dojo.data.ItemFileReadStore');
?>
As I mentioned I ran into a similar issue which I answered here . Another issue I discovered is that the data store does not like dealing with labels declared anything other than "name" for the label declaration in the Zend_Dojo_Data.