Get id of selected values in CAutoComplete in Yii - php

I am using CAutoComplete to select multiple comma separated values. Now I am able to pass only the selected values, I need to pass the id of selected values also when the form is submitted.
Can you please help me to send the id on submit as an array or comma separated.
In my form I am using:
<?php
$this->widget('CAutoComplete', array(
'model' => $model,
'attribute' => 'skills',
'url' => array('serviceRequest/Suggest'),
'multiple' => true,
'htmlOptions' => array('size' => 34),
));
?>
Controller:
public function actionSuggest()
{
if (isset($_GET['q']) && ($keyword = trim($_GET['q'])) !== '')
{
$tags = Skills::model()->suggest($keyword);
if ($tags !== array())
echo implode("\n", $tags);
}
}
Model:
public function suggest($keyword,$limit=20)
{
$tags=$this->findAll(array(
'condition'=>'skills LIKE :keyword',
// 'order'=>'Name',
'limit'=>$limit,
'params'=>array(
':keyword'=>'%'.strtr($keyword,array('%'=>'\%', '_'=>'\_', '\\'=>'\\\\')).'%',
),
));
$names=array();
foreach($tags as $tag)
$names[]=$tag->skills;
return $names;
}

you can pass any option that the JUI autocomplete widget supports by including an options array:
$this->widget('CAutoComplete', array(
// your other settings here
'options' => array(
'select' => new CJavaScriptExpression('function(e, ui) { alert("hi"); }')
),
));
Read here for more: http://www.yiiframework.com/doc/api/1.1/CAutoComplete

Related

yii framework: how to make search result empty in search form?

Hello i created a separate search form having input box and button.
in my model i want to search products by category wise...
but problem is that when input box is empty and clicking on search buttons it displays all entries from the database table..
controller code is-
class AddController extends Controller
{
public function actionAddsearch()
{
$model_form = new Add("search");
$attributes = Yii::app()->getRequest()->getPost("Add");
if(!is_null($attributes))
{
$model_form->setAttributes(Yii::app()->getRequest()->getPost("Add"));
}
$this->render("searchResults", array(
"model" => $model_form,
"models" => $model_form->searchAdd(),
));
}
model code--
class Add extends CActiveRecord
{
public function searchAdd()
{
$criteria = new CDbCriteria();
$criteria->compare("addname", $this->category, TRUE, "OR");
$criteria->compare("category", $this->category, TRUE, "OR");
return Add::model()->findAll($criteria);
}
view code- addsearch.php
<div class="search-bar">
<?php
$form=$this->beginWidget('bootstrap.widgets.TbActiveForm',array(
"action"=>$this->createUrl("add/addsearch"),
'type'=>'search',
)
);
echo $form->textFieldRow($model,'category');
echo "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
$this->widget('bootstrap.widgets.TbButton',array(
'buttonType'=>'submit',
'type'=>'success',
'size'=>'large',
'label'=>'Search For Products ',
));
$this->endWidget();
?>
</div>
searchResults.php
<?php
echo "<h4>Results for Your Search</h4>";
foreach($models as $model):
$this->beginWidget('bootstrap.widgets.TbDetailView',array(
'type'=>'bordered condensed',
'data' => array(
'Shop Name' =>CHtml::link(CHtml::encode($model->addname),array('add/view','id'=> $model->addid)),
'Category' => $model->category
),
'attributes' => array(array('name' => 'Shop Name', 'label' => 'Name of Shop','value'=>CHtml::link(CHtml::encode($model->addname),
array('add/view','id'=>$model->addid)),'type'=>'raw'),
array('name' => 'Category', 'label' => 'Category of Shop'),
),
)
);
echo "<br><hr><br>";
$this->endWidget();
endforeach;
?>
what is wrong in the code??
I want to display no any product when text box is empty..
thanks in advance
change this
if(!is_null($attributes))
{
$model_form->setAttributes(Yii::app()->getRequest()->getPost("Add"));
}
$this->render("searchResults", array(
"model" => $model_form,
"models" => $model_form->searchAdd(),
));
To
if($attributes)
{
$model_form->setAttributes(Yii::app()->getRequest()->getPost("Add"));
$this->render("searchResults", array(
"model" => $model_form,
"models" => $model_form->searchAdd(),
));
}
1) Is the 'caterory' attribute safe on search scenario ? (in rules)
2) category is an attribute of the table schema ?

