首页 > 解决方案 > CakePHP 3 自定义表单

问题描述

我正在使用 CakePHP3 和 Angular 为表单输入提供一些前端验证。这工作得很好,但我希望尽量减少我的前端标记 - 如下:

$this->Form->control('Name', [
     'placeholder' => 'Test', 
     'ng-model' => 'listing.test',
     'templateVars' => [
          'has_errors' => 'ng-class="{\'has-error\': addListing.$submitted && addListing.name.$invalid}"',
          'error' => '<div class="error-message" ng-show="addListing.$submitted && addListing.name.$invalid">Please specify your name</div>',
     ]  
]); 

在我的 app_form.php 我有以下内容:

'input' => '<input class="form-control mb15" type="{{type}}" name="{{name}}"{{attrs}} />{{error}}',

我不想为每个输入重复'<div class="error-message"...',最好只传递'ng-show'。

我的想法是做类似的事情:

'templateVars' => [
     'has_errors' => 'ng-class="{\'has-error\': addListing.$submitted && addListing.name.$invalid}"',
     'show' => 'addListing.$submitted && addListing.name.$invalid',
     'message' => 'Please input your name' 
]

'customError' => '<div class="error-message" ng-show"{{show}}>{{error-message}}</div>',
'input' => '<input class="form-control mb15" type="{{type}}" name="{{name}}"{{attrs}} />{{customError}}',

但是,我认为这种自定义超出了可能,因为我将创建自己的模板密钥。这可能吗?

标签: cakephp-3.7

解决方案


推荐阅读