so I've been trying to get select2Row to work for me forever and I just can't seem to get a hang of it. What I'm trying to do is provide the user with Tags that list a school/university name, while at the same time storing the value of said tag when I save the form. I've noticed that I cannot use both data and tags, else my drop down wont populate so that's not an option. Tags only seem to want text, rather than text and matching values. Any ideas?
<div class="row">
<?php
echo $form->select2Row($model, 'school_id', array(
'asDropDownList'=>false,
'options'=>array(
'tags'=>School::model()->schoolNames,
//'maximumSelectionSize'=>1,
'width'=>'297px',
'tokenSeparators'=>array(','),
),
));
?>
<?php echo $form->error($model,'school_id'); ?>
</div>
And here is the function for schoolNames
public function getSchoolNames()
{
$schools = array();
$result = $this->findAllBySQL("SELECT DISTINCT name FROM School");
foreach($result as $school){
$schools[] = $school->name;
}
return $schools;
}
I've tried using this instead, but the tags won't populate
public function getSchools()
{
$query = "SELECT id,name FROM School";
$results = $this->findAllBySQL($query);
return CHtml::listData($results, 'id', 'name');
}
So at the moment, select2Row is generating a list of tags using getSchoolNames() and that all works fine, except once you submit the form using those tags you'll get this
Please fix the following input errors:
School must be an integer.
Sorry for all the trouble, I'm still a little new to this. I'm sure the answer is probably right in front of me. Thanks either way =)
try this:
echo $form1->select2Row($model, 'school_id', array(
'data' => School::model()->schoolNames,
'multiple' => 'multiple',
'options' => array(
"width" => '70%',
),
));
Related
I try to create dropdown-list with Yii, using listData and activeDropDownList.
I use the examples found on the web, but it refuses to create the optgroups for me.
$data = CHtml::listData(MyModel::model()->getEntries(0), 'id', 'text', 'group');
Generates an array as expected:
Array([group1] => Array([10]=>FirstEntry, [20]=>SecondEntry),
[group2]=>Array([30]=>firstEntryGroup2, [40]=>firstEntryGroup2))
And so on. So it's an associative array filled with sub-arrays...
But when I use
echo CHtml::activeDropDownList($model, 'dropdownName', $data);
All I get is a flat dropdown without the optgroups. Just the entries from the sub-arrays...
Yii 1.1.6 (I read something about safe-attributes and tried to implement it, but with no success...
The old answer below is incorrect. Sorry. You create optgroups using a 'group' attribute in your drop down data array.
array(
array('id'=>256,'text'=>'TV','group'=>'Electrical'),
array('id'=>257,'text'=>'Radio','group'=>'Electrical'),
);
http://www.yiiframework.com/forum/index.php/topic/6903-how-can-i-generate-a-select-list-with-optgroup/
Old answer:
There is a great gist here that shows how do create what you're after: https://gist.github.com/StandardNerd/2909111
<?php echo CHtml::dropDownList('Cars', 'car_id', array(
'Mazda'=>array(
'mazda-rx7'=>'RX7',
'mazda-rx5'=>'RX5',
),
'Volvo'=>array(
'volvo-b9tl'=>'B9TL',
'volvo-l90e-radlader'=>'L90E Radlader',
),
)); ?>
You're currently using an activeDropDownList which should only be different because you add your $model variable instead of 'Cars' and tweak the function
One of solutions is anonymous function.
$data = CHtml::listData(
MyModel::model()->getEntries(0),
'id',
'text',
function(MyModel $aMyModelInstance){
return $aMyModelInstance->getLocalizedGroupNameForThisInstance();
});
I am starting to learn PHP framework, particulary cakephp. I know how to insert into database, but I am curious about displaying the entries on the same page. I don't want to reload or redirect the page just to display the results.
I have only three four fields in the database. id, name, email, and the date. I have used the same validation in the model page.
Controllers:
<?php
function index(){
if($this->request->is('Post')){
$this->Save->save(data);
}
//display the entries from the database
$this->set('saves', $this->Save->find('all'));
}
?>
Index.ctp:
<?php
echo $this->Form->create('Save');
echo $this->Form->input('name', array('class'=>'name', 'required' => true));
echo $this->Form->input('email', array('class'=>'email', 'required' => true));
echo $this->Form->input('message', array( 'required' => true));
echo $this->Form->submit('submit', array('formnovalidate' => true));
//i want to display the entries here
foreach($saves as $row){
echo $row['Save']['name'];
echo $row['Save']['comment'];
}
?>
The problem is that , it affects the textarea. It reduces the size to half. Need help. thanks a lot. I am new to cakephp and I have been googling about it, but have not find the any results. Thanks
From what I understand it seems that your problem is more CSS issue than CakePHP issue. I don't think printing the data will cut the field into half.
Try put after echo $this->Form->submit('submit', array('formnovalidate' => true)); because the submit method does not include afterwards.
You can this $this->Form->end if you don't want to add the form closing tag manually.
I am trying to build in a "search" box on a results page in my cakephp app. The page uses the cakePHP pagination component to show and "page" results. This is perfect, but I am having difficulties to get the next part to work.
The desired outcome:
A cakephp form (post) with an input box and a couple of select boxes, including a date selector so that I can select between dates. The user should be able to populate these fields and submit
On submit, the user selection should change the cakePHP pagination conditions in the controller
In the view I want the pagination bar to keep record of the user selection, so that when I filter through different pages, it keeps the users search. I understand this can be achieved using $this->passedArgs, hence why I am using post and not get.
The code:
// Form:
<?php
echo $this->Form->create('search', array('class' => false));
echo $this->Form->input('searchFor');
echo $this->Form->input('dateFrom');
echo $this->Form->input('dateTo');
echo $this->Form->end();
?>
// Controller:
if($this->request->is("post")) {
$filters = $this->request->data["search"];
$this->passedArgs["searchFor"] = $filters["searchFor"];
$this->passedArgs["dateFrom"] = $filters["dateFrom"]." 00:00:00";
$this->passedArgs["dateTo"] = $filters["dateTo"]." 00:00:00";
// Assign search parameters:
if($this->passedArgs["searchFor"] != "") {
$conditions["Model.field LIKE"] = "%".$this->passedArgs["searchFor"]."%";
}
$conditions["Model.created >="] = $this->passedArgs["dateFrom"];
$conditions["Model.created <="] = $this->passedArgs["dateTo"];
} else {
$conditions = array("Result.status_id >=" => 12);
}
$this->paginate = array(
'conditions' => $conditions,
'order' => array('Result.created ASC'),
'limit' => 20
);
$this->set("results",$this->paginate("Model");
// The view file:
<?php
$this->Paginator->options(array('url' => $this->passedArgs));
?>
Where I am now:
The initial page loads with all of the results
When I populate the search boxes it does return my results
The problem:
I am convinced the way I am doing it is incorrect as I now need to do 2 checks, a) being if results has been posted and b) check if there is passedArgs available. I am 100% convinced this is not the right way of doing it.
Let's say I have 2 free form fields for search, say name and surname, if I leave surname blank my url would be written as below, and this does not look or appear to be correct. That means I have to assign default values to ensure the items below does not happen, which does not appear to be very dynamic.
http://localhost/site/controller/action/surname:0/name:John/date:0/
On refresh it says the page does not exist because the posted values is not available anylonger.
usually I proceed like this in the controller:
//transform POST into GET
if($this->request->is("post")) {
$url = array('action'=>'index');
$filters = array();
if(isset($this->data['searchFor']) && $this->data['searchFor']){
//maybe clean up user input here??? or urlencode??
$filters['searchFor'] = $this->data['searchFor'];
}
//redirect user to the index page including the selected filters
$this->redirect(array_merge($url,$filters));
}
$conditions = array();
//check filters on passedArgs
if(isset($this->passedArgs["searchFor"])){
$conditions["Model.field LIKE"] = "%".$this->passedArgs["searchFor"]."%";
}
//paginate as normal
$this->paginate = array(
'conditions' => $conditions,
'order' => array('Result.created ASC'),
'limit' => 20
);
The idea is to transform the POST sent by your form into GET. so you wont have problems with the paginator nor the refresh
Hope this helps
What you want can be done a lot more simple and DRY by using this search plugin.
It automates what you want more or less plus it already can do more than your code.
So I suggest you to use the plugin directly or take a look at it how it does the trick. :)
The Zend Form is proving to be a bit tricky for me, even as much as I am working with it lately...
I have this form and I am attempting to dynamically create the several checkboxes for it. It is working out alright, except I cannot seem to get the 'value' attribute to change.
In my Zend form class I have this snippet...
// psychotic symptoms
$this->addElement('checkbox', 'psychoticsymptom', array(
'label' => 'psychoticsymptom',
'name' => 'psychoticsymptom',
));
In my view (phtml) I am calling it like this...
<div class="element">
<?php // Psychotic Symptoms
$Criteria = new Criteria();
$Criteria->add( DictionaryPeer::CATEGORY, 'MAR: Psychotic Symptoms' );
$Criteria->addAscendingOrderByColumn( 'Ordinal' );
$this->PsychoticSymptomsList = DictionaryPeer::doSelect( $Criteria );
foreach( $this->PsychoticSymptomsList as $Symptom ) {
$form->psychoticsymptom->setValue($Symptom->getDictionaryId());
$form->psychoticsymptom->setAttrib('name', $Symptom->getWord());
echo $Symptom->getDictionaryId(); // prove my id is coming through... (it is)
$form->psychoticsymptom->getDecorator('label')->setTag(null);
echo $form->psychoticsymptom->renderViewHelper();
$form->psychoticsymptom->setLabel($Symptom->getWord());
echo $form->psychoticsymptom->renderLabel();
echo '<br />';
}
?>
</div>
Everything seems to be working fine, except the value attribute on each checkbox is rendering a value of '1'. I have tried moving the 'setValue' line to several different positions, as to set the value before the form element renders but I am having no luck getting this to work. It's worth any effort to me because I need to do the same type of operation in many areas of my application. I would have done this a bit different too, but I am re-factoring another application and am trying to keep some things unchanged (like the database for instance).
Any help is much appriciated
Thanks
you can try to overwrite the "checkedValue" and "uncheckedValue". check this reference
$this->addElement('checkbox', 'psychoticsymptom', array(
'label' => 'psychoticsymptom',
'name' => 'psychoticsymptom',
'checkedValue' => 'checked Value',
'uncheckedValue' => 'unchecked Value'
));
You seem to only have one psychoticsymptom element "checkbox" which your adding (changing) the value too for each $this->PsychoticSymptomsList.
Maybe you would be better off using a multicheckbox element instead.
First of all I have a Model user.php which connects to users table.
I have a controller UsersController.
I created a view for search: (filename: index.ctp)
<p><?php
echo $this->Form->create("Users", array('action' => 'search'));
echo $this->Form->input("Search Label", array('action' => 'search', 'name' => 'txt_search'));
echo $this->Form->end("Search");
?></p>
And this will go to UsersController/search() function
function search() {
if (!empty($this->data))
{
$name = $this->data['Users']['txt_search'];
$conditions = array("User.name Like " => "%$name%");
$result = $this->User->find('all', array('conditions'=> $conditions));
$this->set('users', $result);
}
}
And this will load search.ctp
My problem is, when I use the variable $users in search.ctp, it gives me an error Undefined variable: Users [APP\views\users\search.ctp, line 10].
I don't understand.
Please help. Thanks!
You're specifying a custom name for your input and then you're checking $this->data which will be empty because you're input is not named properly (and does not get auto populated in $this->data). Use the following.
echo $this->Form->input("txt_search", array('label' => 'Search Label'));
A couple of things you should be looking at.
Set a default value to your users variable so your page doesn't break if they request it directly. Have $result = array(); at the top and do an empty() check on it in search.ctp
Why have you specified an action attribute in your input? You don't need that.