这与Trying扩展ActionView :: Helpers :: FormBuilder类似,但我不想使用:builder => MyThing.
我想扩展表单生成器以添加自定义方法.这是目前的情况:
module ActsAsTreeHelpers
def acts_as_tree_block(method, &block)
yield if block_given?
end
end
ActionView::Helpers::FormBuilder.send :include, ::ActsAsTreeHelpers
Run Code Online (Sandbox Code Playgroud)
安慰:
ruby-1.9.2-p180 :004 > ActionView::Helpers::FormBuilder.included_modules
=> [ActsAsTreeHelpers, ...]
Run Code Online (Sandbox Code Playgroud)
但以下给了我: undefined method acts_as_tree_block for #<ActionView::Helpers::FormBuilder:0xae114dc>
<%= form_for thing do |form| %>
<%= form.acts_as_tree_block :parent_id, {"test"} %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么?
我有一个formbuilder表单,有多个国家/地区列表.当我在我的表单上呈现它们时,如下所示:
{{ form_widget(edit_form.countries) }}
Run Code Online (Sandbox Code Playgroud)
它们看起来像这样:
<input type="checkbox" name="..." value="AD" /><label for="...">Andorra</label>
<input type="checkbox" name="..." value="AE" /><label for="...">United Arab Emirates</label>
<input type="checkbox" name="..." value="AF" /><label for="...">Afghanistan</label>
Run Code Online (Sandbox Code Playgroud)
我希望每个选项在浏览器中显示在它自己的行上,而不是连续显示.如何在选项周围或之间注入html?或者有一种CSS方法来实现这一目标吗?
谢谢.
我是这样的表单构建器:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('file','file')
->add('concurs','entity', array('class' => 'MCFrontEndBundle:Concurs'))
;
}
Run Code Online (Sandbox Code Playgroud)
如您所见,它显示了一个用于选择文件的输入,然后是一个下拉列表,用于选择由Concurs实体填充的任何选项.
我可以手动添加"无"选项吗?
我是Symfony 2的新手,我正在尝试为具有外键的内容类型构建表单.我不知道如何使用表单保存外键.
我的两个表是"类别"和"问题".一个问题属于一个类别(多对一).所以我在Entity中的Question.php文件包含:
<?php
namespace Iel\CategorieBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Question
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Iel\CategorieBundle\Entity\QuestionRepository")
*/
class Question
{
/**
* @ORM\ManyToOne(targetEntity="Iel\CategorieBundle\Entity\Categorie")
* @ORM\JoinColumn(nullable=false)
*/
private $categorie;
/**
* Set categorie
*
@param Iel\CategorieBundle\Entity\Categorie $categorie
*/
public function setCategorie(\Iel\CategorieBundle\Entity\Categorie $categorie)
{
$this->categorie = $categorie;
}
/**
* Get categorie
*
@return Iel\CategorieBundle\Entity\Categorie
*/
public function getCategorie()
{
return $this->categorie;
}
Run Code Online (Sandbox Code Playgroud)
我试过像这样构建控制器函数,但它不是一个正确的语法:
public function addquestionAction()
{
$question = new Question;
$form = $this->createFormBuilder($question)
->add('titre', 'text')
->add('auteur', 'text')
->add('contenu', …Run Code Online (Sandbox Code Playgroud) 我尝试从CategoryType表单设置字段"author"的值.我希望它是来自使用FOS包登录的当前用户的用户ID.
我的CategoryType表单:
namespace My\CategoryBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class CategoryType extends AbstractType
{
private $userId;
public function __construct(array $userId)
{
$this->userId = $userId;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('author')
->add('content')
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'My\CategoryBundle\Entity\Category',
'auteur' => $this->userId
));
}
/**
* @return string
*/
public function getName()
{
return 'my_categorybundle_category'; …Run Code Online (Sandbox Code Playgroud) 我想在用户单击8个问题多项选择测验的提交按钮时将用户引导至"结果"页面,同时还将他们的答案保存到数据库中.
目前我正在使用'form_for'并传入current_user.单击提交时,它将指向User/show操作.我想转到详细说明用户结果的页面.我该怎么做呢?
这是我的(非常粗略的测试)形式,到目前为止,一个多项选择问题(由引导程序设计):
<%= form_for([current_user]) do |f| %>
<p>
<%= text_field(:user, :name) %>
</p>
<div class="btn-group-vertical clearfix form-group" data-toggle="buttons">
<label class="btn btn-primary text-left">
<input name="options" id="option1" type="radio" onclick="multiChoiceClick(1, 1)">A. Always. I'm the leader and should have the final say
</label>
<label class="btn btn-primary text-left">
<input name="options" id="option2" type="radio">B. Sometimes, but I think the group should make decisions if possible
</label>
<label class="btn btn-primary text-left">
<input name="options" id="option3" type="radio">C. I generally don't get involved. The group can make their own decisions …Run Code Online (Sandbox Code Playgroud) 我正在调用web api来检查urlalias是否可用,对于此任务,我在异步验证器中使用了httpservice.问题是,当调用验证器时,将执行所有正确的代码路径(所有console.log()运行并按预期运行).
无论来自验证的promise是否返回/解析为null或者{ 'isUrlAliasActivityMainAvailable': true },控制器总是将错误对象显示为跟随,从而将表单状态保持为无效,为什么(血腥地狱!)?
我正在使用:angular:2.1.0和rxjs:5.0.0-beta.12
这是我的formbuilder:
this.formBuilder.group({
//...
"urlAliasActivityMain":[null,[ ValidatorsZr.isUrlAliasActivityMainAvailableAsyncValidator(this.httpActivityService)]],
});
Run Code Online (Sandbox Code Playgroud)
这是我的验证器:
public static isUrlAliasActivityMainAvailableAsyncValidator(httpActivityService: HttpActivityService) {
return function (control: FormControl): Promise<any> | Observable<any> {
const promise = new Promise<any>(
(resolve, reject) => {
httpActivityService.isUrlAliasActivityMainAvailable(control.value)
.subscribe(
(data: any) => {
console.log("isUrlAliasActivityMainAvailableAsyncValidator");
console.log(data);
if (data == false) {
console.log("data == false");
resolve({ 'isUrlAliasActivityMainAvailable': true });
}
else {
console.log("data == true");
resolve(null);
}
},
)
});
return promise;
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试动态生成一个FormBuilder组,并且被生成的组对象所困扰。我有以下用于单个.html输入字段的组件。
import { Component, onInit } from '@angular/core';
import { FormBuilder, FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
job: any;
constructor(
public formBuilder: FormBuilder
) {
this.job = {
number: 'J001'
}
}
ngOnInit() {
const jobGroup: FormGroup = new FormGroup({});
for (const key in this.job) {
if (this.job.hasOwnProperty(key)) {
const control: FormControl = new FormControl(this.job[key], Validators.required);
jobGroup.addControl(this.job[key], control);
}
}
this.jobForm = this.formBuilder.group(jobGroup);
}
} …Run Code Online (Sandbox Code Playgroud) 我正在从事 Angular 9 项目。
我有一个组件接受 formBuilder 作为来自父组件的输入。这是我的子组件(我正在测试的组件):
export class ChildComponent implements OnInit {
@Input() parentForm: FormGroup; //this is coming from the parentComponent
ngOnInit(): void {
if (this.parentForm) {
this.filteredSelectables = this.parentForm
.get("recipientTypes")
...
}
}
...
Run Code Online (Sandbox Code Playgroud)
我想为此组件编写测试,但我需要创建一个测试可以使用的表单(或者我需要模拟父组件并返回我想要的表单?)
我已将 FormBuilder 添加到 testBed 提供程序中,但我仍然不知道如何制作可以测试的模拟表单。“应该创建”测试正在通过,但我无法测试其他任何内容,因为它们的parentForm 不能为空。这是我当前的测试:
describe("ChildComponent", () => {
let component: ChildComponent;
let fixture: ComponentFixture<ChildComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ChildComponent, MatAutocomplete],
providers: [FormBuilder],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
.compileComponents()
.then(() => {
fixture = TestBed.createComponent(ChildComponent);
component = fixture.componentInstance;
});
}));
beforeEach(() => { …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用正则表达式创建仅适用于整数(例如 60)的验证器。我正在尝试这样做:
meter: ["", Validators.required, Validators.pattern(/[0-9]/)]
Run Code Online (Sandbox Code Playgroud)
它不起作用,但我不得不说我对这种技术并不熟悉。任何人都可以阐明如何修改此表达式以仅接受整数吗?这个数字是针对身高的,身高又分为米和厘米。
我已经搜索过,但我可以找到小数,所以,不知何故,也许因为它太微不足道了,对于整数,我找不到,事实上,我昨天在这里找到了,但我找不到答案了,我找到了,我在寻找另一件事。
formbuilder ×10
angular ×4
symfony ×4
actionview ×1
angular9 ×1
angularjs ×1
choice ×1
promise ×1
regex ×1
ruby ×1
rxjs ×1
testing ×1
typescript ×1
validation ×1