CakePHP 3 - How to apply validation conditionally? - validation

Trying to restrict a compareWith to just on create
$validator->add('password', [
'compare' => [
'rule' => ['compareWith', 'password_confirmation']]]);
I cant seem to work out how to do it added 'create' to the end like
$validator->add('password', [
'compare' => [
'rule' => ['compareWith', 'password_confirmation']]],'create');
in fact i have tried the 'create' in many places either build errors or still validating on an edit
Appreciate any help

Try this:
$validator->add("password", "compare", [
"rule" => ["compareWith", "password_confirmation"],
"message" => __("Password and password confirmation fields don't match."),
"on" => "create"
]);
This will certainly work.
Hope this helps.
Peace! xD

Related

Nexmo Laravel connect to PSTN endpoint not working

My code is shown below. Everything works for me, besides connecting a call to a PSTN endpoint. I'm thinking possibly that maybe the "connect" functions aren't included in the Laravel Nexmo packages that I'm using. I'm using these:
https://github.com/Nexmo/nexmo-laravel
Which is built on another nexmo package:
https://github.com/Nexmo/nexmo-php
My code:
public function getNexmoAnswer(Request $request)
{
return
[
[
'action' => 'talk',
'voiceName' => 'Justin',
'text' => 'Welcome to our great site’
],
[
'action' => 'talk',
'voiceName' => 'Justin',
'text' => "Press # to search now the e.t.a status of your latest order.",
'bargeIn' => true
],
[
'action' => 'connect',
'from' => '17181234567’,
'endpoint' => [
'type' => 'phone',
'number' => '18451234567’
]
]
];
}
The connect action is part of Nexmo's NCCO system and is unrelated to Nexmo-Laravel and Nexmo-PHP. Your NCCO looks to be correct to me.
To connect to another number, you'll need to have activated your account by adding credit. In addition, the from number you're using (17181234567) must be a number purchased from Nexmo

how to stop execution of ctp file in cakephp 2.x after validating the url

In my CakePHP application, I have applied Url validations so that admin can access only those actions which are defined for admin and same as with users.
In my application, "surveylist" is the action of admin and when any user directly access that action(surveylist), URL validations work(Unauthorized access msg is displayed).
But below that message ctp file of surveylist executes forcefully and show errors because I have validated URL through the try-catch block and it cannot get the set variables of action.
I want that ctp file should not execute if unauthorize error comes.
My code for surveylist is:-
public function surveylist($pg=null){
try{
if($this->checkPageAccess($this->params['controller'] . '/' . $this->params['action'])){
$this->Paginator->settings = array(
'Survey' => array(
'limit' => 5,
'order' => 'created desc',
'conditions'=>array('is_deleted'=> 0),
'page' => $pg
)
);
$numbers = $this->Paginator->paginate('Survey');
$this->set(compact('numbers'));
}else{
$this->Flash->set(__('Unauthorised access'));
}
}catch(Exception $e){
$this->Flash->set(__($e->getMessage()));
}
}
I don't want the ctp file of surveylist to execute if control comes to else.
Plz, help me out......
Thanx in advance...
I suppose you are using prefix to separate admin and users, if not please do that it is great way to handle and restrict methods.
After doing that you have to make condition to check which prefix(admin, user) is currently active and according that load Auth component and allow action in allow() method of Auth.
Example:
$this->loadComponent('Auth',[
/*'authorize' => [
'Acl.Actions' => ['actionPath' => 'controllers/']
],*/
'loginRedirect' => [
'controller' => 'Users',
'action' => 'index'
],
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password'
]
]
],
'loginAction' => [
'controller' => 'Users',
'action' => 'login'
],
'unauthorizedRedirect' => [
'controller' => 'Users',
'action' => 'login',
'prefix' => false
],
'authError' => 'You are not authorized to access that location.',
]);
if ($this->request->params['prefix']=='admin') {
// Put actions you want to access to admin in allow method's array
$this->Auth->allow(array('add', 'edit', etc...));
} else if ($this->request->params['prefix']=='user') {
// Put actions you want to access to user in allow method's array
$this->Auth->allow(array('login', 'view', etc...));
}
This way you can restrict actions for particular role.
Hope this helps!

How to override needed messages from core messages in yii2

In yii2, i want to override some core messages translation, example:
in #yii/messages/vi/yii.php, has key => translated message:
'Update' => 'Sửa'
but in my application, i want to change this as:
'Update' => 'Cập nhật'
I have created the file: #app/messages/vi/yii.php, has only one the message which need to override:
return [
'Update' => 'Cập nhật'
];
in my main.php config, I added this to components:
'i18n' => [
'translations' => [
'yii' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#app/messages'
],
],
],
It works but just only for the override messages, others of core don't work.
I think you should copy yii.php from core to #common/messages/<your language>/yii.php and edit it. It should be work stable.
Try somehing like this
'i18n'=>[
'yii'=>[
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => "#vendor/yiisoft/yii2/messages",
'sourceLanguage' => 'en_US', // put your language here
'fileMap' => [
'yii'=>'yii.php',
]
]
]
],
Might be outdated thread but I'm putting this in case someone hits this in a search results as I did.
This works out for me :
// get the default translation file for desired language
$yii = require \Yii::getAlias('#vendor/yiisoft/yii2/messages/az/yii.php');
// just override what ever you want
return yii\helpers\ArrayHelper::merge( $yii,
[
'update' => 'myUpdateString',
// ... and so on
]);

CakePHP 3 cell_cache configuration

I have the following code:
$cell = $this->cell('Admin/Notifications', ['since' => $user['last_login']], [
'cache' => ['config' => 'cell_cache', 'key' => 'notifications_' . $user['id']]
]);
echo $cell;
That i use to call a cell in CakePHP 3 and cache it at the same time. But it outputs an error:
Warning (512): Could not render cell - The "cell_cache" cache configuration does not exist. [CORE\src\View\Cell.php, line 244]
What am I missing? I have searched the manual but have not found if i must declare this configuration and where.
Thank you up front for your answers.
I have found the answer. In "config/app.php" I have added the following code to the "Cache" adapters:
'cell_cache' => [
'className' => 'File',
'prefix' => 'myapp_cell_cache_',
'path' => CACHE . 'persistent/',
'serialize' => true,
'duration' => '+10 minutes',
],
So it was something I have missed to configure, but it is not specified in the docs. I think it should be added.

How to use DbMessageSource in yii2

i am using yii2 advanced template.and i dont understand how to use DbMessageSource.i read the guide and i created two tables source_message and message and i wrote in my common/config/main.php file this code
'*'=> [
'class' => 'yii\i18n\DbMessageSource',
'sourceMessageTable'=>'{{%source_message}}',
'messageTable'=>'{{%message}}',
'enableCaching' => true,
'cachingDuration' => 3600
],
and what i have to write in brackets when i using <?= Yii::t()?>
P.S. i am also changed language in my config.
P.P.S. i generated models and cruds for this tables
try this:
'i18n' => [
'translations' => [
'*' => [
'class' => 'yii\i18n\DbMessageSource',
'forceTranslation'=>true,
]
],
],
set parameter forceTranslation as true. This trick helps me.

Resources