multiple checkbox valiadation for any one in YII frame work for this example - yii

Hi can you please tell how can i validate the multiple checkbox any one is checked in yii framework
array('accept', 'required', 'requiredValue' => 1, 'message' => 'You should select alteast one')

As these value are usually sent as arrays, I wrote an array validator for these cases once: https://github.com/schmunk42/p3extensions/blob/master/validators/P3ArrayValidator.php
Usage example:
array('accept',
'ext.validators.P3ArrayValidator',
'min'=>1,
'allowEmpty'=>false,
'message' => 'You should select at least one'
),

sorry for the late reply.
But, I found a solution without installing any extension.
Take a hidden field with the same name [Checkboxes list field].
<?php echo $form->hiddenField($model,'categories');?>
Display a list of categories with name different from our field name (multiple checkboxes).
But, remember the 'class', and play with the class to save the values.
<?php
echo CHtml::checkBoxList(
'group',
//you can pass the array here which you want to be pre checked
explode(',', trim($model->attributes['categories'], ',')),
CHtml::listData(Category::model()->findAll(),'id','name'),
array('separator'=>'', 'template'=>'<tr><td style="width:5%;">{input}</td><td>{label}</td></tr>', 'class' => 'group')
);
?>
This way validation should work also, you get category ids as comma separated e.g. [,1,2,6,]
<script>
$(function(){
$(".group").click(function(){
var str = $('.group:checked').map(function() {
return this.value;
}).get().join();
var groupCats = (str.length > 0) ? ','+str+',' : '';
$('#ModelNAME_field').val(groupCats);
// Get the 'ModelNAME_field' by viewing source of HTML of hidden field.
});
});
</script>

Related

Yii : Multiple activeCheckboxlist with same model

