Yii CMaskedTextField with one or two digits 0-99 (quantity) - input

I'm trying to make CMaskedTextField as quantity input field with base quantity = 0, and maximum = 99.
Can't figure out the mask and/or charMap.
I've tried:
$this->widget('CMaskedTextField', array(
'model' => $position,
'attribute' => 'Quantity',
'mask' => '99',
'value' => $currentQuantity,
'htmlOptions' => array('size' => 2, 'maxlength'=>2, 'minlength'=>1)
), true);
but this mask means there must be only 2 digits
Optional (i guess js will do the work if cmaskedtextfield not): when user clears input it should converts to '0'.

Mask might be set thru regex:
\d{1,2}
In model rules() you might define:
array('attributeName', 'match', 'pattern' => '/\d{1,2}/',
'message' => '{attribute} can only contain digits from 0 to 99'),
There is no convertion to 0 but the explicit message for user.

Related

Prestashop 1.6.1 Helper Form fields undefined index

I'm struggling with this for quite some hours:
I'm trying to add new fields to a form generated with the HelperForm class in Prestashop for a custom module.
I try to do this for the configuration page of the module in the getContent() function
The following field is accepted and it works:
array(
'type' => 'file',
'label' => $this->l('Button image'),
'id' => 'button_image_path',
'name' => 'button_image_path',
'image' => '<img src="'._MODULE_DIR_.$this->name.'\\img\\'.basename($buttonImage["setting_value"]).'" class="button-image-preview" width="30">'
)
However, when i try to add other fields like this:
array(
'type' => 'text',
'label' => $this->l('Number of displayed products'),
'name' => 'CROSSSELLING_NBR',
'desc' => $this->l('Set the number of products displayed in this block.'
)
It gives this error:
Notice on line 387 in file D:\wamp\www\qmart.ro\tools\smarty\sysplugins\smarty_internal_templatebase.php(157) : eval()'d code
[8] Undefined index: CROSSSELLING_NBR
However, the input is still generated, and it looks like this:
<input type="text" name="CROSSSELLING_NBR" id="CROSSSELLING_NBR" value="" class="">
What i tried:
Changing the input type from text to color for example, and it gave the same error
Changing the label content and the name content, and the error still appeared
I did not change anything in the core files.
So, the form is being built for those inputs, but this "undefined index" thing still occurs.
So, apparentply they force you to choose some default value for the inputs.
i simplu solved it by adding this line:
$helper->fields_value['CROSSSELLING_NBR'] = '';
According to your code...
array(
'type' => 'text',
'label' => $this->l('Number of displayed products'),
'name' => 'CROSSSELLING_NBR',
'desc' => $this->l('Set the number of products displayed in this block.'
)
You have an error in 'desc', you need close the final parenthesis, this should works...
array(
'type' => 'text',
'label' => $this->l('Number of displayed products'),
'name' => 'CROSSSELLING_NBR',
'desc' => $this->l('Set the number of products displayed in this block.')
)

Yii1 CGridView - SUM of Custom Column Value in footer

I want Sum of a custom column value in footer
GridView Code
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'booking-grid',
'dataProvider'=>$model->search(),
'columns'=>array(
array(
'name'=>'name',
'header'=>'User Name',
'value'=>'$data->booking_id',
'filter'=>false,
'footer'=>'Total',
'footerHtmlOptions'=>array('class'=>'grid-footer'),
),
array(
'name'=>'id',
'header'=>'User Fee',
'value'=> array($model,'GetUserFee'),
'filter'=>false,
'footer'=>'' . $model->getTotal($model->search()->getData(), 'id'),
'footerHtmlOptions'=>array('class'=>'grid-footer'),
),
),
));
Get Total
public function getTotal($records,$colName){
$total = 0.0;
if(count($records) > 0){
foreach ($records as $record) {
$total += $record->$colName;
}
}
return number_format($total,2);
}
Second column value are calculate user fee.
In footer sum of User Fee values get the wrong sum. It gives the sum of user ids. and id is table column name.
How can I get the sum of User fee column values
Instead of using a literal representation of id, try using the $data variable created by CGridView, which actually IS the current record from the $model currently being processed in each CGridView iteration.
Your code would then be:
array(
'name'=>'id',
'header' => 'User Fee',
'type' => 'raw',
'value' => '$data->id',
'filter' => false,
'footer' => '$model->getTotal($data)',
'footerHtmlOptions' => array('class'=>'grid-footer'),
),
Notice the value of the type attribute is set to raw.
You can even use a PHP function and pass it the $data variable, like so:
array(
'name'=>'id',
'header' => 'User Fee',
'type' => 'raw',
'value' => function ($data) { ... handle $data how you like ... }
'filter' => false,
'footer' => '$model->getTotal($data)',
'footerHtmlOptions' => array('class'=>'grid-footer'),
),
For more information check out special variables in Yii

Changing CForm dropdown name attribute in Yii

