Have some validating problems which seem to appear only when using the Auth component.
I have 4 fields in my register form: username, password, password_confirm and email.
I also have the multivalidatable behaviour attached to my User model. Here are the rules which apply to the register form:
var $validationSets=array(
"register" => array(
"username" => array(
"usernameFieldNotEmpty" => array(
"rule" => "notEmpty",
"message" => "You did not enter the username!"
),
"usernameValid" => array(
"rule" => "__alphaNumericDashUnderscore",
"message" => "The username you entered is not valid!"
),
"usernameExistsInDatabase" => array(
"rule" => array("__existingRecord", false),
"message" => "The username you entered has been already registered in our database!"
)
),
"password" => array(
"passwordFieldNotEmpty" => array(
"rule" => "notEmpty",
"message" => "You did not enter your password!"
)
),
"password_confirm" => array(
"passwordConfirmFieldNotEmpty" => array(
"rule" => "notEmpty",
"message" => "You did not confirm your password!"
),
"passwordsMatch" => array(
"rule" => array("__fieldMatch", "password"),
"message" => "The passwords you entered don't match!"
)
),
"email" => array(
"emailFieldNotEmpty" => array(
"rule" => "notEmpty",
"message" => "You did not enter the e-mail!"
),
"emailValid" => array(
"rule" => "email",
"message" => "The e-mail you entered is not valid!"
),
"emailExistsInDatabase" => array(
"rule" => array("__existingRecord", false),
"message" => "The e-mail you entered has been already registered in our database!"
)
)
/*"language" => array(
)*/
)
Here is my register form:
<?php echo $this->Form->create('User', array('url' => array('controller' => 'users', 'action' => 'register')));?>
<fieldset>
<legend><?php __('Add User'); ?></legend>
<?php
echo $this->Form->input('username');
echo $this->Form->input('password', array('type' => 'password', 'value' => ''));//value='' - resets the password input on any error on the page
echo $this->Form->input('password_confirm', array('type' => 'password', 'value' => ''));
echo $this->Form->input('email');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
Now, everytime I submit the form EMPTY, the password field, although empty, passes all validation tests (I tried putting value => '' in the code, but it's useless).
Also, the email input seems to pass the 'notEmpty' test and the error shown is that The email is not valid
I've looked over all my code but couldn't find any solution.
http://pastebin.com/xnQ02BCW
this is what i would use.
About the Password problem: what version of CakePHP are you using?
The Auth component in 1.2 automatically hash your password, so that it will result not empty even if it is. In 1.3 it should be OK but I don't know from what version.
I've managed to do a couple of "hacks" so the problem is solved for now.
Don't think this is the most appropriate way of doing it, but it might get in handy for other users having my problem:
Firstly, in my UsersController, I wrote this snippet, so if the password field is left empty by the user, reset it to '' before validation:
if($this->data['User']['password'] == $this->Auth->password('')){
$this->data['User']['password'] = '';
}
$this->User->set($this->data);
if($this->User->validates()){ //post validation login }
The second problem was the e-mail validation. Oddly, I got this problem fixed by changing the order of the rules in the Multivalidatable Behaviour. So, from:
"email" => array(
"emailFieldNotEmpty" => array(
"rule" => "notEmpty",
"message" => "You did not enter the e-mail!"
),
"emailValid" => array(
"rule" => "email",
"message" => "The e-mail you entered is not valid!"
),
"emailExistsInDatabase" => array(
"rule" => array("__existingRecord", false),
"message" => "The e-mail you entered has been already registered in our database!"
)
)
I now have:
"email" => array(
"emailExistsInDatabase" => array(
"rule" => array("__existingRecord", false),
"message" => "The e-mail you entered has been already registered in our database!"
),
"emailValid" => array(
"rule" => "email",
"message" => "The e-mail you entered is not valid!"
),
"emailFieldNotEmpty" => array(
"rule" => "notEmpty",
"message" => "You did not enter the e-mail!"
)
)
I don't know whether this was intended or not, but it seems like the last rule which isn't fulfilled is the one displayed. To my logic, the rules would have been arranged in their order of appearance, so if the first one doesn't apply, stop checking and display it.
I repeat, I don't know if this is the right approach, but I seem to have worked out these problems.
If you have anything to add up, please do so!
EDIT
Found the 'official' explanation in the Cake 1.3 Book. It says:
By default CakePHP tries to validate a field using all the validation rules declared for it and returns the error message for the last failing rule. But if the key last is set to true for a rule and it fails, then the error message for that rule is returned and further rules are not validated. So if you prefer to show the error message for the first failing rule then set 'last' => true for each rule.
So, another approach to my problem is to set the validation rules in their order of appearance and add a last key to the rule array.
Related
I currently have the following configuration for Authentication component in CakePHP
public $components = array(
'Session',
'Auth' => array(
'authError' => 'Please login to your account',
'logoutRedirect' => array(
'controller' => 'pages',
'action' => 'display',
'home'
),
'authenticate' => array(
'Form' => array(
'passwordHasher' => array(
'className' => 'Simple',
'hashType' => 'sha256'
)
)
),
'authorize' => array('Controller') // Added this line
)
);
And my work is integrated with vb.net windows form application. Is there a way or an Authentication class that can be common between both vb.net & CakePHP?
Password hashing is an irreversible process. You cannot "decrypt" them.
If you want to use the same hashes for authentication in your vb.net code then use the same hashing algo to hash user provided plain password and then compare the hashes. When using password hasher with sha256, the hash is generated by appending security salt to the plain text string and then the resulting string is sha256 hashed. So do the same in your vb.net code.
I am using YII 1.0 logger but i am not able to print YII logs without Yii::app()->end.
Below is my test program :
$test = 123;
if($test){
Yii::log('Test', CLogger::LEVEL_INFO, "This is for testing");
}
Below is my configuration settings :
'log' => array(
'class' => 'CLogRouter',
'routes' => array(
array(
'class' => 'CFileLogRoute',
'levels' => 'trace, info, error, warning, vardump',
),
array(
'class' => 'CWebLogRoute',
'enabled' => YII_DEBUG,
'levels' => 'error, warning, trace, notice',
'categories' => 'application',
'showInFireBug' => true,
),
array(
'class'=>'CFileLogRoute',
'logFile'=>'custom.log',
'categories'=>'custom.*',
),
),
),
Is there something wrong in my code ?
Thanks in advance.
You may learn CLogger api for more useful information.
Yii::getLogger()->flush(true);
Also hope it helps:
http://www.yiiframework.com/forum/index.php/topic/8671-force-message-routing-before-the-end-of-the-application/
http://www.yiiframework.com/forum/index.php?/topic/8158-logger-and-flush-problem/
Messages can be logged using yii::log,
syntax:
Yii::log($message, $level, $category);
example:
Yii::log("This is for testing","error","custom");
I have an odd situation where my add form works perfectly, but the edit form will not even load. Instead I get the following error:
Notice: Undefined property: DoctrineModule\Form\Element\ObjectSelect::$object in /vendor/zendframework/zendframework/library/Zend/Form/Element/Collection.php on line 517
Fatal error: Class name must be a valid object or a string in /vendor/zendframework/zendframework/library/Zend/Form/Element/Collection.php on line 517
I have objects of the class Application\Model\Record which have a many-to-many relationship to objects of the class Application\Model\Subject. I want the Subjects to appear as dropdowns in the Record forms, with the option to add multiple Subjects. So in the RecordFieldset.php I have:
$this->add(array(
'name' => 'subjects',
'type' => 'Zend\Form\Element\Collection',
'options' => array(
'label' => 'Subjects',
'count' => 1,
'should_create_template' => true,
'allow_add' => true,
'allow_remove' => true,
'template_placeholder' => '__subjectindex__',
'target_element' => array (
'name' => 'subject',
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'options' => array(
'object_manager' => $this->objectManager,
'target_class' => 'Application\Model\Subject',
'property' => 'name',
'empty_option' => 'Select a subject',
),
'attributes' => array(
'class' => 'form-control',
),
),
),
));
And like I said, this works perfectly in the Record add form. But it looks like on the edit form it cannot load the Subjects. Has anyone else tried something like this? Or do you have an idea what the error is saying?
Environment:
Zend Framework 2.3.3
Doctrine-orm-module 0.8.0
Doctrine 2.4.2
Looks like this is a bug on ZF2, target elements need to be a fieldset for it to work otherwise it will run into this issue. Use a fieldset to wrap the ObjectSelect.
Hi I'm trying to get data from SugarCRM by REST api. It's fine with default module, but with custom module, I got a issue.
I used this example
And getting data from Contacts module which has last name = "abc" is ok with these code:
$get_entry_list_parameters = array(
'session' => $session_id,
'module_name' => 'Contacts',
'query' => "contacts.last_name='abc'",
'order_by' => "",
'offset' => '0',
'select_fields' => array(
'id'
),
'link_name_to_fields_array' => array(
),
'max_results' => '1',
'deleted' => '0',
'Favorites' => false,
);
When I tried to get data from our custom module (eg: Event Session inside Event package), so it's ok as well without Query param like these code
$get_entry_list_parameters = array(
'session' => $session_id,
'module_name' => 'event_event_session',
'order_by' => "",
'offset' => '0',
'select_fields' => array(
'id',
'name'
),
'link_name_to_fields_array' => array(
),
'max_results' => '3',
'deleted' => '0',
'Favorites' => false,
);
But after that I put Query param, I got no record
$get_entry_list_parameters = array(
'session' => $session_id,
'module_name' => 'event_event_session',
'query' => "event_event_session.name='def'",
'order_by' => "",
'offset' => '0',
'select_fields' => array(
'id',
'name'
),
'link_name_to_fields_array' => array(
),
'max_results' => '3',
'deleted' => '0',
'Favorites' => false,
);
I think the way I used to get data from custom module is not correct.
Anyone can help me resolve this issue. Thank you!
Johnny
Are you sure the table name for the custom module is "event_event_session"?
When we create any new custom module using module builder then it is asking for key name. So our module will become as key_modulename. So make sure you are passing the key name along with module name. So that way you can access the data of custom new module.
Hello everybody and thansk for reading .
Iam making a website just for fun and to learn yii. The website is a app that can be used for reporting pirated links.
I have an input filed in which i provide a link and than i have a dropdown menu from where you can select the type of link you are submitting. Depending on the value you select i want different validation rules to perform on the link submitted. If i was not clear in my explenation feel free to visit www.youtubetv.it , on the main page you will see a input field and a dropdown.
The code is as follows ;
<div class="span4">
<div class="input-prepend"><span class="add-on" style="height: 50px;">
<i class="icon-4x icon-globe" style="line-height: 54px;"></i></span>
<?php
echo $form->textField($model, 'link', array(
'prepend' => '<i class="icon-4x icon-globe" style="line-height: 54px;"></i>',
'class' => 'span12', 'maxlength' => 999,
'style' => 'height:60px;font-size: 22px;width: 400px;',
));
?>
</div>
</div>
<div class="span4 offset1">
<?php
echo $form->dropDownList($model, 'category', CHtml::listData(Lookup::model()->findAll('type="kind"'), 'code', 'name'), array(
'prompt' => 'Select Type',
'class' => 'span12',
'style' => 'height:60px;font-size: 22px;',
));
?>
</div>
Current validation rules in the model
public function rules() {
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('category, link', 'required'),
array('id_user', 'numerical', 'integerOnly' => true),
array('link', 'length', 'max' => 999),
array('link', 'url',
'allowEmpty' => false,
'defaultScheme' => null,
//'pattern' => 'esspressione regolare',
'message' => 'The specified model does not exist.',
'validSchemes' => (array('http', 'https'))
),
array('category, web_page', 'length', 'max' => 255),
array('creation_date', 'default',
'value' => new CDbExpression('NOW()'),
'setOnEmpty' => false,
'on' => 'insert'),
array('id_public_link, category, id_user, link, creation_date', 'safe', 'on' => 'search'),
);
}
I would be grateful if someone could show me an example of how i could validate the "url" if someone selects Movie from the dropdown list.
please feel free to ask for clarification if i was not clear
Yii has so called scenario for validation rules, what you need is to add 'on' key with any scenario name you like as a value in a rule you want. And then set scenario for your model as $model->scenario = 'Your scenario';
e.g.
public function rules() {
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('category, link', 'required'),
array('id_user', 'numerical', 'integerOnly' => true),
array('link', 'length', 'max' => 999),
array('link', 'url',
'allowEmpty' => false,
'defaultScheme' => null,
//'pattern' => 'esspressione regolare',
'message' => 'The specified model does not exist.',
'validSchemes' => (array('http', 'https')),
'on'=>'urlcheck'
),
array('category, web_page', 'length', 'max' => 255),
array('creation_date', 'default',
'value' => new CDbExpression('NOW()'),
'setOnEmpty' => false,
'on' => 'insert'),
array('id_public_link, category, id_user, link, creation_date', 'safe', 'on' => 'search'),
);
}
And then in your action use:
...
$type = isset($_POST['Lookup']['type'])?$_POST['Lookup']['type']:false;
if($type === '1') //as I assume from your website '1' is a Movie
$model->scenario = 'urlcheck';
...
Btw, as you can see you already have a scenario in your rule on a 'creation_date' attribute. Scenation 'insert' is default scenarion for a new records. There more default scenarions in Yii you can learn at here