formers / former

A powerful form builder, for Laravel and other frameworks (stand-alone too)
https://formers.github.io/former/
1.34k stars 204 forks source link

Add method to create new field from string #570

Closed dariusj18 closed 6 years ago

dariusj18 commented 6 years ago

Give the ability to create a new element.

ex. Former::field($fieldType)

The reason to do it this way instead of Former::$fieldType is because you can't do this Former::$obj->getFieldType(). Also it makes it so if some sort of sanitation is require it can be handled behind the scenes.

claar commented 6 years ago

To get the type and class of a created element, you can currently use:

1. Type of text is {{ Former::text('test')->getType() }}<br>
2. Class of text is {{ get_class(Former::text('test')) }}

// Output:
1. Type of text is text
2. Class of text is Former\Form\Fields\Input

Does that help?

dariusj18 commented 6 years ago

I think you misunderstood my intent.

The idea is to allow creation of an element with Former::field('text', 'fieldname') in addition to Former::text('fieldname')

claar commented 6 years ago

Hi @dariusj18 -- sorry about that. I did understand your request, but I'm trying to understand your reasoning for the request.

You said that the reason to add this new syntax was to be able to run Former::$obj->getFieldType() and for "some sort of sanitation" to be handled behind the service. I'm not understanding what you mean by these benefits, as I believe you can already do both of these things using the current syntax. Can you expand on your thoughts?

dariusj18 commented 6 years ago

Former::$obj->getFieldType() throws an error in PHP.

The sanitization was just an extra reason it might be useful.

claar commented 6 years ago

What is Former::$obj? And why would you expect ->getFieldType() to be a method on Former::$obj? I'm not following you I'm afraid. I've posted a thorough example above of how Former works currently -- could you do the same for your suggested syntax?

As an aside, I've noticed that you tend to post very fast, very terse issues and replies on GitHub. May I politely request that you slow down a bit and try to communicate more thoroughly? Perhaps with complete code examples, and reading over your post a few times to edit for clarity?

I'm sure you're very busy. I promise you that the maintainers of most repositories are equally busy. If you would take the time to thoroughly explain your ideas, it would make the process of collaborating much more enjoyable for everyone involved.

Thank you for taking the time to contribute these issues and suggestions to this project and others.

dariusj18 commented 6 years ago

If I have object $obj with method getFieldType. (In this case $obj is an object I have to assemble the form from database fields.)

Currently I can run

$fieldType = $obj->getFieldType()
Former::$fieldtype($obj->getFieldName)

however I would like to accomplish this in one line. I propose a new method signature on the \Former\Former class. My name might not be ideal, but as an example createFieldByType

you could add it to the __call,

if($method == 'createFieldByType') {
  $fieldType = array_shift($parameters);
  $field = $this->dispatch->toFields($fieldType, $parameters)
} else {
  $field = $this->dispatch->toFields($method, $parameters);
}

then my code would look like this

Former::createFieldByType($obj->getFieldType(), $obj->getFieldName)
claar commented 6 years ago

Gotcha.. that makes way more sense.

I agree that such a feature could be useful, but I don't see a trivial way to add it at the moment and I don't have a ton of time to devote to this project.

A PR would be welcome and should include 100% test coverage.