Yii: Unique e-mail address in rules validation not working - yii

I built a form that displays a user's existing email, then asks for a new email and a confirmation email to assure that they've typed it in correctly. The field for the new email is 'new_email', the confirmation field is 'new_email_confirm'. The model already has the field 'email' where their existing email address is stored. The scenario used is 'change_email'. Every other rule works and is validating with errors in my model but this one...when I enter in the exact same email/confirm that's already taken in 'email' I don't get an error, it is allowed to save.
In rules, I have added the following:
array(
'new_email,new_email_confirm',
'required',
'message'=>'This is a required field',
'on'=>'change_email',
),
array(
'new_email',
'email',
'message'=>'Invalid email address',
'on'=>'change_email',
),
array(
'new_email',
'compare',
'compareAttribute'=>'new_email_confirm',
'message'=>"Emails don't match",
'on'=>'change_email',
),
array(
'new_email',
'unique',
'className'=>'User',
'attributeName'=>'email',
'on'=>'change_email',
),
What am I doing wrong?

I have two dirty suggestions to do this, since I am not able to find out what the exact problem is.
1- write a custom validation for uniqueness and remove the unique rule:
array('new_email','customValidationRule'),
and write it like below:
public function customValidationRule(){
//after all validation rules
if(!$this->hasErrors()){
$check=User::model()->findByAttributes(array('email'=>$this->new_email)); //or $this->new_email_confirm
if(!is_null($check) || !empty($check)){
$this->addError('new_email','The new email has been already taken by other!');
}
}
}
2- replace new_email with new_email_confirm in your unique rule.

Related

Module configuration page with user groups

I'm completing a module that concerns the registration of new users.
Now, my module creates a table in the db that has an id field and an id_usergoup field.
On the configuration page, I would like to see the box with all the user groups available.
The user can select some and their id goes to write in my table. Similarly, in the box I have to see selected ones already chosen (if there are).
How can I make the box with user groups with HelperForm?
And take select the values already chosen and saved in my table?
I hope in your suggestion. Thank you.
In the array for the inputs, you can generate the customer group with this code:
array(
'type' => 'group',
'label' => $this->l('Customer group'),
'name' => 'groupBox',
'required' => true,
'values' => Group::getGroups((int)$this->context->language->id),
'hint' => $this->l('Customer group for which this tab will be available.'),
)
And you can get the selected values with: Tools::getValue('groupBox');
Regards!

Problems adding items with contact references when authenticated as an app

I'm trying to add an Item, where one of the fields is of type contact (user), to Podio.
I do not have the contact profile_id, only the name, so I need to search the contact to get the profile_id before adding.
The problem is that the /contact/ resources are inaccessible since I'm using app authentication.
The error is: "Authentication as app is not allowed for this method"
What is the recommended way to do this?
Thanks.
As I can see, the tricky part here is that you have just a name of the user. So you need to search this name first.
To be able to search you should be authenticated not as an app, but as a user with appropriate rights. I believe this is because search functions a rate-limited per user. You may authenticate on client side, server side or just by entering user's email and password (see documentation here).
Then, when authenticated, just use search functions with the parameter "ref_type": "profile" to look for the user name within space, organisation or globally. Example for PHP-client:
$attributes = array(
"query" => "John Doe",
"ref_type" => "profile"
);
$results = PodioSearchResult::space( $space_id, $attributes ); // search in space
$results = PodioSearchResult::org( $org_id, $attributes ); // search in organisation
$results = PodioSearchResult::search( $attributes ); // search globally
Functions above will return an array of the most relative results found. There you can get a user id and other user info.
Note that technically several different users may have the same name, so there might be more that one result found. It will be up to you to choose one of them somehow.

Laravel Sentry and check user without login