has just started out Yii web app and encountered this problem, any suggestions are welcome:)
What i am trying to achieve:
-To display a form with tabs, each tab content contains a list of checkboxes from the same model.
-so user can select some items from tab 1, some from tab 2, etc and then click submit button to process.
Problem:
But i couldn't think of anyway such that the last tab activecheckboxlist will not clobbered the previous one up.
I am trying to to something similar to this : [www.yiiframework.com/forum/index.php/topic/20388-2-checkboxlist-and-1-model]
but instead of fixing it at 2, mine is dynamic.
What i have done so far:
<?php
$tabArray = array();
foreach ((Product::model()->listParentChild(0)) as $productparent) {
array_push($tabArray, array(
'label' => $productparent['name'],
'content' => CHtml::activeCheckBoxList(
$model, 'products', CHtml::listData(Product::model()->listParentChild($productparent['id']), 'id', 'name'), array(
'labelOptions' => array('style' => 'display:inline'),
'template' => '<div class="check-option">{input} {label}</div>',
'separator' => '',
)
), 'active' => ($productparent['id'] == 1 ? true : false),
));
}
?>
<?php
$this->widget('bootstrap.widgets.TbTabs', array(
'type' => 'tabs', // 'tabs' or 'pills'
'placement' => 'left',
'tabs' => $tabArray,
));
?>
and in my product model:
public function listParentChild($parentid) {
$sql = "SELECT * FROM piki_product WHERE parentid=:parentid";
$productlist = Yii::app()->db->createCommand($sql);
$productlist->bindValue(":parentid", $parentid, PDO::PARAM_INT);
return $productlist->queryAll();
}
any suggestions will be appreciated.. :/
I could be wrong, but I don't think cliffbarnes is on the right track with his comments about dynamic nesting. As far as I can tell, you're only dealing with one level of child products; it's just that there could be multiple sets of these child products.
In that case, the link you sited actually offers the correct solution:
<?php echo CHtml::checkBoxList('array1', CHtml::listData(Atributos::model()-> findAllByAttributes(array('tipo'=>'talla')), 'id_atributo','valor'))?>
<?php echo CHtml::checkBoxList('array2', CHtml::listData(Atributos::model()-> findAllByAttributes(array('tipo'=>'talla')), 'id_atributo','valor'))?>
Each set of checkboxes is given a different name (array1, and array2), so that each field's selected values doesn't override the other. In your case, the solution is the same; you just need to make the field names dynamic. I.E.
foreach ((Product::model()->listParentChild(0)) as $productparent) {
$fieldname = 'product' . $productparent['id'];
echo CHtml::checkBoxList($fieldname, ... (etc)
Within your controller you would check to see whether there are results for each dynamic field name.
foreach ((Product::model()->listParentChild(0)) as $productparent) {
if (isset($_POST['product' . $productparent['id']]) {
// Add values to $model->product
}
}
An even better solution would be to output each checkbox individually, so you can create one array of results, indexed by child ID.
foreach ((Product::model()->listParentChild(0)) as $productparent) {
foreach (Product::model()->listParentChild($productparent['id']) as $child) {
CHtml::checkBox("product[{$child['id']}]", ... (etc)
Then in your controller, all you'd have to do is this:
if (isset($_POST['product']) && count($_POST['product']) > 0) {
$model->product = array_keys($_POST['product']);
}
This solution does not work with activeCheckBoxList(). It would work if you wanted to override the __get() and __set() magic methods to make these dynamic property names available to your model, but that's probably over kill.
Edit (as per request)
If you need to have default selections for your checkboxes, you can just pass them as the second argument of CHtml::checkBoxList(). http://www.yiiframework.com/doc/api/1.1/CHtml#checkBoxList-detail
But if you still want to use __get() and __set(), here's an example:
class YourModel extends CActiveRecord {
// I usually create a placeholder to contain the values of my virtual attribute
protected $_childValues = array();
public function __get($name) {
// The following regular expression finds attributes
// with the name product_{parent ID}
if (preg_match("/^product_\d+$/", $name)) {
// I put the underscore in the name so I could get
// parent ID easier.
list($junk, $id) = explode("_", $name);
if (!isset($this->_childValues[$id])) {
$this->_childValues[$id] = array();
}
return $this->_childValues[$id];
}
else {
// Make sure to still call the parent's __get() method
return parent::__get($name);
}
}
public function __set($name, $value) {
// Same regex as above
if (preg_match("/^product_\d+$/", $name)) {
list($junk, $id) = explode("_", $name);
$this->_childValues[$id] = $value;
}
else {
// Make sure to still call the parent's __set() method
parent::__set($name, $value);
}
}
}
$model = new YourModel;
// Any property in the format of product_{parent ID} is available
// through your model.
echo $model->product_1;
$model->product_300 = array();
You might also consider checking to see if the parent ID in a property name corresponds with a parent ID in the database, instead of just allowing any property in that format to pass through.

Restricting a category for a certain country in Prestashop 1.5

I need to restrict a category to a set of countries in Prestashop 1.5.
This restriction would prevent the shipping of a product belonging to such a category; as such, the users would still be able to see the products but they would not be able to buy them.
Ideally, I wanted to develop a module that would insert a list of countries (checkbox style, as in the Modules -> Payment page (AdminPayment)) inside a category's edit page, but I haven't been able to do so.
Why can't i simply paste the following code inside the renderForm() function?
Only the description is visible if i do so...
array(
'items' =>Country::getCountries(Context::getContext()->language->id),
'title' => $this->l('Country restrictions'),
'desc' => $this->l('Please mark the checkbox(es) for the country or countries for which you want to block the shipping.'),
'name_id' => 'country',
'identifier' => 'id_country',
'icon' => 'world',
),
EDIT:
I managed to get the list of countries working:
array(
'type' => 'checkbox',
'label' => $this->l('Restricted Countries').':',
'class' => 'sel_country',
'name' => 'restricted_countries',
'values' => array(
'query' => Country::getCountries(Context::getContext()->language->id),
'id' => 'id_country',
'name' => 'name'
),
'desc' => $this->l('Mark all the countries you want to block the selling to. The restrictions will always be applied to every subcategory as well')
),
Now, I can save these values by checking if the value "submitAddcategory" is being submitted in the postProcess function and by running an insert query there. Similarly, I can also load the IDs of the blocked countries from the database, but how can I tick the respective select boxes in the list of countries?
My initial "quick and dirty" idea was to use jQuery selectors inside a document.ready(), but the code gets inserted before everything else and, as such, it won't work because jQuery isn't even loaded yet.
How can this be done?
Cheers
I solved it by using the following code right before the end of the renderForm() function.
The Pièce de résistance was $this->fields_value, as sadly I didn't known of its existence.
public function getRestrictedCountries($obj)
{
// Loading blacklisted countries
$country = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT DISTINCT id_country
FROM `'._DB_PREFIX_.'category_country_restriction`
WHERE id_category = ' . (int)Tools::getValue('id_category') . ';');
$blacklisted_countries = array();
if (is_array($country))
foreach ($country as $cnt)
$blacklisted_countries[] = $cnt['id_country'];
// Global country list
$c_todos = Country::getCountries(Context::getContext()->language->id);
// Crossmatching everything
foreach ($c_todos as $c)
$this->fields_value['restricted_countries_'.$c['id_country']] = Tools::getValue('restricted_countries_'.$c['id_country'], (in_array($c['id_country'], $blacklisted_countries)));
}
PS: The table I am reading from is basically an associative table between 'category' and 'country'

Yii and database row in dropdown

I have two model: test1 , test2
And an action in test1 :
public function active_widgets_list()
{
$widgets = SiteWidget::model()->find('status=:status', array(':status' => '1'));
return $widgets;
}
And I will show test1.tbl_1 rows as dropdown list in test2's view:
$list=CHtml::listData(SiteWidget::model()->active_widgets_list(), 'id', 'title');
echo $form->dropDownList($model,'widget_id', $list, array('empty' => 'Select Please'));
but down't work. i have just an empty dropdown.
You should be using findAll instead of find, since find returns only a single active record with the specified condition.
$widgets = SiteWidget::model()->findAll('status=:status', array(':status' => '1'));
If you use Gii tools, You don't need any thing for saving. It generate all the codes you need it. It is so easy to make a huge of models, controllers, views and CRUD.
http://www.yiiframework.com/doc/guide/1.1/en/topics.gii

Yii - Widgets - Explanation

I am trying to use drop down list widget :
$this->widget('ext.multiselect.JMultiSelect',array(
'model'=>$model,
'attribute'=>'attribute',
'data'=>$data,
// additional javascript options for the MultiSelect plugin
'options'=>array()
));
What I want to know is that Details of each option available in the widget array Like what is 'attribute' , 'model' and'data' represent , as I an unable to understand it form the documentation.
The model param is the model you are creating the multi select for.
The attribute is the model attribute you are creating the multi select for.
The data is an array of key/value pairs for the list items you want to display in the multi select.
For example, if you had a model 'User' and in that model you had a field 'access_rights' and you wanted to have that field as a multi select box with a few values, you might do something like:
In your controller:
$model = new User;
$data = array(
'admin_area'=>'Admin Area Access',
'product_area'=>'Product Area Access',
'customer_area'=>'Customer Area Access',
... etc
);
In your form in your view file:
$this->widget('ext.multiselect.JMultiSelect',array(
'model'=>$model,
'attribute'=>'access_rights',
'data'=>$data,
// additional javascript options for the MultiSelect plugin
'options'=>array()
));
Edit:
To add data to the multi select options from another model you can use the CHtml::listData() method, this takes an active record result set and converts it into an array of key/value pairs so you can use in any of the other CHtml methods that require a key/value pair. To do this you simply get the records you're after from the database using active record, eg;
$myData = Data::model->findAll();
You can then put that into the listData() method and it'll create your array:
$this->widget('ext.multiselect.JMultiSelect',array(
'model'=>$model,
'attribute'=>'access_rights',
'data'=>CHtml::listData($myData, 'id', 'name'),
// additional javascript options for the MultiSelect plugin
'options'=>array()
));
(where 'id' and 'name' are the fields from the model table that you want to be the 'key' and 'value' within the array)
echo $form->dropDownList($model, 'category', CHtml::listData(TblCategory::model()->findAll(),
'id', 'category_name'), array('empty' => '---Select Category---',
'style' => 'width:350px;')), array() ?>
<?php echo $form->error($model, 'category'); ?>

how to add static elements to yii dropDownList?

echo $form->dropDownList(
$model,'categoryId',
CHtml::listData(Category::model()->findAllBySql(
'SELECT * from category where isnull(parent_id)'),
'id', 'name'),
array(
'empty'=>Yii::t('fim','Search All'),
Yii::t('fim','Jobs'),
Yii::t('fim','Training'),
Yii::t('fim','Events'),
Yii::t('fim','News')
)
);
Jobs, Training, Events and News are not appearing.
How can/should we build this, in order to add those values to the select box ?
Thanks
You cannot add static elements by using the $htmlOptions parameter. Here is how I do it:
$data = CHtml::listData(Category::model()->findAllBySql(
'SELECT * from category where isnull(parent_id)'),
'id', 'name');
// Add extra options here: I am actually prepending with this syntax,
// but you are free to append or interleave instead. Array keys are the values.
$static = array(
'jobs' => Yii::t('fim','Jobs'),
'training' => Yii::t('fim','Training'),
'events' => Yii::t('fim','Events'),
'news' => Yii::t('fim','News'),
);
echo $form->dropDownList(
$model,
'categoryId',
$static + $data,
array('empty'=>Yii::t('fim','Search All')));
For me, what i did is i added jquery code, i append an html option
$("#categoryId").append("<option value='0'>Additional Field</option>");
its less complicated and it works for me