Yii dropdownlist saving value in ivar onclick

how can I save value as ivar from dropDownlist in yii? here my code, which doesn't work :/
declaration of ivar:
public $blaaa;
and my dropdownlist code:
echo CHtml::dropDownList(
'categoryDropDownList',
'',
CHtml::listData(Category::model()->findAll(), 'id', 'name'),
array(
'empty' => 'Select Category',
'onchange'=>function () {
$this->blaaa = 'js:this.value';
}
));
I tried also with ajax, but it also doesnt work:
'ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('category/actionBla'),
'data' => array('id' => 'js:this.value')
where
public function bla()
{
if (isset($_POST['id'])) {
$this->blaaa = $_POST['id'];
}
thanks in advance for help!
In ajax section, change url as below
'url' => CController::createUrl('category/bla'),
In controller, your function name should be
public function actionBla()
I solved my problem with echo'ing CHtml::dropDownList in form:
echo CHtml::form();
echo CHtml::dropDownList(
'categoryDropDownList',
'',
CHtml::listData(Category::model()->findAll(), 'id', 'name'),
array(
'empty' => 'Select Category'
));
echo CHtml::endForm();
and now activeForm bother about sending parameters via POST

php for loop on an object -zendform 2 forms

I am trying to retrieve data (a dependency) from a database and use the returned values to populate a zend form select element.
The final values should look like below:
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'jobId',
'options' => array(
'label' => 'countryList',
'value_options' => array(
'1'=>'USA',
'2'=> 'United Kingdom',
etc
'
),
),
'attributes' => array(
'value' => '1' //set selected to '1'
)
));
I have used doctrine2 to retrieve the values, i.e:
public function getOptionsForSelect()
{
$entity = $this->getEntityManager()
->getRepository('Workers\Entity\CountryList')
->findAll();
foreach ($entity as $entity)
{
echo $entity->country;
echo $entity->id;
}
}
The above gives me all the required values. I am however stuck on how to then place these values into an array such that once the $this->getOptionsForSelect() is placed in the form, it will immediately populate the values;
i.e.
foreach ($entity as $entity)
{
$id = $entity->id;
$country= $entity->country;
$data['data'] = $id.'=>'.$country;
}
return $data;
The final version of the form field will look like below:
$this->add(array(
'name' => 'countrylist',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'countrylist',
'value_options' => $this->getOptionsForSelect(),
'empty_option' => '--- please choose ---'
)
));
There is ObjectSelect/EntitySelect in DoctrineModule for ZF2 which might simplify this - https://github.com/doctrine/DoctrineModule/blob/master/docs/form-element.md
You don't need your own getOptionsForSelect(), because doctrine can handle that already.
If I understood you correctly, you could implement your getOptionsForSelect like so:
public function getOptionsForSelect()
{
$entity = $this->getEntityManager()
->getRepository('Workers\Entity\CountryList')
->findAll();
$options = array();
foreach ($entity as $entity) {
$options[$entity->id] = $entity->country;
}
return $options;
}
I haven't worked with doctrine, but I'd expect it to have some method to extract data as an array, or at least that I'd be possible to pass it to some serialized object to do that.

Filtering CGridView with CArrayDataProvider in Yii: how?

