# Laminas component form validator RecordExists, what's the best way to pass dbadapter into?

**URL:** <https://discourse.laminas.dev/t/laminas-component-form-validator-recordexists-whats-the-best-way-to-pass-dbadapter-into/2364>\
**Category:** Components & MVC\
**Tags:** laminas-db, laminas-validator\
**Created:** [June 17, 2021, 5:12am UTC](https://discourse.laminas.dev/t/laminas-component-form-validator-recordexists-whats-the-best-way-to-pass-dbadapter-into/2364 "2021-06-17T05:12:03Z")\
**Posts on this page:** 18\
**Page:** 2

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [June 18, 2021, 6:04am UTC](https://discourse.laminas.dev/t/laminas-component-form-validator-recordexists-whats-the-best-way-to-pass-dbadapter-into/2364/21 "2021-06-18T06:04:48Z")

</div>

The configurations of the modules looks good.

* * *

Do you override the standard database adapter?

> The delegator tries to fetch a database adapter via the name `Laminas\Db\Adapter\AdapterInterface` from the service container and sets the adapter to the requested service. The adapter itself must be an instance of `Laminas\Db\Adapter\Adapter` .

Another option is to debug the delegator class for the database adapter:

> <https://github.com/laminas/laminas-db/blob/af9634d981693ba1db61ca71f026563b76dabe42/src/Adapter/AdapterServiceDelegator.php#L22-L47>

---

<div class="post-metadata">

**Author:** ![jobsfan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/jobsfan/32/399_2.png) [@jobsfan](https://discourse.laminas.dev/u/jobsfan)\
**Post date:** [June 18, 2021, 6:21am UTC](https://discourse.laminas.dev/t/laminas-component-form-validator-recordexists-whats-the-best-way-to-pass-dbadapter-into/2364/22 "2021-06-18T06:21:46Z")

</div>

> [@froschdesign](#):
>
> Do you override the standard database adapter

no, I didn’t overwrite

```bash
public function getServiceConfig()
    {
        return [
            'factories' => [
                Model\BaseFuncModel::class => function($container){ //注册基础函数库到服务管理器里面
                    return new Model\BaseFuncModel();
                },

                /** discourse comment_main start */
                Model\CommentMainTable::class => function($container){
                    $tableGateway = $container->get(Model\CommentMainTableGateway::class);
                    return new Model\CommentMainTable($tableGateway);
                },
                Model\CommentMainTableGateway::class => function($container){
                    $dbAdapter = $container->get(AdapterInterface::class);
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Model\CommentMain());
                    return new TableGateway('comment_main', $dbAdapter, null, $resultSetPrototype);
                },
                /** discourse comment_main end */

```

You can find **$dbAdapter = $container-\>get(AdapterInterface::class);** in the above codes.

---

<div class="post-metadata">

**Author:** ![jobsfan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/jobsfan/32/399_2.png) [@jobsfan](https://discourse.laminas.dev/u/jobsfan)\
**Post date:** [June 18, 2021, 6:30am UTC](https://discourse.laminas.dev/t/laminas-component-form-validator-recordexists-whats-the-best-way-to-pass-dbadapter-into/2364/23 "2021-06-18T06:30:43Z")

</div>

Can I send the whole project to you? I think it is hard to figure out the problem just from code snippet.

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [June 18, 2021, 6:32am UTC](https://discourse.laminas.dev/t/laminas-component-form-validator-recordexists-whats-the-best-way-to-pass-dbadapter-into/2364/24 "2021-06-18T06:32:00Z")

</div>

Do you have a GitHub repository for it?

---

<div class="post-metadata">

**Author:** ![jobsfan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/jobsfan/32/399_2.png) [@jobsfan](https://discourse.laminas.dev/u/jobsfan)\
**Post date:** [June 18, 2021, 6:34am UTC](https://discourse.laminas.dev/t/laminas-component-form-validator-recordexists-whats-the-best-way-to-pass-dbadapter-into/2364/25 "2021-06-18T06:34:03Z")

</div>

no, no github repository, it is our company project deposit on gitlab(built in our inner network, company network.)

I can zip it as a package, and send to your email with sensitive info removed.

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [June 18, 2021, 6:54am UTC](https://discourse.laminas.dev/t/laminas-component-form-validator-recordexists-whats-the-best-way-to-pass-dbadapter-into/2364/26 "2021-06-18T06:54:13Z")

</div>

> [@jobsfan](#):
>
> I can zip it as a package, and send to your email with sensitive info removed.

Look on GitHub, there you will find my email address.

---

<div class="post-metadata">

**Author:** ![jobsfan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/jobsfan/32/399_2.png) [@jobsfan](https://discourse.laminas.dev/u/jobsfan)\
**Post date:** [June 18, 2021, 7:06am UTC](https://discourse.laminas.dev/t/laminas-component-form-validator-recordexists-whats-the-best-way-to-pass-dbadapter-into/2364/27 "2021-06-18T07:06:25Z")

</div>

Email sent, pls let me known, whether you’ve got it.

the problem comes from url /application/forum/threadmainpost

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [June 18, 2021, 7:28am UTC](https://discourse.laminas.dev/t/laminas-component-form-validator-recordexists-whats-the-best-way-to-pass-dbadapter-into/2364/28 "2021-06-18T07:28:54Z")

</div>

And it works. I do not get an error.

Enable the development mode: [The Skeleton Application - tutorials - Laminas Docs](https://docs.laminas.dev/tutorials/getting-started/skeleton-application/#development-mode)

Otherwise the merged application config is cached.

---

<div class="post-metadata">

**Author:** ![jobsfan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/jobsfan/32/399_2.png) [@jobsfan](https://discourse.laminas.dev/u/jobsfan)\
**Post date:** [June 18, 2021, 7:30am UTC](https://discourse.laminas.dev/t/laminas-component-form-validator-recordexists-whats-the-best-way-to-pass-dbadapter-into/2364/29 "2021-06-18T07:30:36Z")

</div>

I run with command **php -S 0.0.0.0:8080 -t public**

Ok, I will check again. thank you for your time and efforts! I will feedback soon.

---

<div class="post-metadata">

**Author:** ![jobsfan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/jobsfan/32/399_2.png) [@jobsfan](https://discourse.laminas.dev/u/jobsfan)\
**Post date:** [June 18, 2021, 7:45am UTC](https://discourse.laminas.dev/t/laminas-component-form-validator-recordexists-whats-the-best-way-to-pass-dbadapter-into/2364/30 "2021-06-18T07:45:59Z")

</div>

You are right!!! It works when I change it into develop mode.  
Before I run with php command in local and in development mode.  
But I forgot that I change it into product mode when I push to gitlab.

Thank you so much for your time and efforts!!!

---

<div class="post-metadata">

**Author:** ![jobsfan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/jobsfan/32/399_2.png) [@jobsfan](https://discourse.laminas.dev/u/jobsfan)\
**Post date:** [June 18, 2021, 7:50am UTC](https://discourse.laminas.dev/t/laminas-component-form-validator-recordexists-whats-the-best-way-to-pass-dbadapter-into/2364/31 "2021-06-18T07:50:19Z")

</div>

I still have a question.

I wanna to write a custom Validator, can I use a factory to inject dependency to the customed Validator via \_\_construct()?

As you see in my project, I need to valid a formHash element which need to use the BaseFuncModel-\>**formHashProcess()**  
I wanna to inject BaseFuncModel::class into my customed Validator.

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [June 18, 2021, 8:02am UTC](https://discourse.laminas.dev/t/laminas-component-form-validator-recordexists-whats-the-best-way-to-pass-dbadapter-into/2364/32 "2021-06-18T08:02:08Z")

</div>

We need to summarise the most important parts:

- do not create input filters decoupled from your application
- do not create forms decoupled from your application
- use the form element manager to fetch forms

Then you can use custom form elements, validators, filters and more, which themselves have dependencies.

See also the related tutorials:

- [Usage in a laminas-mvc application - laminas-form - Laminas Docs](https://docs.laminas.dev/laminas-form/v3/application-integration/usage-in-a-laminas-mvc-application/)
- [Usage in a laminas-mvc application - laminas-inputfilter - Laminas Docs](https://docs.laminas.dev/laminas-inputfilter/application-integration/usage-in-a-laminas-mvc-application/)

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [June 18, 2021, 8:03am UTC](https://discourse.laminas.dev/t/laminas-component-form-validator-recordexists-whats-the-best-way-to-pass-dbadapter-into/2364/33 "2021-06-18T08:03:15Z")

</div>

Please create separate topic for this question because it does not belongs to validators and database adapters. Thanks!

---

<div class="post-metadata">

**Author:** ![jobsfan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/jobsfan/32/399_2.png) [@jobsfan](https://discourse.laminas.dev/u/jobsfan)\
**Post date:** [June 18, 2021, 8:04am UTC](https://discourse.laminas.dev/t/laminas-component-form-validator-recordexists-whats-the-best-way-to-pass-dbadapter-into/2364/34 "2021-06-18T08:04:33Z")

</div>

Ok, I will sort my mind and open a new topic later.

---

<div class="post-metadata">

**Author:** ![ALTAMASH80](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/altamash80/32/895_2.png) [@ALTAMASH80](https://discourse.laminas.dev/u/ALTAMASH80)\
**Post date:** [June 24, 2021, 6:46am UTC](https://discourse.laminas.dev/t/laminas-component-form-validator-recordexists-whats-the-best-way-to-pass-dbadapter-into/2364/35 "2021-06-24T06:46:39Z")

</div>

Hi, I know this is a very late reply. I would suggest looking into [LmcUser](https://github.com/LM-Commons/LmcUser) module. You’ll find record exists usage there. It may give you an idea about how to use it in your project. Thanks!

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [June 24, 2021, 10:03am UTC](https://discourse.laminas.dev/t/laminas-component-form-validator-recordexists-whats-the-best-way-to-pass-dbadapter-into/2364/36 "2021-06-24T10:03:50Z")

</div>

> [@ALTAMASH80](#):
>
> You’ll find record exists usage there.

The current thread is not about how to use the validator, but how to set the database adapter. Therefore I do not see how your answer help here because this part is missing in your component.

---

<div class="post-metadata">

**Author:** ![ALTAMASH80](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/altamash80/32/895_2.png) [@ALTAMASH80](https://discourse.laminas.dev/u/ALTAMASH80)\
**Post date:** [June 25, 2021, 6:29am UTC](https://discourse.laminas.dev/t/laminas-component-form-validator-recordexists-whats-the-best-way-to-pass-dbadapter-into/2364/37 "2021-06-25T06:29:50Z")

</div>

Hi @froschdesign, you’re following this post and many posts which are on this forum. I’ve only tried to give my suggestion based on the question. My answer may not be the exact answer he/she is looking for. But might help if he/she is really trying to learn or developing something. Apologies in advance if I’ve taken this topic off the course.

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [June 25, 2021, 7:11am UTC](https://discourse.laminas.dev/t/laminas-component-form-validator-recordexists-whats-the-best-way-to-pass-dbadapter-into/2364/38 "2021-06-25T07:11:05Z")

</div>

> [@ALTAMASH80](#):
>
> Apologies in advance if I’ve taken this topic off the course.

The problem is: I have searched the LmcUser repository, but it does not answer the question of this forum thread. More than that, the part is completely missing.

[Previous page](https://discourse.laminas.dev/t/laminas-component-form-validator-recordexists-whats-the-best-way-to-pass-dbadapter-into/2364.md?page=1)
