A
A
Anton2021-01-31 13:35:17
Kohana
Anton, 2021-01-31 13:35:17

Validation, output of standard framework errors, how to replace them?

Hello everyone, changing the password on the site was not done by me, before that the programmer.

I want to remove the framework errors from the action, how can I directly enter the text of the error output in the action.
At the moment, it displays errors in standard framework messages - validation.php

It was possible, of course, to replace the text there, but there are standard fields in which the text can work for many fields, I tried to make a new one, but it did not work.

Framework ko7, kohana, but I don't think that's what the framework is about.

I want to replace the output of errors, that is, output not from a file, but somehow directly enter directly into the action, tell me how to implement it.

Password change action. (implemented like this)

public function action_retypepassword()
  {
    $token = Arr::get($_REQUEST, 'token', '');

    if (!is_string($token))
      $token = '';
    else
      $token = trim($token);

    if (!preg_match('/^[0-9a-f]{32}$/', $token))
      throw new http_exception_404();

    $tokenExists = ORM::factory('User')->where('remember_pass_token', '=', $token)->count_all();

    if (!$tokenExists)
      throw new http_exception_404();

    if ($this->request->method() === request::POST) {

      $data = Arr::extract($_POST, ['password', 'password_confirm']);

      foreach ($data as &$row) {
        if (!is_string($row))
          $row = '';
        else
          $row = trim($row);
      }

      $valid = Validation::factory($data)
        ->rule('password', 'not_emptyp')
        ->rule('password', 'min_length', [':value', 6])
        ->rule('password_confirm', 'not_emptyp')
        ->rule('password_confirm', 'matches', [':validation', 'password', 'password_confirm']);

      $errors = [];

      if (!$valid->check())
        $errors = $valid->errors('');

      if (!count($errors)) {
        $user = ORM::factory('User')->where('remember_pass_token', '=', $token)->find();
        $user->password = $data['password'];
        $user->remember_pass_token = DB::expr('NULL');
        $user->save();
        $success = true;
      }
    }

    $this->template->content = View::factory('index/regaut/retypePassword', [
      'token' => $token,
      'errors' => isset($errors) ? $errors : [],
      'success' => isset($success) ? $success : false,
    ]);
  }


These fields take error text from messages - validation.php
->rule('password', 'not_emptyp')
        ->rule('password', 'min_length', [':value', 6])
        ->rule('password_confirm', 'not_emptyp')
        ->rule('password_confirm', 'matches', [':validation', 'password', 'password_confirm']);


In general, how can you replace these fields and not take the validation text from the framework file.
I would like to enter the text right there in the code that should be displayed for any error.

I tried to add there: (but it throws an error).
if (empty($data['password'])) {
        throw new http_exception_404('Введите новый пароль');
      }


Or like this, it doesn't work.
if (!empty($data['password'])) {
        if (mb_strlen($data['password']) < 5 or mb_strlen($data['password']) > 32) {
          throw new Exception('Пароль должен быть не менее 5 и не более 32 символов');
        }
      }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question