Could not find validation handler checkCurrentPassword for current_password - php

I am getting error like this
Warning (512): Could not find validation handler checkCurrentPassword
for current_password
[CORE/Cake/Model/Validator/CakeValidationRule.php, line 281]
my User.php
public function validate_passwords() {
return check( $this->data[$this->alias]['confirm_password'], $this->data[$this->alias]['password']);
}

You can not access check() like this beacause it is a protected method
for more info see : http://api.cakephp.org/3.0/class-Cake.Validation.Validation.html
don't you try something like below :
public function validate_passwords() {
return array('check' => array($this->data[$this->alias]['confirm_password'], $this->data[$this->alias]['password']));
}
To validate confirm_password with password add this rule:
$validator->add('confirm_password', 'no-misspelling', [
'rule' => ['compareWith', 'password'],
'message' => 'Passwords are not equal',
]);

you can use this for validate confirm_password with password
public function validate_passwords()
{
return $this->data[$this->alias]['password'] === $this->data[$this->alias]['confirm_password'];
}
its work for you.

Related

How to change the default message in Respect Validation?

I use Respect Validation for password matches on a Slim app:
class PasswordController extends Controller
{
;
;
public function postChangePassword($request, $response) {
$validation = $this->validator->validate($request, [
'password_old' => v::noWhitespace()->notEmpty()->matchesPassword($this->auth->user()->password),
'password' => v::noWhitespace()->notEmpty()
]);
if($validation->failed()) {
// stay on the same page
}
die('update password');
}
}
I can authenticate the password:
class MatchesPassword extends AbstractRule
{
protected $password;
public function __construct($password) {
$this->password = $password;
}
public function validate($input) {
// compare the non-hashed input with the already hashed password
}
}
...and I created my own custom string for the 3rd rule ('password_old'):
class MatchesPasswordException extends ValidationException
{
public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => 'Password does not match.',
],
];
}
The script works fine, I get the following message when I submit with 'password_old' field empty:
"Password_old must not be empty"
I would like to change the above default message to a custom string, e.g.:
"The value must not be empty"
You can overwrite the messages using the findMessages method of ValidationException and using assert:
try {
v::noWhitespace()->notEmpty()->matchesPassword($this->auth->user()->password)->assert($request->getParam('password_old'));
v::noWhitespace()->notEmpty()->assert($request->getParam('password'));
} catch (ValidationException $exception) {
$errors = $exception->findMessages([
'notEmpty' => 'The value must not be empty'
]);
print_r($errors);
}

For Laravel, How to return a response to client in a inner function

I'm building an api using laravel, the issue is when the client requests my api by calling create() function, and the create()function will call a getValidatedData() function which I want to return validation errors to the client if validation fails or return the validated data to insert database if validation passes, my getValidatedData function is like below so far
protected function getValidatedData(array $data)
{
// don't format this class since the rule:in should avoid space
$validator = Validator::make($data, [
'ID' => 'required',
'weight' => 'required',
]);
if ($validator->fails()) {
exit(Response::make(['message' => 'validation fails', 'errors' => $validator->errors()]));
}
return $data;
}
I don't think exit() is a good way to return the errors message to clients. are there any other ways I can return the laravel Response to clients directly in an inner function. use throwing Exception?
This was what worked for me in Laravel 5.4
protected function innerFunction()
{
$params = [
'error' => 'inner_error_code',
'error_description' => 'inner error full description'
];
response()->json($params, 503)->send();
}
What you can do is using send method, so you can use:
if ($validator->fails()) {
Response::make(['message' => 'validation fails', 'errors' => $validator->errors()])->send();
}
but be aware this is not the best solution, better would be for example throwing exception with those data and adding handling it in Handler class.
EDIT
As sample of usage:
public function index()
{
$this->xxx();
}
protected function xxx()
{
\Response::make(['message' => 'validation fails', 'errors' => ['b']])->send();
dd('xxx');
}
assuming that index method is method in controller you will get as response json and dd('xxx'); won't be executed
You can use this method
public static function Validate($request ,$rolse)
{
// validation data
$validator = Validator::make($request->all(),$rolse);
$errors = $validator->getMessageBag()->toArray();
$first_error = array_key_first($errors);
if (count($errors) > 0)
return 'invalid input params , ' . $errors[$first_error][0];
return false;
}
in controller :
$validate = ValidationHelper::Validate($request,
['title' => 'required']);
if ($validate)
return response()->json(['message' =>'validation fails' , 'error'=> $validate], 403);

How to use email validation inside a custom validation function?

I have created a custom validation function. Now I want to validate email inside that function, how can I do that?
Below is my code:
public function rules()
{
return [
['email', 'filter', 'filter' => 'trim'],
['email', 'required'],
['email', 'validateEmail'],
];
}
public function validateEmail($attribute, $params) {
if(ctype_digit($this->email)){
if(strlen($this->email)!=10)
$this->addError($attribute,'Phone number should be of 10 digits');
else{// Email validation using emailvalidator.}
}
}
You can call the email validator directly. The message is the error message that will be added to the model when validateAttribute is used. If you want to use the default error message you can leave it out:
public function validateEmail($attribute, $params) {
...
else {
$emailValidator = new \yii\validators\EmailValidator();
$emailValidator->message = "Invalid Email";
$emailValidator->validateAttribute($this, $attribute));
}
}

Numeric Custom validation Message in Laravel 5.2

I am writing the following code server side to validate if the role is selected or not ...
public function rules()
{
return [
'RoleID' => 'required|integer|min:1',
];
}
public function messages() {
return [
'User.RoleID' => 'Please select role.',
];
}
Current Message
The role i d must be at least 1.
Expected Message
Please select role.
Can you please guide me to right path?
try this
public function messages () {
return [
'RoleID.min' => 'Please select role.',
];
}

validation rule in yii

The following is the validation rule
public function rules()
{
return array(
// username and password are required
array('oldPassword', 'required'),
array('oldPassword', 'authenticate'),
....
);
}
public function authenticate($attribute,$params)
{
$this->userModel=User::model()->findByPk(Yii::app()->user->id);
if($this->userModel!=null){
if(!$this->userModel->validatePassword($this->oldPassword))
$this->addError($attribute, "Incorrect current password");
}
}
Everything works fine but the problem lies here... when I keep the oldPassword blank both the validation error for "required" & "authentication' are shown whereas I want to show the error msg for the first one,if not blank then the later.
Add a condition in authenticate() to only validate if oldPassword is not empty:
public function authenticate($attribute, $params) {
if ($this->oldPassword) {
...
}
}

Categories