I'm using Yii and I'm having a little problem with some dropdowns.
Basically I'm using CForm to display some dropdown menus of courses. A student can select up to two courses and for each course choice the student can select a 1st choice and second. It is a requirement that each course choice is inserted separately into the database. For example, it a student wants to study 2 courses and wants to have a 1st and 2nd priority course, they would choose like this:
Course one - 1st Priority
Course one - 2nd Priority
Course two - 1st Priority
Course two - 2nd Priority
This would put 4 new rows into the database. The administrators of the courses want this displayed as 4 dropdown menus containing the courses.
At the moment, I'm testing with just the 1st and 2nd priorities of course one, but the problem is that course one - priority one is always empty unless a value is selected for priority two. Then priority one gets the same value as priority two, even though two different courses are selected. I've been following this tutorial Form Builder as I am using the Wizard Behavior which uses CForm to build the forms.
Here is my code so far, again only dealing with "course one":
This is a snippet of the relevant code from the controller:
// inside controller
$model = new CourseChoice();
$form = new CForm('application.views.wizard.ccForm', $model);
$form['courseOneP1']->model = new CourseChoice();
$form['courseOneP2']->model = new CourseChoice();
$c1p1 = $form['courseOneP1']->model;
$c1p2 = $form['courseOneP2']->model;
// Here I am just reading the attributes and exiting for testing
if ($form->submitted()&& $form->validate()) {
echo '<pre>';
print_r($c1p1->attributes);
print_r($c1p2->attributes);
echo '</pre>';
exit;
..........
And here is code in the form in ccForm
return array(
'showErrorSummary' => true,
'title' => 'Course Choice 1',
'elements' => array(
// Course 1 - 1st Priority
'courseOneP1' => array(
'type' => 'form',
'elements' => array(
'course' => array(
'label' => '1st Priority',
'type' => 'dropdownlist',
'id' => 'c1p1',
'prompt' => 'Select 1st Priority Course',
'items' => CHtml::listData(CoursePeriod::model()->with('course')->findAll("year = 2014"), 'id', 'course.course_name'),
)
),
),
// Course 1 - 2nd Priority
'courseOneP2' => array(
'type' => 'form',
'elements' => array(
'course' => array(
'label' => '2nd Priority',
'type' => 'dropdownlist',
'id' => 'c1p2',
'prompt' => 'Select 2nd Priority Course',
'items' => CHtml::listData(CoursePeriod::model()->with('course')->findAll("year = 2014"), 'id', 'course.course_name'),
)
),
),
),
'buttons' => array(
'previous' => array(
'type' => 'submit',
'label' => 'Previous'
),
'submit' => array(
'type' => 'submit',
'label' => 'Next'
)
)
);
So lets say I choose 2 courses, one with an id of 15 and the other with an id of 86, I get the following when I print_r() both dropdowns:
Array // Dropdown 1
(
[course] => 86
.... // other irrelevant attributes
)
Array // Dropdown 2
(
[course] => 86
.... // other irrelevant attributes
)
Update
I've been looking further into this and when I look at firebug, I see that both dropdowns have the same name:
<div class="row field_course">
<label for="c1p1">1st Priority</label>
<select id="c1p1" name="CourseChoice[course]">
</div>
<div class="row field_course">
<label for="c1p2">2nd Priority</label>
<select id="c1p2" name="CourseChoice[course]">
</div>
So the 2nd menu is overwriting the first. But how can I change this? If I change 'course'=>array(.... in the CForm for either subform, the applicable dropdown does not render. I have already tried adding 'name'=>'course1' in the form but it makes no difference.
Couldn't you just set the name of the 2nd priority input element?
'course' => array(
'label' => '2nd Priority',
'name' => 'course2',
'type' => 'dropdownlist',
'id' => 'c1p2',
'prompt' => 'Select 2nd Priority Course',
'items' => CHtml::listData(CoursePeriod::model()->with('course')->findAll("year = 2014"), 'id', 'course.course_name'),
)
Just to answer my own question and close it as it is pretty old now, CForm does not support tabular input and would need to extended to achieve this. Probably not a big job but in the end I convinced management that the four dropdowns design was horrible. :-) I went with a more flexible design showing a gridview of courses in a pop-up to choose courses instead which works well and is less confusing for the user.
Anyone interested in this issue can see the open issue here. There is a link in there to view a possible implementation of extending CForm, though this was posted at the end of 2009.

WP_Query $args and compare operators

I'm having troubles to generate an sql query with operators.Actually wirte this code:
$args = array(
'post_type' => 'listings',
'posts_per_page' => 10,
'paged' => $paged,
'meta_query' => array(
//CITY
array(
'key' => 'city',
'value' => $city,
'compare' => 'LIKE'
),
//type
array(
'key' => 'type',
'value' => $type,
'compare' => 'LIKE'
),
//PRICE
array(
'key' => 'price',
'value' => array( 100, 5000 ),
'compare' => 'BETWEEN'
),
)
);
This code work fine, but now, i need to get the posts with price between $start and $end, OR price = -1. In other words, post that not have set the price, or set to -1 must show in the query. The others conditions (city, type,etc) must be match with AND
Any ideas? Thanks!
My initial thought is to add a filter onto posts_where or posts_join to inject a custom SQL condition into the query.
Something along these lines:
wp_postmeta.meta_key='price' AND
(wp_postmeta.meta_value='-1' OR
CAST(wp_postmeta.meta_value AS INT) BETWEEN $start AND $end)

yii tokeninput extension cursor focus on wrong place after same match?

I am having problem with yii tokeninput extension. When i search name it gives the user list and if i select any name and if that name is also selected previous than the cursor point after the selected item, it does not point at the end of all the the item in the input box.
I am using this configuration.
$this->widget('ext.tokeninput.TokenInput', array(
'model' => $model,
'attribute' => 'USER_ID',
'url'=>$this->createUrl('user/searchUserNames'),
'options' => array(
'allowCreation' => false,
'preventDuplicates' => true,
// 'resultsFormatter' => 'js:function(item){ return “<li><p>” + item.name + “</p></li>” }',
'theme' => 'facebook',
//'hintText' => 'Type',
'prePopulate' => $prePopulate,
'processPrePopulate' => $processPrePopulate,
)
));
I have also lookout at the examples but does not find the solution. can any one help me ?
Loopj: jquery token input demo
plz comment the line number 509 in **jquery.tokeninput.js**
input_token.insertAfter(found_existing_token);
that line insert cursor after that selected item so if you comment
this line cursor is at the end of all names