Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form schema fixes #5239

Merged

Conversation

chillu
Copy link
Member

@chillu chillu commented Mar 28, 2016

Further work on #4938, in preparation for a React-powered asset and campaign admin.

/cc @tractorcow @flashbackzoo

davidmorrisonnz and others added 5 commits March 28, 2016 21:14
Also moved keys of higher importance to start of array,
easier to review this way
Temporary measure until we implement nested fields
More of a standard API approach to return data by default, and make customisation
via HTTP headers an optional mode.
The RFC requires a FormField implementation to override $schemaDataType,
but its defined on the trait - which can't be redefined by a field subclass.

In the end, the trait was never designed to be reuseable on classes other than FormField.
We need to admit that architecturally, we'll have to add all that API weight to the base FormField
class because of the way forms are structured in SilverStripe (mainly due to a missing layer
of indirection in getCMSFields implementations).

Also implemented the $schemaDataType on fields where its known.

See silverstripe#4938
See http://php.net/manual/en/language.oop5.traits.php#language.oop5.traits.properties.example
@chillu
Copy link
Member Author

chillu commented Mar 29, 2016

@silverstripe/core-team We're hoping to get this pull request over the line in the next 48h because we have an opportunity for staff to fix things during an internal hackday here at SilverStripe Ltd on NZ Friday, ideally without too much branching fragmentation. Can you please review in your respective areas of expertise (PHP and/or JS/React)? @flashbackzoo, @scott1702 and myself have largely peer-reviewed each others work here, but would be good to get the thumbs up from at least one other core committer since these pull requests lay the ground work for further React integration. You'll see some @todo tags, some of it is early stages, but we agree with @sminnee's guidance that we should try to integrate early and often in our product development cycle.

*
* @var string
*/
protected $schemaDataType;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would there be any merit in storing these somewhere, perhaps as constants? I can foresee myself getting the case wrong

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for class constants on the trait object, with the values set to these strings?

FormFieldSchemaTrait::TYPE_STRING = 'String';
FormFieldSchemaTrait::TYPE_HIDDEN = 'Hidden';

It'll also make changes that create a new one of these data types will have a commit back here with a new constant, which seems an appropriate level of announcement.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a docblock note saying that the value for this should be set in definition of the FormField subclasses. I'm not sure if that blocks us from using class constants for the values, though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for constants for types. They should be reasonably fixed, unless the universe invents new primitive data types. :D

@kinglozzer
Copy link
Member

Gah, it sucks that we have to lose the trait. Is it just the one $schemaDataType property that’s the problem? Can we work around it somehow?

public function getSchemaDataType() {
    if (!$this->schemaDataType) {
        $this->schemaDataType = 'Text';
    }
    return parent::getSchemeDataType();
}

Or possibly call $this->setSchemaDataType('Foo'); in the subclass constructors?

@chillu
Copy link
Member Author

chillu commented Mar 29, 2016

Gah, it sucks that we have to lose the trait.

@kinglozzer @tractorcow Where do you see the advantage in the trait? For me, it's on the same level as creating a "section" in a class definition through some kind of PHP comment markers. I can't see a reuse for this trait outside of the FormField class. So the only thing it does is confuse devs with less powerful IDEs on where a certain method is defined.

* @package forms
* @subpackage core
*/
class FormField extends RequestHandler {

use SilverStripe\Forms\Schema\FormFieldSchemaTrait;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so this comment is about this entire commit but I have to say that this is quite surprising. I don't see the fact that the trait is used in 1 place as a reason not to break the code into smaller pieces.

Perhaps the trait should mark abstract function getSchemaDataType(), and the implementation of only that method is pulled into FormField?

@sminnee
Copy link
Member

sminnee commented Mar 30, 2016

I'm with @kinglozzer on this one. I think breaking down code into smaller pieces is a pretty fundamental piece of good software design.

I'd shift only the implementation of getSchemaDataType() (and the protected property) into FormField, and declare it as abstract in the trait.

@chillu
Copy link
Member Author

chillu commented Mar 30, 2016

OK three votes for traits (@sminnee, @kinglozzer, @tractorcow , one against (@chillu - in this context). I'll add the trait back in, and move getSchemaDataType() and getSchemaComponent() to FormField.

@hafriedlander
Copy link
Contributor

Jumping in for no good reason, while I agree that breaking code into smaller pieces is good design, using a trait to do it seems a bit smelly. You can't inject or replace traits, and they share the same access space as the class they're attached to. Why isn't this a seperate class with a decorator pattern? If the argument is "Form Fields should be able to override specific behaviour", the argument for moving that out into a different class is the same as the argument for moving the base trait behaviour into a seperate class.

@tractorcow
Copy link
Contributor

I like traits but I don't care that strongly about it in this case. I'd have done it if it were up to me, but happy for it to be left out for this PR.

Why isn't this a separate class with a decorator pattern?

Also fine with me.

@tractorcow
Copy link
Contributor

I'm going to merge, since "moving schema into a trait, extension, or other class" isn't something that we can't do at a later date. The tests pass so let's keep moving onwards.

@tractorcow
Copy link
Contributor

Maybe I'll wait for @chillu to respond to the query on using constants; Since that seems to be the only un-addressed point here.

See #5239 (comment)

@sminnee
Copy link
Member

sminnee commented Mar 30, 2016

OK well, if Ingo and Damian are both okay with the solution, I'm not going to block merge of this.

@sminnee
Copy link
Member

sminnee commented Mar 30, 2016

However, it would be good to hear if @kinglozzer thinks we should hold fire and refactor this some more before merging...

@kinglozzer
Copy link
Member

Yeah moving it back into a trait is my preference, but I don’t feel strongly enough about it to block merge, so +1. Same for the constants I guess - there’s no reason we can’t merge this and address them in a subsequent PR

@chillu chillu force-pushed the pulls/4.0/form-schema-fixes branch from 9a424e3 to 55f1293 Compare March 30, 2016 10:00
@chillu
Copy link
Member Author

chillu commented Mar 30, 2016

OK, updated the PR according to feedback:

  • Added a comment regarding definition of $schemaDataType in subclasses
  • Using constants: FormField::SCHEMA_DATA_TYPE_*. A bit lengthy, but just TYPE might be too confusing.
  • Haven't re-instated the trait, as per latest feedback.

@tractorcow tractorcow merged commit 0fa7271 into silverstripe:master Mar 30, 2016
@tractorcow tractorcow deleted the pulls/4.0/form-schema-fixes branch March 30, 2016 20:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants