Welcome Guest, Not a member yet? Register   Sign In
The 'matches' validation rule fails even though I'm sure its correct
#4

(This post was last modified: 06-09-2021, 09:10 PM by InsiteFX.)

[quote pid="387484" dateline="1623240301"]
donnot for got entity 

look at my code    model controler entity  and view 

PHP Code:
<?php namespace CoreCommon\Controllers;


use 
CoreCommon\Entities\UsersEntity;
use 
CoreCommon\Libraries\CustomFileSystem;
use 
CoreCommon\Models\UsersModel;
use 
CodeIgniter\HTTP\ResponseInterface;


class 
Profile extends ApiController
{

    /**
    * index function
    * @method : GET
    */
    public function index()
    {

        $this->setupAuthClasses();
        $userModel = new UsersModel();
        $user $userModel->where('id'$this->userId)->get()->getResultObject();

        return $this->respond([
            'data' => $user
        
], ResponseInterface::HTTP_OKlang('Common.api.receive'));
    }


    public function create()
    {


        $userModel = new UsersModel();
        $userEntity = new UsersEntity();
        $usersMythAuth = new \Myth\Auth\Models\UserModel();
        $isPassword false;
        $customConfig = new \CoreCommon\Config\CoreAppConfig();
        $imageService = \CodeIgniter\Config\Services::image();
        $handyFunction = new CustomFileSystem();

        if ($this->request) {
            //get request from Vue Js

            $rules = [
                'first_name' => 'if_exist|required|max_length[255]',
                'last_name' => 'if_exist|required|max_length[255]',
                'address' => 'if_exist|required|max_length[255]',
                'phone' => 'if_exist|required|max_length[11]',
                'password' => 'if_exist|required',
                'pass_confirm' => 'if_exist|required|matches[password]',
                'gender' => 'if_exist|required',
                'country' => 'if_exist|required|max_length[255]',
                'city' => 'if_exist|required|max_length[255]',
                'image' => 'if_exist|uploaded[image]|max_size[image,4096]|ext_in[avatar,png,jpg,jpeg,webp]',

            ];

            if (!$this->validate($rules)) {

                return $this->respond([
                    'error' => $this->validator->getErrors(),
                    'success' => false
                
], ResponseInterface::HTTP_NOT_ACCEPTABLElang('Common.api.validation'));

            }

            $user $usersMythAuth->where('id'$this->userId)
                ->first();
            if (isset($this->request->getJSON()->password)) {

                $user->password $this->request->getJSON()->password;
                $user->reset_hash null;
                $user->reset_at null;
                $user->reset_expires null;
                $user->force_pass_reset false;
                $isPassword true;

            } else if (isset($this->request->getJSON()->first_name)) {

                $userEntity->first_name $this->request->getJSON()->first_name;
                $userEntity->last_name $this->request->getJSON()->last_name;
                $userEntity->gender $this->request->getJSON()->gender;
            } else if (isset($this->request->getJSON()->address)) {
                $userEntity->address $this->request->getJSON()->address;
                $userEntity->country $this->request->getJSON()->country;
                $userEntity->city $this->request->getJSON()->city;

            }

            if (isset($_FILES['image'])) {
                $path $userModel->where('id'$this->userId)
                    ->first();
                $handyFunction->removeSingleFile(ROOTPATH $path->image);
                $avatar $this->request->getFile('image');
                $avatar->move($customConfig->uploadDirectory '/profile'time() . '.' $avatar->getClientExtension());
                $userEntity->image $avatar->getName();
                $userEntity->editImage();
                $imageService->withFile(ROOTPATH $userEntity->image)
                    ->withResource()->fit(100100'center')
                    ->save(ROOTPATH $userEntity->image90);
            }

            if (is_null($user->phone) && isset($this->request->getJSON()->phone)) {
                $userEntity->phone $this->request->getJSON()->phone;

            } else if (is_null($user->email) && isset($this->request->getJSON()->email)) {
                $userEntity->email $this->request->getJSON()->email;
            }


            if ($isPassword == true) {
                if (!$usersMythAuth->save($user)) {

                    return $this->respond([
                        'data' => $usersMythAuth->errors(),
                        'success' => false,
                    ], ResponseInterface::HTTP_BAD_REQUESTlang('Common.api.reject'));

                }
            } else {
                if (!$userModel->update($this->userId$userEntity)) {

                    return $this->respond([
                        'data' => $userModel->errors(),
                        'success' => false,
                    ], ResponseInterface::HTTP_BAD_REQUESTlang('Common.api.reject'));

                }
            }

            return $this->respond([
                'success' => true,

            ], ResponseInterface::HTTP_CREATEDlang('Common.api.save'));
        }


    }


}




