本文概述
- A.使用remove方法删除字段
- B.使用buildForm中的选项进行过滤
例如, 在本文中, 我们将使用以下FormType即UserType:
<
?phpnamespace userBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
class UserType extends AbstractType{/*** {@inheritdoc}*/public function buildForm(FormBuilderInterface $builder, array $options){$builder->
add('name', TextType::class , array("attr" =>
array("class" =>
"form-control")))->
add('username', TextType::class, array("attr" =>
array("class" =>
"form-control")))->
add('description', TextareaType::class, array("attr" =>
array("class" =>
"form-control", "maxlength" =>
255)))->
add('password', RepeatedType::class, array('type' =>
PasswordType::class, 'invalid_message' =>
'The password fields must match.', 'options' =>
array('attr' =>
array('class' =>
'form-control')), 'required' =>
true, 'first_options'=>
array('label' =>
'Password'), 'second_options' =>
array('label' =>
'Repeat Password'), ));
} /*** {@inheritdoc}*/public function getBlockPrefix(){return 'userbundle_user';
}}
该FormType具有4个字段:名称, 用户名, 描述和密码。此表单将用于2个动作:newAction和editAction。在newAction中工作时, 我们不需要删除任何字段, 但是, 当用户进行编辑时, 我们需要删除密码字段, 可以通过两种方式实现:
A.使用remove方法删除字段通过createForm方法从返回的对象中, 只需调用Form类的remove方法:
<
?phpnamespace userBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
// Classes of the exampleuse userBundle\Form\UserType;
use userBundle\Entity\User;
class UserController extends Controller{// .. //public function editAction(Request $request, $id){$em = $this->
getDoctrine()->
getManager();
$user = $em->
getRepository("userBundle:User")->
find($id);
if(!$user){throw $this->
createNotFoundException("The providen user doesn't exist with id $id");
}// Prepare Form$editForm = $this->
createForm(UserType::class, $user);
// Remove the password field !$editForm->
remove('password');
// Rest of the code ...}// .. //}
这将仅从表单中删除字段, 仅此而已。
B.使用buildForm中的选项进行过滤【如何以编程方式从Symfony 3表单(表单类型)中删除字段】你可能(或可能不知道)知道, createForm方法期望将带有选项的数组作为第三个参数:
/** * Creates and returns a Form instance from the type of the form. * * @param string $typeThe fully qualified class name of the form type * @param mixed$dataThe initial data for the form * @param array$options Options for the form * * @return Form */protected function createForm($type, $data = http://www.srcmini.com/null, array $options = array()){}
作为Form Type类中的第二个参数发送到buildForm函数:
public function buildForm(FormBuilderInterface $builder, array $options)
因此, 你可以使用一个简单的” flag” 参数来指定(仅在定义时)是否呈现密码字段, 例如, 你将需要修改类型并使用以下选项添加条件:
<
?phpnamespace userBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
class UserType extends AbstractType{/*** {@inheritdoc}*/public function buildForm(FormBuilderInterface $builder, array $options){$builder->
add('name', TextType::class , array("attr" =>
array("class" =>
"form-control")))->
add('username', TextType::class, array("attr" =>
array("class" =>
"form-control")))->
add('description', TextareaType::class, array("attr" =>
array("class" =>
"form-control", "maxlength" =>
255)));
// If the usePassword options is set to true, if($options["usePassword"]){$builder->
add('password', RepeatedType::class, array('type' =>
PasswordType::class, 'invalid_message' =>
'The password fields must match.', 'options' =>
array('attr' =>
array('class' =>
'form-control')), 'required' =>
true, 'first_options'=>
array('label' =>
'Password'), 'second_options' =>
array('label' =>
'Repeat Password'), ));
}}public function setDefaultOptions(OptionsResolverInterface $resolver){// Set by default when the options aren't providen, use the password$resolver->
setDefaults(array('usePassword' =>
true));
}/*** {@inheritdoc}*/public function getBlockPrefix(){return 'userbundle_user';
}}
请注意, 如果在创建表单时未提供第三个参数, 则需要使用setDefaultOptions来指定值。最后, 在你的控制器中指定一个带有选项的数组, 在这种情况下, 该数组仅是ona, 即具有布尔值的usePassword:
<
?phpnamespace userBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
// Classes of the exampleuse userBundle\Form\UserType;
use userBundle\Entity\User;
class UserController extends Controller{// .. //public function editAction(Request $request, $id){$em = $this->
getDoctrine()->
getManager();
$user = $em->
getRepository("userBundle:User")->
find($id);
if(!$user){throw $this->
createNotFoundException("The providen user doesn't exist with id $id");
}// Prepare Form and remove the password field$editForm = $this->
createForm(UserType::class, $user , ['usePassword' =>
false]);
// Rest of the code ...}// .. //}
编码愉快!
推荐阅读
- 在Express中,app.router到底做了什么()
- 如何在你的Symfony 3表单中检索未映射字段的值
- 如何在Symfony 3中合并多个PDF
- 如何在WinForms中使用Barcodelib库使用C#从具有不同格式的字符串中创建条形码图像
- Winforms跨线程操作无效(从不是在其上创建线程的线程访问的控件”控件名”)
- 如何在Windows中使用Swift编程语言
- 每个Twig开发人员都应该能够回答的20个问题
- 如何使用WinForms中的OpenCVSharp库和带有C#的网络摄像机拍摄快照
- 如何在C#中使用AES加密算法对文件进行加密和解密