I'm working on a project about a school. I have the entity "Teacher" and the corresponding form with several fields:
Entity
class Teachers implements UserInterface
{
...
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="dni", type="string", length=10, unique=true )
*/
private $dni;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
* @Assert\NotBlank()
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="surname", type="string", length=255)
* @Assert\NotBlank()
*/
private $surname;
/**
* @var \DateTime
*
* @ORM\Column(name="birthdate", type="date")
* @Assert\NotBlank()
*/
private $birthdate;
/**
* @var string
*
* @ORM\Column(name="address", type="string", length=255)
* @Assert\NotBlank()
*/
private $address;
/**
* @var string
*
* @ORM\Column(name="phone", type="string", length=12, nullable=true)
* @Assert\Regex(pattern="/^\d{3}([- .]?\d{2}){3}$/",message="Not a valid phone number.")
*/
private $phone;
/**
* @var string
*
* @ORM\Column(name="mobile phone", type="string", length=12, nullable=true)
* @Assert\Regex(pattern="/^\d{3}([- .]?\d{2}){3}$/",message="Not a valid phone number.")
*/
private $mobile phone;
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=255, nullable=true)
* @Assert\Email()
*/
private $email;
...
In one part the project the user can only change their contact information: phone, mobile phone and email. For this I have used in a template the following code:
Twig template
...
{{ form_start(form) }}
{{ form_row(form.email) }}
{{ form_errors(form.email) }}
{{ form_row(form.phone) }}
{{ form_errors(form.phone) }}
{{ form_row(form.mobilePhone) }}
{{ form_errors(form.mobilePhone) }}
{{ form_widget(form._token) }}
{{ form_errors(form._token) }}
{{ form_row(form.submit) }}
{{ form_end(form, {'render_rest': false}) }}
I read that with 'render_rest': false I can only send those fields that I explicitly indicated, but the problem is that in the controller detect that the form is invalid. Doing var_dump ($ editForm-> getErrorsAsString ()); Shows me all fields that can not be null with the error This value should not be empty.
How can I solve this problem by updating only a few fields of the general form?
I'm new to this, thank you for your help.