Is there other ways to add custom class to a form?
My related form class(if I wanna to add some attributes like class, can I define it in this part?):
class MemberForm extends Form implements InputFilterProviderInterface
{
public function init() : void
{
$this->add([
'name' => 'id',
'type' => Hidden::class,
]);
$this->add([
'name' => 'username', //用户名
'type' => Text::class,
'options' => [
'label' => '用户账号',
'label_attributes' => [
'class' => 'form-label',
],
],
'attributes' => [
'class' => 'form-control',
],
]);
What I do now is to define things in the phtml view, like(write several lines at the top of the view. personally, I don’t like it, i think it is not grace):
<?php
/**
* @var Laminas\View\Renderer\PhpRenderer $this
*/
$form->setAttribute('action', $this->url('member/index', ['action' => 'register']));
$form->setAttribute('novalidate', false);
$form->setAttribute('class', 'needs-validation');
$form->prepare();
?>
<div class="col-md-12">
<div class="w-100 m-auto mt-3" style="max-width: 330px;">
<div class="text-center">
<img class="mb-4 m-auto" src="/img/avatar.jpg" alt="" width="72" height="72">
<h1 class="h3 mb-3 fw-normal">注册账户</h1>
</div>
<?= $this->form()->openTag($form) ?>
<div class="row g-3">
<div class="col-12">
<?= $this->formLabel($form->get('username')) ?>
<?= $this->formText($form->get('username')) ?>
<?= $this->formElementErrors($form->get('username')) ?>
</div>
<div class="col-12">
<?= $this->formLabel($form->get('email')) ?>
<?= $this->formText($form->get('email')) ?>
<?= $this->formElementErrors($form->get('email')) ?>
</div>
Besides, the phpstorm editer mark the first $form as red, how to resolve this, it makes me uncomfortable.
At last, I actually wanna to validate my forms like Checkout example · Bootstrap v5.3
like this style(the error style from the php backend render, not from front js.).
The following is my demo page https://www.3dsjs.cn/member/index/register, when I submit the form without filling contents. it shows as following, a little simple and plain.
Before I found a stackoverflow article to add a .has-error manually, but I can not find the webpage. And I don’t think it is a good way to do so. Any other more graceful moves?
Have a nice weekend!!!