Giter Site home page Giter Site logo

michalsn / codeigniter-htmx Goto Github PK

View Code? Open in Web Editor NEW
64.0 12.0 12.0 772 KB

HTMX helper library for CodeIgniter 4 framework

Home Page: https://michalsn.github.io/codeigniter-htmx/

License: MIT License

PHP 96.48% JavaScript 3.52%
codeigniter codeigniter4 htmx php8 php

codeigniter-htmx's People

Contributors

datamweb avatar dependabot[bot] avatar jozefrebjak avatar lonnieezell avatar michalsn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

codeigniter-htmx's Issues

TypeError when running Feature tests

This library works great for normal use. However, when running in a feature test I end up getting the following error:

1) Tests\Blog\PagesTest::testIndex
TypeError: Michalsn\CodeIgniterHtmx\Config\Services::request(): Return value must be of type CodeIgniter\HTTP\CLIRequest|Michalsn\CodeIgniterHtmx\HTTP\IncomingRequest, CodeIgniter\HTTP\IncomingRequest returned

/Users/kilishan/WebSites/personal/learn-codeigniter/vendor/michalsn/codeigniter-htmx/src/Config/Services.php:29
/Users/kilishan/WebSites/personal/learn-codeigniter/vendor/codeigniter4/framework/system/Config/BaseService.php:253
/Users/kilishan/WebSites/personal/learn-codeigniter/vendor/codeigniter4/framework/system/Config/Services.php:285
/Users/kilishan/WebSites/personal/learn-codeigniter/vendor/codeigniter4/framework/system/Config/BaseService.php:253
/Users/kilishan/WebSites/personal/learn-codeigniter/vendor/codeigniter4/framework/system/Test/FeatureTestTrait.php:182
/Users/kilishan/WebSites/personal/learn-codeigniter/vendor/codeigniter4/framework/system/Test/FeatureTestTrait.php:219
/Users/kilishan/WebSites/personal/learn-codeigniter/tests/Blog/PagesTest.php:32

I have changed the IncomingRequest use statement in the BaseController as recommended in the README with no change.

A simple test would be:

public function testIndex()
{
    $result = $this->get('blog');

    $result->assertOK();
    $result->assertSee('Latest Posts');
}

Minimum requirements

Hello
just a suggestion, maybe write minimum requirements for the installation environment
e.g. PHP minimum supported version

Also, in the installation of the examples, maybe a more verbose installation guide for "dummies" that are new to Codeigniter .

Why not to allow them (I include myself to the list) to start directly with a better UX?

Thank you
Robert

Debug toolbar integration

Not really an issue but how about just advising users to include the HTMX Ajax-header extension and set hx-ext=“ajax-header” to body tag then you don’t have to change Toolbar::prepare and create a services::Toolbar function to make it work as it is supposed to?

view_fragment returns whole page: what am I doing wrong?

I am attempting to implement form field validation that would mimic client-side validation, but view_fragment is refusing to return only a fragment of the view :(

Here is my controller:

<?php

namespace App\Controllers;

use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;

class Fragment extends BaseController
{

    private $validation;

    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        // Do Not Edit This Line
        parent::initController($request, $response, $logger);
        $this->validation =  \Config\Services::validation();
        helper('form');
    }

    public function create()
    {

        $this->data['title'] = 'Test with fragments';
        $this->data['validation'] = $this->validation;

        $this->data['includes'] = ['htmx'];
        return view('fragment_form_view', $this->data);
    }

    private function returnRules()
    {
        return [
            'first_name' => ['label' => 'Name', 'rules' => 'required|min_length[5]'],
            'last_name' => ['label' => 'Surname', 'rules' => 'required|min_length[5]'],
        ];
    }

    public function validateField(string $fieldName): string
    {

        $this->validation->setRules($this->returnRules($fieldName));
        $this->validation->withRequest($this->request)->run();

        if ($this->validation->hasError($fieldName)) {
            $this->data['errors'] = $this->validation->getErrors();
        }
        
        $this->data[$fieldName] = $this->request->getPost($fieldName);
        
        $this->data['title'] = '';
        $this->data['validation'] = $this->validation;

        return view_fragment('fragment_form_view', $fieldName, $this->data);
    }
}

And this is my view file fragment_form_view.php:

<?= $this->extend('common/default_layout') ?>

<?= $this->section('content') ?>

<h1 class="mt-4"><?php echo $title; ?></h1>

<ol class="breadcrumb mb-4">
    <li class="breadcrumb-item">Fragmentai</li>
    <li class="breadcrumb-item active"><?php echo $title; ?></li>
</ol>

<div class="container" style="max-width:500px;">

    <form action="<?= site_url('fragment/save') ?>" method="post">

        <div class="row">
            <div id="first_name" class="col-12">
                <?php $this->fragment('first_name'); ?>
                <div class="mb-3">
                    <label class="form-label" for="first_name">Name</label>
                    <input hx-target="#first_name" hx-post="<?= site_url('fragment/validateField/first_name') ?>" type="text" class="form-control" name="first_name" value="<?php old('first_name'); ?>">
                    <?= $validation->showError('first_name') ?>
                </div>
                <?php $this->endFragment(); ?>
            </div>


            <div id="last_name" class="col-12">
                <?php $this->fragment('last_name'); ?>
                <div class="mb-3">
                    <label class="form-label" for="last_name">Surname</label>
                    <input hx-target="#last_name" hx-post="<?= site_url('fragment/validateField/last_name') ?>" type="text" class="form-control" name="last_name" value="<?php old('last_name'); ?>">
                    <?= $validation->showError('last_name') ?>
                </div>
                <?php $this->endFragment(); ?>
            </div>

            <div class="col-12">
                <div class="mb-3">
                    <button type="submit" class="btn btn-success">Send</button>

                </div>
            </div>
        </div>

    </form>
</div>

<?= $this->endSection() ?>

They are messy and incomplete, but the code should return view fragment on blur of a form field. Instead, it returns the whole page (and messes up the display of the form. What am I doing wrong? Or is it a bug in the ci-htmx lib?

Call to an undefined method CodeIgniter\HTTP\CLIRequest|CodeIgniter\HTTP\IncomingRequest::isHtmx().

@michalsn Thank you for a perfect library. I'm just exploring what is possible to do with HTMX, till the library exists I used $this->requst->isAjax() method with Ajax-Header htmx extension.

Now I installed this library and changed isAjax() to isHtmx(). It works as excepted, but now PHPStan is reporting issue with:

Call to an undefined method CodeIgniter\HTTP\CLIRequest|CodeIgniter\HTTP\IncomingRequest::isHtmx().  

So for now I'm just ignoring this error with ignore-next-line PHPDoc.

Psalm is reporting this issue with:

ERROR: UndefinedMethod - modules/Recycler/Controllers/RecycleController.php:59:29 - Method CodeIgniter\HTTP\CLIRequest::isHtmx does not exist (see https://psalm.dev/022)

This error is reported, because in BaseController is defined default:

use CodeIgniter\HTTP\IncomingRequest;

for

    /**
     * Instance of the main Request object.
     *
     * @var CLIRequest|IncomingRequest
     */
    protected $request;

So, easy solution is to change default IncomingRequst for:

use Michalsn\CodeIgniterHtmx\HTTP\IncomingRequest;

or suggest users to use some other variable than $request to use with all of defined functions in README.

@michalsn What you think ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.