auraphp / aura.input Goto Github PK
View Code? Open in Web Editor NEWTools to describe HTML form fields and values.
License: MIT License
Tools to describe HTML form fields and values.
License: MIT License
I would like to add an addRule
, so that we can add multiple error messages.
This is from the discussion in irc http://colabti.org/irclogger/irclogger_log/auraphp?date=2013-03-13 . For a quick reference I am keeping.
17:04 harikt 1 qn , currently the filter can only hold one filter for the field, why not add it for multiple ones?
17:04 harikt I mean for the Aura.Input Filter
17:04 harikt not Aura.Filter
17:14 pmjones oh, so, on Aura.Input filter, it's intended as a placeholder for a "real" filter system
17:14 pmjones it's the simplest thing that can possibly work as a filter system, but it's clearly not very robust
17:15 pmjones and doing it this way means anyone can add any filter system, and add on the very basic Aura.Input setRule() method
17:16 pmjones hope that helps explain
17:29 harikt not sure whether you understood me clearly .
17:30 harikt pmjones, if you look into this https://github.com/harikt/Aura.Input/blob/6456c130e9c7007badadfd8630102c29101a9419/src/Aura/Input/Filter.php
17:30 harikt You can see the rules field I added as array .
17:31 harikt I agree it is just a place holder, but it can act more powerful like adding more filters than a single rule
17:31 harikt Try adding setRule twice on a field.
17:32 harikt only the last one will be taken , this way we can add multiple rules
18:20 pmjones harikt: yes, that's correct, only the last one gets used.
18:20 pmjones it's "set" rule, not "add" rule
18:21 pmjones and because the rule is a closure, it means you can add any logic you want, meaning you can add multiple checks inside the closure
18:21 pmjones thus, there's no need for multiple rules when using setRule()
18:21 pmjones you add whatever rules you want inside the closure
Yes I noticed the set
and add
, so I guess we need a addRule
for the setRule
can only set one error message. But it will be good if we have multiple error messages.
I also agree with your point of multiple rules inside the closure, but error message is same makes me feel not a good idea.
Say you need to check the max string length, and whether it is string. So the user should know from the error what the error is. Not just it is not a string or the entered value is incorrect. But a clear understanding of what made wrong.
I also agree this is a simple filter and users can use other filtering / validation system. But I prefer to add it in this simple filter. It is not that much ;-)
Regarding #13, I'm wondering if we could discuss this again?
I feel add an additional addRule
method would be really useful and would avoid developers having to write their own class that extends Filter
or implements FilterInterface
.
I don't think that having two method, setRule
and addRule
, would be confusing for developers.
Also, Filter
currently supports multiple messages per Field
, so I don't think there will be any BC breaking with the messages.
What are your current thoughts?
Cheers
Stephen
We should not set the value for the csrf token. Is it for the user to select the name like __csrf_token
or will it be constant always?
Hi Paul,
there seems something went wrong.
v2 config was added in April 4334ce6
We released the version 1.1.1 on May 31st.
https://github.com/auraphp/Aura.Input/blob/1dd69640b72b1814e6195b986234c5040eaeba5d/composer.json
Which is missing the config for v2 :( .
So may be release merge didn't worked as expected ? See master branch composer.json which has no change. Aah I guess this was an error in the build script.
Some times we will be having input of
<input type="text" name="title[]" id="title0" value="some value" />
In this case the filter can be
$filter->addSoftRule('title', $filter::IS, 'alnum');
Which needs to be validated for all the title array. Rather than we writing title0
it is always wise to have the title[]
field.
We want to find a way that passing the value to filter can be addressed. Not sure whether it should come in Aura.Filter
or Aura.Input
I was working with a special requirement and I feel there should be a way to set the error messages for the field.
The reason is some validators are validating the model like username already exists in the database, and it will be nice if we can set the error messages to the corresponding field name.
I will work on both the Aura.Input
and Aura.Filter
to have a method setMessages($field = null)
.
What do you think? Go with it ?
Current packagist 'dev-develop' points old reference.
https://packagist.org/packages/aura/input
reference: 5c949cf ( 2013-05-09 21:22 UTC)
Its not latest.
https://github.com/auraphp/Aura.Input/commits/develop
Web hook failed ?
It will be good to have hasField
method for when we are adding a AntoCsrf
class, if it is not authenticated the field __csrf_token
will not be added and it will thow an exception
Uncaught exception 'Aura\Input\Exception\NoSuchInput' with message '__csrf_token'
Form::getIterator() doesn't work.
$form = new Form(new Builder, new Filter);
$form->setField('foo');
$form->setField('bar');
$iterator = $form->getIterator();
'Aura\Input\Exception\NoSuchInput : fields' exception thrown with this code.
I noticed currently when the id is not specified the id field is null, what about assigning the same name for the id, if it is not specified..
I feel this is good option else we may want to specify at many times,
Can we add a FormFactory to the default.php
<?php
$di->params['Aura\Input\FormFactory'] = [
// a map of form names to form factories
'map' => [
'map-name' => $di->newFactory('classes'),
],
];
$di->set('form_factory', $di->lazyNew('Aura\Input\FormFactory'));
$di->setter['Aura\Framework\Web\Controller\AbstractPage']['setFormFactory'] = $di->lazyGet('form_factory');
and the FormFactory
as
<?php
namespace Aura\Input;
class FormFactory
{
protected $map = [];
public function __construct($map = [])
{
$this->map = $map;
}
public function newInstance($name)
{
if (! array_key_exists($name, $this->map)) {
throw new \Exception("No form with the name found");
}
$factory = $this->map[$name];
$form = $factory();
return $form;
}
}
That way everyone don't need to spend more time on making things to work. What do you think ? I can give a PR if so.
I'm evaluating aura for using it as a form component. It looks very promising. Thanks for your work!
I am struggling with fieldsets at the moment. I would like to use them to group my input fields into them and render a html fieldset. Setting it up is all fine, rendering as well. But I struggle with filling the field data.
Did I miss something? Is there another way? Or what is the purpose of the fieldsets/collections?
Aura.Input/src/Aura/Input/Fieldset.php
Line 350 in c46ee2c
I really like the idea of collections and fieldsets in Aura, but I struggle at some points. Here is another one.
At the moment an collection can only be rendered if any data is filled in (by a model, by post etc.). It would be great add an empty fieldset row or at least to get the fieldset so I could create empty rows in the View of the collection.
public function addEmptyFieldset()
{
$index = $this->count();
$new = $this->newFieldset($index);
$this->fieldsets[$index] = $new;
return $new;
}
Hello,
I'm dealing with a situation where I need to use $fieldset->options
during validation. There's no way to inject it in through the factory, and no way to call $fieldset->setOptions()
. For example:
class PriceRangeFieldset extends Fieldset {
public function init() {
$this->setField('low', 'number');
$this->setField('high', 'number');
$this->addRule('low', "The {$this->options['label']} low field only accepts digits.", function ($value) {
return $value === null || $value === '' || ctype_digit($value);
});
$this->addRule('high', "The {$this->options['label']} high field only accepts digits.", function ($value) {
return $value === null || $value === '' || ctype_digit($value);
});
}
}
And in the form:
class MyForm extends Form {
public function init() {
$this->setFieldset('design', 'price-range')
->setOptions(['label' => 'Design']);
$this->setFieldset('development', 'price-range')
->setOptions(['label' => 'Development']);
}
}
Was there a choice to prevent this use-case?
I am mixing the packages, sorry. But consider
$data = $_POST;
$form->fill($data);
$form->filter();
In this case we get a filtered data of the fields, it have. But in many cases it will be like $_POST
having more values. So I feel there is a need for $form->getValues()
or something like that which we only get the values of the fields it have, removing the extra post values.
What do you think ?
Hi @pmjones ,
I wish it is a good idea to rename toArray than asArray .
https://github.com/auraphp/Aura.Input/blob/develop/src/Aura/Input/Field.php#L131
When we look into SplFixedArray::toArray
is already naming in php. http://php.net/manual/en/splfixedarray.toarray.php
So less confusions ?
I feel there is an inconsistency in the output provided by both.
For eg ๐
public function getField($name)
{
$value = isset($this->values[$name])
? $this->values[$name]
: null;
return ['name' => $name]
+ $this->fields->get($name)->asArray()
+ ['value' => $value];
}
public function getFields()
{
return $this->fields;
}
The getField
returns an array and getFields
returns a FieldCollection
.
Wondering whether it is good to have return Field
itself than an array for getField
.
https://github.com/auraphp/Aura.Input/blob/develop/src/Aura/Input/Form.php#L92
For some time I have been excited about the Filter_Interface
package, and the
ability to integrate Input
and Filter
. However, now I am questioning if
that's right.
Filter
is for validating and sanitizing. That's my domain logic, right?
Input
is mainly about describing (HTML) inputs. That's presentation, right?
I don't need to inject an object into my domain that describe the inputs. I need
to take the output of my domain (errors), and pass them to the thing describing
the UI, no?
// Domain
class CreateThing
{
//...
public function __invoke(array $input)
{
$this->payload->setInput($input);
// No reason i need to know about describing HTML UI stuff here
if (! $this->filter->apply($input)) {
$failures = $this->filter->getFailures();
return $this->payload
->setStatus(Status::INVALID)
->setOutput($failures);
}
//...
}
}
// Responder
class CreateThingResponder
{
//...
protected function invalid()
{
$this->response = $this->response->withStatus(HTTP_STATUS::INVALID);
$this->view->setScript('thing/create')
$this->view->addData(
[
'input' => $this->payload->getInput(),
'errors' => $this->payload->getOutput()
]
);
$this->renderViewToResponse();
}
}
// view: create/thing.php
$form = new CreateThingForm;
if ($this->input) {
$form->setInput($this->input);
}
if ($this->errors) {
$form->setErrors($this->errors);
}
// This is the only time i need information describing the HTML UI
echo $this->render('form', ['form' => $form]);
Are we sure Input
needs to know about validation outside of just understanding
that errors exist?
Since Input
has been stuck in 1.x land, here's how I've been doing things for a little bit:
https://github.com/jnjxp/jnjxp.form
Thoughts?
Maybe I'm wrong, but I kinnda think Input
should just deal with describing UI
stuff, and not know about validation and sanitization logic.
Hi all,
I am currently cleaning up the Aura.Input 1.x branch in order to move it to 3.x.
Are there any feature request / problems we need to resolve ?
Please create issues or discuss here.
Thank you
What is the future of this project? In my opinion, it is feature complete and only needs to have the latest versions of PHP (7.3, 7.4 & 8.0 as of this writing) tested via travis or Github Actions to ensure it continues to work with the currently supported version of PHP. Will anyone be updating the CI tests for this project?
Hi Paul,
I was working on form, and submissions lately with the ADR .
The form is injected to the Domain\Contact . Now when the request method is post the form is calling fill()
and filter()
.
We are setting the form back to the responder.
One thing probably you may mention will be keep the get and post Actions different. But to me it seems duplicating so much efforts.
I was thinking whether it is good to have a isSuccess()
method and return true / false if a check on filter()
is called. Else nothing is returned.
Another thing is getMessages()
can be checked for errors, but if the form is not submitted the getMessages()
empty and that doesn't means it is success and display the message, the way isSuccess
help us is recognize whether a call to filter has happened rather the user testing again on responder with certain flags set when a request is submitted etc.
Interested to hear your thoughts.
Thank you
With applying Aura.Filter the
$form->filter();
it always throwing the filter errors .
Have a look into the https://github.com/harikt/form-example
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.