<?
php namespace CoreCommon\Entities;

use \
CodeIgniter\Entity;
use 
CodeIgniter\I18n\Time;

class  UsersEntity extends Entity
{

    protected $id;
    protected $first_name;
    protected $last_name;
    protected $phone;
    protected $image;
    protected $address;
    protected $password_hash;
    protected $gender;
    protected $country;
    protected $city;
    protected $email;

    protected $attributes = [
        'id' => null,
        'first_name' => null,
        'last_name' => null,
        'phone' => null,
        'email' => null,
        'address' => null,
        'image' => null,
        'password_hash' => null,
        'gender' => null,
        'country' => null,
        'city' => null,
        'reset_hash' => null,
        'reset_at' => null,
        'reset_expires' => null,
        'force_pass_reset' => null,
    ];
    protected $datamap = [
    ];

    protected $dates = [];

    protected $casts = [];

    protected $permissions = [];

    protected $roles = [];

    public function setPassword()
    {
        $config = new \Myth\Auth\Config\Auth();

        if (
            (defined('PASSWORD_ARGON2I') && $config->hashAlgorithm == PASSWORD_ARGON2I)
            ||
            (defined('PASSWORD_ARGON2ID') && $config->hashAlgorithm == PASSWORD_ARGON2ID)
        ) {
            $hashOptions = [
                'memory_cost' => $config->hashMemoryCost,
                'time_cost' => $config->hashTimeCost,
                'threads' => $config->hashThreads
            
];
        } else {
            $hashOptions = [
                'cost' => $config->hashCost
            
];
        }

        $this->attributes['password_hash'] = password_hash(
            base64_encode(
                hash('sha384'$this->password_hashtrue)
            ),
            $config->hashAlgorithm,
            $hashOptions
        
);

        /*
            Set these vars to null in case a reset password was asked.
            Scenario:
                user (a *dumb* one with short memory) requests a
                reset-token and then does nothing => asks the
                administrator to reset his password.
            User would have a new password but still anyone with the
            reset-token would be able to change the password.
        */

        $this->attributes['reset_hash'] = null;
        $this->attributes['reset_at'] = null;
        $this->attributes['reset_expires'] = null;
        $this->attributes['force_pass_reset'] = false;


    }

    public function editImage()
    {


        $this->attributes['image'] = 'public/upload/profile/' $this->attributes['image'];

        return $this;
    }
}




<?
php namespace CoreCommon\Models;

use 
CoreCommon\Entities\UsersEntity;
use 
CodeIgniter\Model;

class  UsersModel extends Model
{


    /**
    * table name
    */
    protected $primaryKey "id";
    protected $table "users";

    /**
    * allowed Field
    */
    protected $allowedFields = [
        'first_name',
        'last_name',
        'image',
        'address',
        'phone',
        'email',
        'password_hash',
        'gender',
        'country',
        'city',
        'reset_hash',
        'reset_at',
        'reset_expires',
        'force_pass_reset',
    ];

    protected $returnType UsersEntity::class;
    protected $validationRules = [
        'first_name' => 'if_exist|required|max_length[255]',
        'last_name' => 'if_exist|required|max_length[255]',
        'image' => 'if_exist|required|max_length[255]',
        'address' => 'if_exist|required|max_length[255]',
        'phone' => 'if_exist|required|max_length[11]',
        'email'  => 'if_exist|required|valid_email|is_unique[users.email]',
        'password_hash' => 'if_exist|required',
        'gender' => 'if_exist|required',
        'country' => 'if_exist|required|max_length[255]',
        'city' => 'if_exist|required|max_length[255]',
    ];
    protected $validationMessages = [];
    protected $skipValidation false;



Enlightenment  Is  Freedom
Reply


Messages In This Thread
RE: The 'matches' validation rule fails even though I'm sure its correct - by paliz - 06-09-2021, 10:54 AM



Theme © iAndrew 2016 - Forum software by © MyBB