我有一个User
实体,我以称为Register
的形式收集所有数据。但是一旦注册,我想通过表单编辑用户的某些字段。但是避免某些字段被更新(电子邮件),并且“密码”字段是可选的。我尝试呈现相同的注册表,但是$form->handleRequest($request)
抛出错误:
在属性路径中给出的类型为“字符串”,“ NULL”的预期参数 “密码”。
我想知道是否有办法吗?
我尝试了$form->submit($request->request->get($form->getName()),false);
而不是$form->handleRequest($request);
,但没有用。
//这是我的控制器。
/**
* @Route("/update/{id}", name="update")
* @param $id
* @param Request $request
* @param UserPasswordEncoderInterface $passwordEncoder
* @param UserRepository $userRepository
* @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
*/
public function updateUser($id,Request $request, UserPasswordEncoderInterface $passwordEncoder, UserRepository $userRepository){
$user = $userRepository->find($id);
$form = $this->createFormBuilder($user)
->add('email',EmailType::class,[
'label'=>'Email',
'required' => false,
'attr'=>['placeholder'=>"Email"]
])
->add('password',RepeatedType::class,[
'type' => PasswordType::class,
'invalid_message' => 'The password fields must match.',
'required' => false,
'options' => ['attr' => ['class' => 'password-field']],
'first_options' => ['label' => 'Password','attr'=>['placeholder'=>"Password"]],
'second_options' => ['label' => 'Confirm Password','attr'=>['placeholder'=>"Confirm Password"]],
])
->add('firstName',TextType::class,['label'=>'First Name', 'attr'=>['placeholder'=>"First Name"]])
->add('lastName',TextType::class,['label'=>'Last Name','attr'=>['placeholder'=>"Last Name"]])
->add('address',TextareaType::class,['required' => false,'label'=>'Address','attr'=>['placeholder'=>"Address"]])
->add('idNumber',TextType::class,['label'=>'NIC Number','attr'=>['placeholder'=>"NIC Number"]])
->add('phoneNumber',TelType::class,['label'=>'Phone Number','attr'=>['placeholder'=>"Phone Number"]])
->add('image',FileType::class,['label'=>'Photo','required'=>false,'attr'=>['hidden'=>"hidden", 'accept'=>"image/jpeg, image/png"]])
->add('save',SubmitType::class,[
'label'=>'Register',
'attr' => [
'class'=>"btn btn-outline-success float-right"
]
])
->getForm();
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
if($user->getPassword() !=""){
$user->setPassword($passwordEncoder->encodePassword($user,$user->getPassword()));
}
$em = $this->getDoctrine()->getManager();
$em->flush();
return $this->redirectToRoute('home');
}
return $this->render('register/update.html.twig', [
'form'=>$form->createView(),
]);
}
//这是我的TWIG文件。
{% extends 'base.html.twig' %}
{% block title %}Edit Info{% endblock %}
{% block refHome %}{{ path('main') }}{% endblock %}
{% block body %}
<div class="container" style=" margin-left: auto;margin-right: auto;margin-top: 1cm;" >
<div class="row">
{# <div class="col-lg-2"></div>#}
<div class="col-lg-10">
<div class="card" style="width: 50rem;text-align: center;">
<div class="card-body">
<h3 class="card-title float-left">Edit Info</h3>
<div style="margin-top: 2cm;">
<p class="card-text">
<form name="register" id="register_form" method="post" enctype="multipart/form-data">
<div id="register">
<div class="container">
<div class="row">
<div class="col-sm-5">
{{ form_errors(form.firstName) }}
{{ form_widget(form.firstName) }}
</div>
<div class="col-sm-2"></div>
<div class="col-sm-5">
{{ form_errors(form.lastName) }}
{{ form_widget(form.lastName) }}
</div>
</div>
<div style="margin-top: 0.5cm;"></div>
<div class="row">
<div class="col-sm-6">
{{ form_errors(form.email) }}
{{ form_widget(form.email) }}
</div>
<div class="col-sm-1"></div>
<div class="col-sm-5">
{{ form_errors(form.idNumber) }}
<input type="text" id="register_idNumber" placeholder="NIC Number" onkeypress="isInputNumber(event)" pattern="^[0-9]{9}[a-zA-Z]$" value="{{ form.idNumber.vars.value }}" name="register[idNumber]" required="required" class="form-control" " /> <div class="input-group-append">
</div>
</div>
</div>
<div style="margin-top: 0.5cm;"></div>
<div class="row">
<div class="col-sm-5">
<div style="margin-top: 0.5cm;"></div>
{{ form_errors(form.phoneNumber) }}
<input type="tel" id="register_phoneNumber" value="{{ form.phoneNumber.vars.value }}" name="register[phoneNumber]" onkeypress="isPhone(event)" required="required" placeholder="Phone Number" class="form-control" pattern="^[0-9]{10}$" />
</div>
<div class="col-sm-2"></div>
</div>
<div style="margin-top: 0.5cm;"></div>
<div class="row" >
<div class="col-sm-7">
{{ form_errors(form.address) }}
{{ form_widget(form.address) }}
</div>
<div class="col-sm-1" hidden="hidden">
{{ form_widget(form.image) }}
</div>
</div>
<div style="margin-top: 0.5cm;"></div>
<div class="form-group row">
<div class="col-sm-7">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="defaultUnchecked" style="float: left">
<label class="custom-control-label" for="defaultUnchecked">Change Password</label>
</div>
</div>
</div>
<div style="margin-top: 0.5cm;"></div>
<div id="register_password" >
<div class="form-group row">
{% for passwordField in form.password %}
<div class="col-sm-5">
{{ form_errors(passwordField) }}
{{ form_widget(passwordField) }}
</div>
<div class="col-sm-2"></div>
{% endfor %}
</div>
<div style="margin-top: 0.5cm;"></div>
</div>
</div>
{{ form_widget(form.save,{'label':"Update"}) }}
{{ form_widget(form._token) }}
{{ form_errors(form) }}
</form>
</p>
</div>
</div>
</div>
</div>
<div class="col-lg-1"></div>
</div>
</div>
{% endblock %}
{% block javascripts %}
<script>
const checkBoxPwd = document.getElementById('defaultUnchecked');
const pwdDiv = document.getElementById('register_password');
pwdDiv.style.visibility = "hidden";
checkBoxPwd.addEventListener("change",function () {
if (checkBoxPwd.checked == true){
pwdDiv.style.visibility = "visible";
}else{
pwdDiv.style.visibility = "hidden";
}
});
</script>
<script>
function isInputNumber(eve) {
var ch = String.fromCharCode(eve.which);
if (document.getElementById('register_idNumber').value.length < 9){
if (!(/[0-9]/.test(ch))){
eve.preventDefault();
}
}else {
if (!(/[a-zA-Z]/.test(ch))){
eve.preventDefault();
}
}
if (document.getElementById('register_idNumber').value.length > 9){
eve.preventDefault();
}
}
function isPhone(eve) {
var ch = String.fromCharCode(eve.which);
if (!(/[0-9]/.test(ch))){
eve.preventDefault();
}
if (document.getElementById('register_phoneNumber').value.length > 9){
eve.preventDefault();
}
}
</script>
{% endblock %}