Validate date of one column greater than with another column in yii - yii

I have a form which has date_of_purchasing and expiry_date. I need to check date_of_purchasing should not be greater than expiry_date. How can i write this validation rule in model.
Im using YII framework. And im new for this framework.

Yii2
This should work if the compared column comes from the same table.
public function rules()
{
return [
[
['date_end'],
'compare',
'compareAttribute' => 'date_start',
'operator' => '>=',
'type' => yii\validators\DateValidator::TYPE_DATE,
'message' => 'Date end has to be greater than date start',
'enableClientValidation' => false
],
// other rules...
];
}
Don't forget to set enableClientValidation to false because Yii2 can't check the other date without a server side request.
Other configuration options here.
PS. I decided to update this thread 3+ years after because it remains
one of the first StackOverflow results on Google Search for this argoument.

Try to use CompareValidator at this link: Compare .

Related

phpbb 3.2.x user_add including custom Profile field

This has been driving me nuts for 2 days and I can't find an answer anywhere on Google so would really appreciate a little help..
I have a custom registration form on my website, which sends the data to a fairly standard PHPBB3 user_add process as follows:
$user_row = array(
'username' => request_var('createUsername',''),
'user_password' => phpbb_hash(request_var('createPassword','')),
'user_email' => request_var('createEmail',''),
'group_id' => '2',
'user_timezone' => '1.00',
// 'user_dst' => '0',
'user_lang' => 'en',
'user_type' => $user_type,
'user_actkey' => $user_actkey,
'user_ip' => $user->ip,
'user_regdate' => time(),
'user_inactive_reason' => $user_inactive_reason,
'user_inactive_time' => $user_inactive_time,
);
// Register user...
$user_id = user_add($user_row, $cp_data);
// If creating the user failed, display an error
if ($user_id === false)
{
trigger_error('NO_USER', E_USER_ERROR);
}
That works fine and I'm happy with it, however, I have created a custom profile field in the Admin Control Panel called 'ea_real_name' which I want to hold the user's real name. This corresponds to a field on the registration form called 'createRealName' (sent through as $_POST['createRealName'])
I know that user_add takes an optional field called 'cp_data', but I can't for the life of me work out how to format this data... Should it be an array (something like 'ea_real_name' => request_var('createRealName','') or something else?
PHPBB's wiki for the field is empty (https://wiki.phpbb.com/Custom_profile::submit_cp_field) so not much help...
Thanks! :-)
I was right in my assumption! It's an array with the field name prefixed by pf_.
Finally found an answer here: https://www.phpbb.com/community/viewtopic.php?f=71&t=1638905
$cp_data = array(
'pf_ea_real_name' => request_var('createRealName','')
);
Is the correct way to do it...

Custom editable fields in Contao

Is it possible to add new custom editable fields to the module Peronal data? If so, how does this work? PHP my admin and add Mysql tables? Or can this be done via the contao backend? Please advise
Its very much possible. I am not sure which contao version you are using now because they differ in how you create the database field.
Lets assume you want to add accept terms checkbox to the registration module.
Contao 2.11
In modules directory create a folder with the following structure
myModule/config/database.sql
myModule/dca/tl_member.php
myModule/languages/en/tl_member.php
In database.sql, create the field as follows
CREATE TABLE `tl_member` (
accept_terms char(1) NOT NULL default '',
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
In dca/tl_member.php, add the field to tl_member dca close to where login details are as follows.
$GLOBALS['TL_DCA']['tl_member']['palettes']['default'] = str_replace('login;','login,accept_terms;',$GLOBALS['TL_DCA']['tl_member']['palettes']['default']);
Create the field as follows(used to generate the checkbox input)
$GLOBALS['TL_DCA']['tl_member']['fields']['accept_terms'] = array(
'label' => &$GLOBALS['TL_LANG']['tl_member']['accept_terms'],
'inputType' => 'checkbox',
'eval' => array( 'mandatory' => true, 'tl_class' => 'w50', 'feEditable' => true,'feViewable'=>true)
);
Note:
mandatory => true // make it a mandatory field
feEditable => true // enable edit in module personal data or module registration
feViewable=>true // make it appear in module personal data or module registration
in languages/en/tl_member.php, create the labels as follows
$GLOBALS['TL_LANG']['tl_member']['accept_terms'] = array('Terms & Conditions', 'I accept the terms and conditions of using this website.');
Contao 3
The structure is pretty much the same only that you don't need the database.sql i.e you can remove it and modify dca/tl_member.php as follows
$GLOBALS['TL_DCA']['tl_member']['fields']['accept_terms'] = array(
'label' => &$GLOBALS['TL_LANG']['tl_member']['accept_terms'],
'inputType' => 'checkbox',
'eval' => array( 'mandatory' => true, 'tl_class' => 'w50', 'feEditable' => true,'feViewable'=>true),
'sql' => "char(1) NOT NULL default ''"
);
Note the addition of this line 'sql' => "char(1) NOT NULL default ''" in the array.
Now go to the install tool and create your field in mysql. login to the backend, go to modules, your personal data module and you should be able to see your field there. Check it to include it to frontend fields and you are done.
Please not the consistency of using tl_member and accept_terms in all the directories

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

LookbackAPI: When did user stories become unblocked?

I'm running the following query to the lookback API to find stories in a date range that were unblocked, but I'm getting no results. Am I missing something obvious? No errors, warnings or results returned.
Below is the Generated Query I get back from the lookback API:
'GeneratedQuery' => {
'fields' => 'true',
'skip' => 0,
'limit' => 100,
'find' => {
'_PreviousValues.Blocked' => 'true',
'_TypeHierarchy' => -51038,
'Blocked' => 'false',
'_ValidFrom' => {
'$lte' => '2012-11-02T04:00:00.000Z',
'$gte' => '2012-07-01T04:00:00.000Z'
}
}
},
When you pass in Boolean values, you need to make sure that they are bare true or false. If you pass them in as Strings, it will not behave as expected. Similarly for values of type Number. They should not have quotes around them.
Ok, the problem was related to "true" and "false" and the fact that I'm using Perl.
I'm using the Perl JSON library, and I didn't realize that you need to pass in JSON::true() and JSON::false() for true and false, not the literals 'true' and 'false'. So, in effect Larry was right: it was passing "true" instead of true.

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

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>