1

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.

2 Answers 2

1

There are at least 3 ways to do this.

With validation groups, you define which set of rules to use for the validation. So as default you want to use all the rules, but in this action you indicate that only the contact info should be validated.

Create a new FormType and use this as a parent class for the TeacherType. In this new base class you place all the contact fields and then use this class in this action.

Create a new FormType and use the remove method to remove the unwanted fields. This is error prone, when you change the parent formtype, you need to also change this type.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @bartv, validation groups are just what I needed. Maybe I read something about this some time ago but I had already forgotten.
I have a problem @bartv, using "validation groups" already recognizes the form as valid but the data of the rest of unvalidated fields are updated in the database by deleting the value it already had and leaving it empty. What I need is to only update the data of the contacts which is what the user can modify in this way.
@Joseph i think the neatest solution is to create a new formtype, option 2 of the answer
Thanks @bartv, I'll do it that way. I would like to take the opportunity to show you another problem that I have in my project and that I have not had a solution, since I have explained a little wrong and I have not put much information of help. I think I have to use "collection Field Type" but I'm not sure. Here you can see the question.
0

You need to change your Asserts statements to exclude all NotBlank assertions if you don't need them.

Or if you want to populate all NotBlank fields with pre-defined values you can do it right in your controller.

If you have existing entity you should pass it to your form when you create it:

$form = $this->createForm(TeacherType::class, $teacher);

1 Comment

Hello @MichaelSivolobov, the problem I have is that for example for the registration of teachers I need these fields can not be empty and what I want is to use the same form but only show the contact fields and update those three fields only how could I do that ? Should I create another form with these three types alone or can I use the same type of form to save only some data? In the form to edit the values ​​of the fields of the database are opted, reason why no required field is empty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.