Comments (24)
What about condition?
$form->addSelect('country', 'Country:', $countries);
$form->addSubmit('submit', 'Send');
$form['country']
->addConditionOn($form['submit'], $form::SUBMITTED)
->setRequired('Choose country');
from forms.
How does this help me when I have those countries loaded using AJAX and have $select->checkAllowedValues = FALSE;
? I need to disable scope validation for that select for the submit to work.
Sorry if I haven't made myself clear about that.
from forms.
Why you need to disable scope validation for select?
from forms.
Because I am stupid, $select->setPrompt()
is what I needed, not to disable validation scope. But I still think that disabling the scope per field would be better.
from forms.
@dg I am closing this as it is probably unnecessary to have it here.
from forms.
@dg I have to disable validation scope when I use $select->setRequired();
, $select->setPrompt()
is not applicable in this case.
The problem is when I submit form with AJAX data, the value of select is outside $data
variable, I have to get it using $form->getHttpData()
which is okay but the form fails to submit because value of select is required and there is none in the time of checking.
I am back to my initial issue, disabling validation scope in this way is not friendly.
from forms.
Can you try to implement any solution?
from forms.
@dg Well I have a few ideas
First idea
- Add
$validateScope
property toBaseControl
which will be bool. - Allow disabling scope per control using
$control->disableValidationScope();
// just examples - Before the form is builded, iterate over each control and build validationScope and assign it to SubmitButton
- But what if there is more than one
SubmitButton
control? Is that possible? That would cause problems.
Second idea
- Add class
Nette\Forms\ValidationScopeBuilder
- Implement
\Traversable
or extend\ArrayObject
- Pass form instance into it
- Call
$vsb->disableScopeFor('control');
- Pass instance into
$submit->setValidationScope($vsb);
- This will get all current controls from the form, remove those disabled and foreach in
setValidationScope
will iterate over them.
Third idea
- Each BaseControl can get it's parent Form instance
- We could add disableValidationScopeFor('control') on the SubmitButton control
- Add those control names into list and then just select all controls from form and leave out disabled ones
What do you think? Does any of those sound good?
from forms.
the form fails to submit because value of select is required and there is none in the time of checking.
Why not just set $select->checkAllowedValues = FALSE
? Redesigning validation scope seems like overkill.
EDIT: Seems like we would need to modify ChoiceControl::getValue()
to respect $checkAllowedValues
as well. What do you think?
EDIT: No we can't do it, it would be a BC break.
from forms.
@JanTvrdik Yeah, it is particularly troubling issue this one. I don't mind working with validation scope, it's just not very intuitive to work with.
from forms.
I don't think that validation scopes should be used for this kind of issue at all.
Another random idea
$form->addSelect('country', 'Country', [$form->getHttpData('country') => '...'])
->setRequired();
from forms.
@JanTvrdik I don't think that validation scopes should be used for this kind of issue at all.
Really? Why?
With your another random idea, I see potential problem with setting default value for editing.
from forms.
@juniwalk Because if you don't want the value to be validated, you should not make the select box required. What's the point of setting validation rules if they should be ignored?
from forms.
@JanTvrdik I do want the value validated, and I need it to be required, but that does not play well with AJAX values feeded in to the select box ...
from forms.
I do want the value validated
Could you elaborate on what exactly and where do you validate?
from forms.
@JanTvrdik In this context I ment the requirement validation as i use this to suggest user names and I get back just int (user Id) which I then in the onSuccess (probably not properly done but that is how I use it).
from forms.
I'm sorry, I still don't understand it. How could you use the server-side validation when the select box is not validated at all because it is not included in the validation scope? What did I miss?
from forms.
@JanTvrdik Ignore it, we diverged from the problem here, validation is not the issue, getting the value is.
from forms.
@JanTvrdik Yes, and to disable the scope is absolute horrendous work to do (especially for large forms).
from forms.
->setValidationScope(array_diff_key($form->getControls(), ['country' => TRUE]))
is horrendous?
edit: $form->getControls()
returns ArrayIterator, so iterator_to_array($form->getControls())
.
from forms.
@dg Hey, now that looks much better, would you mind if I send PR to docs with this example to help others like me?
from forms.
Of course I'll be happy. But try it please, I wrote from my head.
from forms.
@dg So I tried that array_diff_key and it throws an error
PHP Warning: array_diff_key(): Argument #1 is not an array in ./app/forms/ProductForm.php:136
EDIT: I'd have to combine it with iterator_to_array()
. Having $select->disableValidationScope('submit');
would be really lovely.
from forms.
Hi,
I revisited this old issue. Now when I am more skilled in Nette I basically never modify validationScope, I do my own validation in onValidate event.
I was modifying it before to keep the clientSide validation so user gets alerted before submitting that he did not select something required, however I can live without it.
I think we can close this issue.
from forms.
Related Issues (20)
- SubmitButton without caption throws depracated error on render on PHP 8.1
- UploadControl MaxLenght message %i HOT 1
- private const Array (request to change to public)
- Validation problem with rule MIME_TYPE
- Unable to add class to input programatically when rendered using n:name
- netteForms.js doesn't toggle properly when form is reset
- Cannot call method startTag() / endTag() on string
- addDate(), addTime(), addDateTime() - how to get nullable values?
- netteForms: min/max wrong validation of numbers HOT 1
- setValue() as well as setDefaultValue() don't set the value and the field shows error states instead HOT 2
- ChoiceControl::getRawValue(): Return value must be of type string|int, null returned HOT 1
- Validation of MimeType rule fails when FileControl->getContentType returns null HOT 1
- src/assets/netteForms.js broken in 3.2-dev HOT 1
- `Nette\Forms\Blueprint::dataClass($form)`: Undefined variable `$bp` HOT 1
- Number inputs format shouldn't be always validated HOT 7
- Number input with maxlength validation always fails HOT 2
- Compound condition does not toggle element
- Error in JS validation for rule Form::Image
- Implementation of setOmitted() to Nette\Forms\Rules
- errors cummulate when instances of Nette\HtmlStringable
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from forms.