9 Replies - 317 Views - Last Post: 24 April 2020 - 01:50 PM Rate Topic: -----

#1 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Symphony form

Posted 24 April 2020 - 12:21 PM

In Symfony 3.4 I'm using a form with address values like:
 	->add('city', TextType::class, array('required'=>false))

I have a form with address information, none of which is required, but all of which must be validated if submitted. I want to bind the form to an order so that I can modify the order if the form is submitted. That works fine, except if a field is left off. In that case, I want the previous value to remain. If the field is included, but blank, I want the value to be set to an empty string/null depending on the field. What's happening is that the field is getting set to empty if the field is missing or empty.
So I need:
  • Validation to continue working.
  • Missing fields are to be ignored.
  • Present, but empty fields will empty in the object as long as that validates.

Any ideas?

Is This A Good Question/Topic? 0
  • +

Replies To: Symphony form

#2 Splashsky   User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 565
  • Joined: 25-August 13

Re: Symphony form

Posted 24 April 2020 - 12:39 PM

Presumably you have ORM, so if you have an instance of the object you're updating, would you be able to if empty(field) instead = obj->old value?

Sorry, on phone so this sucks lol

This post has been edited by Splashsky: 24 April 2020 - 12:40 PM

Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: Symphony form

Posted 24 April 2020 - 12:43 PM

I'm trying to avoid that. Some fields are private, so I'd have to either iterate over an array of method calls or check each field individually, and that defeats the purpose of using Symfony's validation.
Was This Post Helpful? 0
  • +
  • -

#4 Splashsky   User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 565
  • Joined: 25-August 13

Re: Symphony form

Posted 24 April 2020 - 12:45 PM

Ah, then I'm lost. Laravel is built on Symphony. Validation more or less is an array of field=>rules pairs, and so long as validation passes the value can be whatever.

What do you mean by private fields?

This post has been edited by Splashsky: 24 April 2020 - 12:45 PM

Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: Symphony form

Posted 24 April 2020 - 12:51 PM

The object is an entity, and those classes typically use private fields:
https://symfonycasts...e/create-entity

Even if they were public, I'd still need to check each one. Right now, we only want to allow updating of a shipping address and a a requested shipping method, so there are only 9 fields. If we add more later, manually updating/reverting them will get messy and error prone.
Was This Post Helpful? 0
  • +
  • -

#6 Splashsky   User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 565
  • Joined: 25-August 13

Re: Symphony form

Posted 24 April 2020 - 12:53 PM

Ah, I see what you mean. Then is it a problem to populate the form with current parameters, let the user change it, then submit and as long as validation passes its good? I guess I'm not super clear on the dilemma.
Was This Post Helpful? 1
  • +
  • -

#7 Ornstein   User is offline

  • D.I.C Head

Reputation: 105
  • View blog
  • Posts: 216
  • Joined: 13-May 15

Re: Symphony form

Posted 24 April 2020 - 12:54 PM

I've not used a form builder in about a thousand years, but would the empty_data option not help you here?
Was This Post Helpful? 1
  • +
  • -

#8 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: Symphony form

Posted 24 April 2020 - 12:58 PM

I think I found the problem. I need to use patch, not post. Symfony should only evaluate the submitted fields then. Will post again when I'm sure.

View PostSplashsky, on 24 April 2020 - 01:53 PM, said:

Then is it a problem to populate the form with current parameters, let the user change it, ...

It's for an API that can be receiving data from other automated systems, so no "user" really.
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: Symphony form

Posted 24 April 2020 - 01:45 PM

Well, patch may or may not help, but there still seems to be a problem with the form. It assigns an empty value to anything that isn't sent. That usually isn't a problem, but it is in this case!
Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: Symphony form

Posted 24 April 2020 - 01:50 PM

For anyone with a similar problem, I forgot to add false to the submission (clearMissing):
$form->submit($request->request->all(), false);

This post has been edited by CTphpnwb: 24 April 2020 - 02:02 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1