It is possible to check user by credentials without login for:
exits user
password is correct
is not banned
is activated
...
As I can see, I must write a lot of code for this. Maybe exists any easiest way?
Check the user is valid without logging in with Auth (Native):
$credentials = array('email' => 'xx', 'password' => 'xx', 'deleted_at' => null);
if(Auth::attempt($credentials, false, false)) {
// Exists and password is correct and not banned
}
This requires you to keep the deleted_at field in your table to soft deleting technique (By disabling or enabling a user temporarily).
If you are using a different field to make a user inactive/active then pass that field instead of deleted_at, for example, you may use a field active with value 1 to indicate an active user and 0 for an inactive user, in this case pass active => 1 to check the active field has value 1 as the third item in the credentials array.

Magento API - Several methods do not work

I've got the following problem. I built a PHP file, which reads categories from a file, to impor tthem into Magento. I am able to read the file, no problem. The connection via NuSOAP to the Magento API works aswell. I can get the SessionID and I am able to get data, like Information for a category, also its possible to delete categories.
But, whenever I try to create or update anything, it throws an error. The rights for the user are ok aswell. For example, when I create a category, I add the usual data to the call:
$proxy->call(
$sessionId,
'category.create',
$rootCategory, array(
'name' => "TEST",
'is_active' => '1',
'page_layout' => 'two_columns_right',
'description' => "TEST",
'meta_title' => "TEST",
'meta_description' => '',
'meta_keywords' => "TEST",
'include_in_menu' => '0',
'display_mode' => 'PRODUCTS',
'available_sort_by' => 'price',
'default_sort_by' => 'price',
'is_anchor' => '0'
)
);
All the time, it says:
(
[faultcode] => 102
[faultstring] => Category not exists. )
Which is not true. The $rootCategory is definatly a category, which is existing. I tried all other categories, I tried to add a 'path' to the info, I tried to use less information (only the neccesary stuff), I tried to read existing categories to get their IDs, NOTHING works. It always throws this faultcode.
Same happens, when I try to update a category, or create /update a product. Deleting is no problem at all.
Do you see the problem?
i just compared your NON working exemple and i found this while comparing it with another exemple i has ( i do not pretent to be expert ) ..
but seems like your $new_category, array(blahblha) ...
should be INSIDE an array according to the exemple i already have
like this array($new_category,array(blahblah) ...
this is the mains difference i just saw ..
here is the EXEMPLE i just pulled out of the web ... Adapt to your needs..
$proxy->call(
$sessionId,
'category.create',
array(
3,
array(
'name'=>'New openerp',
'is_active'=>1,
'include_in_menu'=>2,
'available_sort_by'=>'position',
'default_sort_by'=>'position')) );
Have you tried specifying the category_id key in your $rootCategory within the call:
$selectedCategory['category_id'],
array('name'=>'New Category Through Soap')
)
Reference: http://www.magentocommerce.com/wiki/doc/webservices-api/api/catalog_category

CakePHP: how to get recently created auto-incremented field

I have a user submit a field to the database, it validates, and makes the entry. The primary key of this new row is auto-incremented.
The user then gets to another form where that newly created field is required.
Can anyone shed any light on this problem?
Thanks in advance!
Check out Model::getInsertID();
$this->getLastInsertID();
cakephp model docs
// Save the first form
$this->Ingredient->save($newData);
// Get the id of the record just saved
$newIngredientId = $this->Ingredient->id;
// Redirect to a new form
$this->redirect(array(
'controller' => 'dish',
'action' => 'add',
'?' => array('lastId' => $newIngredientId)
));
http://book.cakephp.org/view/1031/Saving-Your-Data,
http://book.cakephp.org/view/1442/link
If it's in the database, when you get to your next form just read it out again in your controller and set it to a view variable.
$this->set('lastid', $this->Model->read(null,$id));
Or if you need to search the database for the field use find
$this->set('lastid', $this->Model->find('first', array('conditions'=>array('username'=>'MyUserName')) , array('id')));
You could go around it by using mysql_insert_id()