I created a CGridView in Yii whose rows are read from an XML file. I'm not using any models: only a controller (where I read the file) and a view (where I display the grid). What I cannot create is a filter (one input field per column) in the first row of the grid so to visualize only certain rows. How can I do it?
This is what I have until now:
Controller:
<?php
class TestingController extends Controller {
public function actionIndex() {
$pathToTmpFiles = 'public/tmp';
$xmlResultsFile = simplexml_load_file($pathToTmpFiles.'/test.xml');
$resultData = array();
foreach ($xmlResultsFile->result as $entry) {
$chromosome = $entry->chromosome;
$start = $entry->start;
$end = $entry->end;
$strand = $entry->strand;
$crosslinkScore = $entry->crosslinkScore;
$rank = $entry->rank;
$classification = $entry->classification;
$mutation = $entry->mutation;
$copies = $entry->copies;
array_push($resultData, array('Chromosome'=>$chromosome, \
'Start'=>$start, 'End'=>$end, Strand'=>$strand, \
'Crosslink_Score'=>$crosslinkScore,'Rank'=>$rank, \
'Classification'=>$classification, 'Mutation'=>$mutation, \
'Copies'=>$copies));
}
$this->render('index', array('resultData' => $resultData));
}
}
?>
View:
<?php
$dataProvider = new CArrayDataProvider($resultData, \
array('pagination'=>array('pageSize'=>10,),));
$this->widget('zii.widgets.grid.CGridView', array( 'id' => 'mutationResultsGrid',
'dataProvider' => $dataProvider, 'columns' => array(
array(
'name' => 'Chromosome',
'type' => 'raw',
),
array(
'name' => 'Start',
'type' => 'raw',
),
array(
'name' => 'End',
'type' => 'raw',
),
array(
'name' => 'Strand',
'type' => 'raw',
),
array(
'name' => 'Crosslink_Score',
'type' => 'raw',
),
array(
'name' => 'Rank',
'type' => 'raw',
),
array(
'name' => 'Classification',
'type' => 'raw',
),
array(
'name' => 'Mutation',
'type' => 'raw',
),
array(
'name' => 'Copies',
'type' => 'raw',
),
),
));
?>
Thanks for your help
Ale
file: FiltersForm.php (I put it in components folder)
/**
* Filterform to use filters in combination with CArrayDataProvider and CGridView
*/
class FiltersForm extends CFormModel
{
public $filters = array();
/**
* Override magic getter for filters
*/
public function __get($name)
{
if(!array_key_exists($name, $this->filters))
$this->filters[$name] = null;
return $this->filters[$name];
}
/**
* Filter input array by key value pairs
* #param array $data rawData
* #return array filtered data array
*/
public function filter(array $data)
{
foreach($data AS $rowIndex => $row) {
foreach($this->filters AS $key => $value) {
// unset if filter is set, but doesn't match
if(array_key_exists($key, $row) AND !empty($value)) {
if(stripos($row[$key], $value) === false)
unset($data[$rowIndex]);
}
}
}
return $data;
}
}
In your controller:
...
$filtersForm = new FiltersForm;
if (isset($_GET['FiltersForm'])) {
$filtersForm->filters = $_GET['FiltersForm'];
}
$resultData = $filtersForm->filter($resultData);
$this->render('index', array(
'resultData' => $resultData,
'filtersForm' => $filtersForm
)}//end action
And last what need - add filters to CGridView config array:
...
'dataProvider' => $dataProvider,
'enableSorting' => true,
'filter' => $filtersForm,
...
Why don't you store the information from the xml to a database and use YII ActiveRecord?
In your case you would need a live mechanism to filter the resultset on every filter query. Like with the search() method, when you use YII ActiveRecord models.
So you could use something like array_filter() with callback on your array on every filter call. (Edit: or the mechanism used here with stripos to return the matching "rows": Yii Wiki)
Or, second option, you could make the xml parser dependend on your filter inputs, which does not feel good to me :). The parser would habe to parse on every filter input, which could be a problem with big xml files.
Or, as mentioned, save the information to the database and use standard YII mechanisms.
Assuming you can use the data obtained in the objects in your foreach loop as the filter for that particular column, you could then pass these values through to the view, something like:
<?php
class TestingController extends Controller {
public function actionIndex() {
$pathToTmpFiles = 'public/tmp';
$xmlResultsFile = simplexml_load_file($pathToTmpFiles.'/test.xml');
$resultData = array();
foreach ($xmlResultsFile->result as $entry) {
...
$chromosomeFilter[] = $entry->chromosome;
...
}
$this->render('index', array(
'resultData' => $resultData,
'chromosomeFilter' => $chromosomeFilter,
...
);
}
}
?>
And then use that value in the filter for that column;
...
array(
'name' => 'Chromosome',
'type' => 'raw',
'filter' => $chromosomeFilter,
),
...
I've not tested, and it depends a lot on the structure of your xml and $entry->chromosome, but that might help put you on the right path?
I had the same problem
and what I did was I implement http://www.datatables.net/
And pull the data remotely . I pass the sorting and pagination to javascript .

Codeigniter - re-populating form on failed validation after submitting

I have a form that requires the user to enter some information. If they fail to complete the required fields they are re-presented with the form; the top of the page notifying them what fields are required and I've enabled sticky forms (set_value()) so their input is not lost.
I'm using flashdata to display messages to the user (i.e., if what they've entered already exists in the database).
My form is in the index method of my controller.
When submit is clicked from my view it calls the add() method in my controller.
The add() method performs the validation and depending on the results either submits to the database or kicks back out to the user to get more data.
I have several issues with the way that i've done this.
1. If validation fails I'm using $this->index() to get back to my form and display the validation errors. If I try using redirect, I lose my validation errors and my $_POST[] data so my sticky forms end up blank.
2. Using $this->index() appends the 'add' to the end of my url
3. Using $this->index() causes issues with the flashdata. Random results.
Any ideas?
<?php
class Restaurant extends Controller {
function Restaurant() {
parent::Controller();
}
function index() {
// Load libraries and models
$this->load->model('/restaurant/mRestaurantTypes');
$this->load->model('/restaurant/mRestaurant');
$this->load->model('/utilities/mUtilities');
// Get states
$stateSelect = array();
$getStates = $this->mUtilities->getStates();
if($getStates->num_rows() > 0) {
foreach($getStates->result() as $row) {
$stateSelect[$row->abbr] = $row->name;
}
}
// Get restaurant types
$restaurantTypes = array();
$getRestaurantTypes = $this->mRestaurantTypes->getRestaurantTypes();
if($getRestaurantTypes->num_rows() > 0) {
foreach($getRestaurantTypes->result() as $row) {
$restaurantTypes[$row->restaurant_types_id] = $row->type;
}
}
// Create form elements
$data['name'] = array(
'name' => 'name',
'id' => 'name',
'value' => set_value('name'),
'maxlength' => '200',
'size' => '50'
);
$data['address'] = array(
'name' => 'address',
'id' => 'address',
'value' => set_value('address'),
'maxlength' => '200',
'size' => '50'
);
$data['city'] = array(
'name' => 'city',
'id' => 'city',
'value' => set_value('city'),
'maxlength' => '50',
'size' => '25'
);
$data['state'] = $stateSelect;
$data['zip'] = array(
'name' => 'zip',
'id' => 'zip',
'value' => set_value('zip'),
'maxlength' => '10',
'size' => '10'
);
$data['phone'] = array(
'name' => 'phone',
'id' => 'phone',
'value' => set_value('phone'),
'maxlength' => '15',
'size' => '15'
);
$data['url'] = array(
'name' => 'url',
'id' => 'url',
'value' => set_value('url'),
'maxlength' => '255',
'size' => '50'
);
$data['type'] = $restaurantTypes;
$data['tags'] = array(
'name' => 'tags',
'id' => 'tags',
'value' => set_value('tags'),
'maxlength' => '255',
'size' => '50'
);
$data['active'] = array(
'name' => 'active',
'id' => 'active',
'value' => 'Y',
'maxlength' => '1',
'size' => '2'
);
// Set page variables
$data_h['title'] = "Add new restaurant";
// Load views
$this->load->view('/template/header', $data_h);
$this->load->view('/restaurant/index', $data);
$this->load->view('/template/footer');
}
/**
* Add the the new restaurant to the database.
*/
function add() {
// Load libraries and models
$this->load->library('form_validation');
$this->load->model('/restaurant/mRestaurant');
// Define validation rules
$this->form_validation->set_rules('name', 'Name', 'trim|required|max_length[255]|xss_clean');
$this->form_validation->set_rules('address', 'Address', 'trim|required|max_length[100]|xss_clean');
$this->form_validation->set_rules('city', 'City', 'trim|required|max_length[128]|xss_clean');
//$this->form_validation->set_rules('state', 'State', 'trim|required');
$this->form_validation->set_rules('zip', 'Zip', 'trim|required|max_length[128]|xss_clean');
$this->form_validation->set_rules('phone', 'Phone', 'trim|required|max_length[10]|xss_clean');
$this->form_validation->set_rules('url', 'URL', 'trim|required|max_length[255]|xss_clean');
$this->form_validation->set_rules('tags', 'Tags', 'trim|xss_clean');
// Form validation
if ($this->form_validation->run() == FALSE) {
// On failure
$this->index();
} else {
// On success, prepare the data
$data = array(
'name' => $_POST['name'],
'address' => $_POST['address'],
'city' => $_POST['city'],
'state' => $_POST['state'],
'zip' => $_POST['zip'],
'phone' => $_POST['phone'],
'url' => $_POST['url'],
'type' => $_POST['type'],
'tags' => $_POST['tags'],
'active' => $_POST['active'],
);
// Check if the restaurant already exists
$check = $this->mRestaurant->getRestaurant($data['name'], $data['zip']);
// If no records were returned add the new restaurant
if($check->num_rows() == 0) {
$query = $this->mRestaurant->addRestaurant($data);
if ($query) {
// On success
$this->session->set_flashdata('status', '<div class="success">Added New Restaurant!</div>');
} else {
// On failure
$this->session->set_flashdata('status', '<div class="error">Could not add a new restaurant.</div>');
}
redirect('restaurant/confirm', 'refresh');
} else {
// Notify the user that the restaurant already exists in the database
$this->session->set_flashdata('status', '<div class="notice">This restaurant already exists in the database.</div>');
redirect('restaurant/index');
}
}
}
function confirm() {
$data['title'] = "Confirm";
$this->load->view('/template/header');
$this->load->view('/restaurant/confirm', $data);
$this->load->view('/template/footer');
}
}
?>
I will try to help with the logic in the controller that I always use:
function index()
{
//set some default variables
$data['error_message'] = '';
//if this is to edit existing value, load it here
// from database and assign to $data
//...
//set form validation rules
$validation = array();
$validation['field_name'] = array(
'field' => 'field_name',
'label' => 'Field label',
'rules' => 'trim|required'
);
//more rules here
//...
$this->load->library('form_validation');
$this->form_validation->set_rules($validation);
//run validation
if ($this->form_validation->run() == FALSE)
{
$data['error_message'] .= validation_errors();
}
else
{
//do insert/update
//
//it's better to do redirection after receiving post request
//you can use flashdata for success message
if ( $success )
{
$this->session_set_flashdata('success_message', MESSAGE_HERE);
}
redirect(RESULT_PAGE);
}
//reaching this block can have 2 meaning, direct page access, or not have valid form validation
//assign required variables, such as form dropdown option, etc
//...
//load view
$this->load->view(VIEW_FILE, $data);
}
View file:
...
<?php if ( $error_message ): ?>
<?php echo $error_message; ?>
<?php endif; ?>
<?php echo form_open(current_url, array('id' => 'some_form_id')); ?>
<!-- form field here -->
<label for="field_name">Field label</label>
<input name="field_name" value="<?php echo set_value('field_name', $DEFAULT_FIELD_NAME_IF_NEEDED); ?>" />
<!-- more form field here -->
<?php echo form_close(); ?>
...
I hope this will help you.
For the $DEFAULT_FIELD_NAME_IF_NEEDED in the view file, I use this to pass the default value if this form page is to edit existing data from database. You can load the data in the controller, then pass it to view file and display it in the form field.
-------controller-------
$data['post'] = $_POST;
$this->load->view('view/view', $data);
then in your view
<input type="text" value="><?=$post['username'];?>" name="username">
I had a similar problem and I ended up doing:
My form is to create new user but you should get the idea.
if($this->form_validation->run() == FALSE)
{
$this->data['title'] = "Add User";
$this->load->vars($this->data);
$this->load->view('head');
$this->load->view('header');
$this->load->view('admin/sidebar');
$this->load->view('admin/add_user');
$this->load->view('footer');
}
So effectively instead of calling new function I was showing new view from the same function.
Not the nicest solution but it works.
Also you might want to use something like this to check if the restaurant already exists:
function _check_username($str)
{
$this->db->where('username', $str);
$query = $this->db->get('sm_users');
if($query->num_rows() == 0)
{
return TRUE;
}
else
{
$this->form_validation->set_message('_check_username', "User '$str' already exists!");
return FALSE;
}
}
And use callback validation function:
$this->form_validation->set_rules('userName','User Name','trim|required|min_length[3]|callback__check_username');
You could try having the form post to index, and in the index method, do validation if the form has been submitted. Then either render the index view (if there are errors) or add the restaurant and render the confirm view.

Categories