Why does this Zend_Form NotEmpty validator give me an error message? - zend-form

For Zend Form Elements which are required I normally do this:
$this->addElement('text', 'loginEmail');
$this->loginEmail
->setLabel('Email')
->setRequired(true);
I was unhappy with the error message though. The Zend Documentation says that setRequired() merely adds a NotEmpty validator to the chain, so I did this:
$validator = new Zend_Validate_NotEmpty();
$validator->setMessage('A value is required');
$this->addElement('text', 'loginEmail');
$this->loginEmail
->setLabel('Email')
->addValidator($validator);
But now I'm not getting any error message for that element!

OK, so I didn't really get an answer to my question, however I have come to realise that the setRequired() validator is a complex little animal, and probably not the Zend team's proudest moment. So, instead of looking for an answer to the question above I have found an approach to setRequired which works quite nicely. Here it is:
$this->addElement('text', 'orgName');
$this->orgName
->setLabel('Name')
->addFilter('StringTrim')
->addValidator('NotEmpty', true, array(
'messages' => 'Name is required'))
->setRequired(true)
->addValidator('Alnum', false, array(
'allowWhiteSpace' => true,
'messages' => 'Alpha and numeric characters only'))
->addValidator('StringLength', false, array(
'min' => 3,
'max' => 128,
'messages' => 'Between 3 and 128 characters'));
The key thing is to add the NotEmpty validator and configure it before calling setRequired. When setRequired is called it detects that a NotEmpty validator has already been added to the chain and therefore does not add its own instance.

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...

Validate date of one column greater than with another column in 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 .

findByAttributes Example

Explanation for the use of $condition and $param in findByAttributes in Yii
In most case, this is how I use findByAttributes
Person::model()->findByAttributes(array('first_name'=>$firstName,'last_name'=>$lastName));
Copy from this thread http://www.yiiframework.com/forum/index.php/topic/21178-findbyattributes-example/
Explain what you want to do and where is your errors.
Try to read documentation http://www.yiiframework.com/doc/api/1.1/CActiveRecord#findByAttributes-detail
When you do a find in Yii's CActiveRecord using the attributes of the model, you would most likely find by some equality.
Person::model()->findByAttributes(array(
'first_name' => $firstName,
'last_name' => $lastName,
));
In English, this translates to "Find me a person whose last name is $lastname and whose first name is $firstname". But this is a bad example. Consider the following.
Person::model()->findByAttributes(array(
'age' => 18,
));
This is a better example which translates, in English, to "Fetch me all eighteen year olds" (that is, "where age = 18). This can be re-written with conditions like so
Person::model()->findByAttributes(array(
'condition' => 'age = :age',
'params' => array(
':age' => 18,
),
));
So far, there may seem to be little benefits. But such expressions make us able to use more boolean operators. For example...
Person::model()->findByAttributes(array(
'condition' => 'age < :age',
'params' => array(
':age' => 18,
),
));
...can now help us find all those below a certain age.
The query can grow as complex as you wish with $conditions and $params, but with your normal method, you may only use assignment queries.

Drupal module's list of permissions are stuck

From what I understand, my problem is that I am (rather, my client is) running an older version of Drupal, specifically Core 6.26 If you're curious about any additional specs, I'll be happy to divulge.
With that out of the way, I made a new module with the following menu hook.
function checkin_menu(){
$items = array();
$items['checkin'] = array(
'title' => 'Checkin'
,'type' => MENU_CALLBACK
,'access arguments' => array('checkin')
,'page callback' => 'checkin'
);
}
The permissions listed out exactly what I expected. There was a section called "Checkin" the same as the module's name as specified in the .info file, and one item to give permissions to "checkin"
Later on I expanded the module to have two different paths. The second one is supposed to be for admins only.
function checkin_menu(){
$items = array();
$items['checkin'] = array(
'title' => 'Checkin'
,'type' => MENU_CALLBACK
,'access arguments' => array('create a checkin')
,'page callback' => 'checkin'
);
$items['checkin_admin'] = array(
'title' => 'Checkin Admin'
,'type' => MENU_CALLBACK
,'access arguments' => array('view all checkins')
,'page callback' => 'device_checkin_page'
);
return $items;
}
Much to my surprise neither "create a checkin" or "view all checkins" is showing up. I still have the original "checkins" showing on the permissions page. I've been hunting for answers for a couple days now. Help a guy out?
Permissions are defined by a different hook, which is hook_perm.
So you should be doing something like this:
/**
* Implementation of the hook_perm()
*/
function checkin_perm() {
return array (
'create a checkin',
'view all checkins',
);
}

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.