Codeigniter 4 -表格确认

g52tjvyc  于 2023-02-01  发布在  其他
关注(0)|答案(2)|浏览(99)

我正在使用CodeIgniter 4的验证表单,我已经检查了帖子是否到来,但每次都收到FALSE,请查看我的代码。

function login(){   
    
    helper(['form', 'url']);
    
    $validation =  \Config\Services::validation();

    $validation->setRules([
        'email' => ['label' => 'E-mail', 'rules' => 'required'],
        'password' => ['label' => 'Senha', 'rules' => 'required|min_length[6]']
    ]);

    var_dump($this->validate($validation) ? true : false);exit;

}

我也试过了,

$rules = [
        'email' => ['label' => 'E-mail', 'rules' => 'required'],
        'password' => ['label' => 'Senha', 'rules' => 'required|min_length[6]']
    ];

var_dump($this->validate($rules) ? true : false);exit;
wfauudbj

wfauudbj1#

看看我的密码

<?php

namespace Modules\Common\Controllers;

use Modules\Common\Config\Services;
use Modules\Common\Entities\AdvertisementEntity;
use CodeIgniter\HTTP\ResponseInterface;
use Modules\Shared\Controllers\ApiController;

class Advertisement extends ApiController
{

    /**
     * index function
     * @method : GET
     */
    public function index()
    {
        $advertisementEntity = new AdvertisementEntity();
        $this->urlQueryParam->dataMap($advertisementEntity->getDataMap());

        $advertisementService = Services::advertisementService();
        $findAllData = $advertisementService->index($this->urlQueryParam);

        return $this->respond([
            'data' => $findAllData['data'],
            'pager' => $findAllData['pager']
        ], ResponseInterface::HTTP_OK, lang('Shared.api.receive'));

    }

    /**
     * show function
     * @method : GET with params ID
     */
    public function show($id = null)
    {

        $advertisementService = Services::advertisementService();
        $findOneData = $advertisementService->show($id);

        return $this->respond([
            'data' => $findOneData['data'],
            'pager' => $findOneData['pager']
        ], ResponseInterface::HTTP_OK, lang('Shared.api.receive'));

    }

    /**
     * create function
     * @method : POST
     */
    public function create()
    {

        $rules = [
            'name' => 'required|min_length[3]|max_length[255]',
            'link' => 'required',

        ];

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

            return $this->respond([
                'error' => $this->validator->getErrors(),

            ], ResponseInterface::HTTP_NOT_ACCEPTABLE, lang('Shared.api.validation'));

        };

        $advertisementEntity = new AdvertisementEntity((array)$this->request->getVar());
        $advertisementEntity->enableStatus()->createdAt();
        $advertisementService = Services::advertisementService();
        $advertisementService->create($advertisementEntity);

        return $this->respond([
            'insertId' => $advertisementService->getInsertID()
        ], ResponseInterface::HTTP_CREATED, lang('Shared.api.save'));

    }

    /**
     * update function
     * @method : PUT or PATCH
     */
    public function update($id = null)
    {

        if ($this->request) {
            //get request from Vue Js
            $json = $this->request->getJSON();
            if (!isset($id)) {
                $id = $json->id;
            }

            $rules = [
                'name' => 'required|min_length[3]|max_length[255]',
                'link' => 'required',

            ];

            if (!$this->validate($rules)) {
                return $this->respond([
                    'error' => $this->validator->getErrors(),

                ], ResponseInterface::HTTP_NOT_ACCEPTABLE, lang('Shared.api.validation'));

            }

            $advertisementEntity = new AdvertisementEntity((array)$this->request->getVar());
            $advertisementEntity->updatedAt();

            $advertisementService = Services::advertisementService();
            $advertisementService->update($id, $advertisementEntity);

        }
        return $this->respond([
        ], ResponseInterface::HTTP_OK, lang('Shared.api.update'));

    }

    /**
     * edit function
     * @method : DELETE with params ID
     */
    public function delete($id = null)
    {

        $advertisementService = Services::advertisementService();
        $advertisementService->delete($id);

        return $this->respond([
        ], ResponseInterface::HTTP_OK, lang('Shared.api.remove'));

    }

}
daolsyd0

daolsyd02#

但前提是:

public function save()
    {  
        $validation =  \Config\Services::validation();  
        $validation->setRules(
    [
        'sap' => [
            'label'  => 'sap',
            'rules'  => 'required|is_unique[hizmetsat.sap]',
            'errors' => [
                'required' => 'Sap No boş bırakılamaz.',
                'is_unique' => 'Bu Sap numarası zaten kayıtlıdır.',
            ],
        ],
        'btarih' => [
            'label'  => 'btarih',
            'rules'  => 'required',
            'errors' => [
                'required' => 'Başlama tarihi boş bırakılamaz.',
                
            ],
        ],
    ]
);
if (!$validation->withRequest($this->request)->run()) {
    echo view('Admin/Hizmet/Hizmet_form', $data,  ['errors' => $validation->getErrors()]);
        }
        else {
     $this->data['request'] = $this->request;
     $post = [
         'sap' => $this->request->getPost('sap'),
         'sas' => $this->request->getPost('sas'),
         'sirket' => $this->request->getPost('sirket'),
         'adi' => $this->request->getPost('adi'),
         'tilgili' => $this->request->getPost('tilgili'),
         'firma' => $this->request->getPost('firma'),
         'aciklama' => $this->request->getPost('aciklama'),
         'hyeri' => $this->request->getPost('hyeri'),
         'btarih' => $this->request->getPost('btarih'),
         'durum' => $this->request->getPost('durum'),
         'odurum' => $this->request->getPost('odurum'),
         'fdurum' => $this->request->getPost('fdurum')
        
     ];
     $save = $this->Hizmetmodels->insert($post);
     return redirect()->to('/Hizmet/Hizmet_bakim')->with('success', 'Tebrikler! <br> Kayıt başarı ile tamamlandı.');     
}
        
    }

相关问题