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
html form validation initial steps with test html file #13969
Conversation
Thanks for the pull request, and welcome! The Servo team is excited to review your changes, and you should hear from @jdm (or someone else) soon. |
Heads up! This PR modifies the following files:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good start! Let me know if any of my comments are unclear!
@@ -22,6 +22,7 @@ use dom::node::{Node, UnbindContext, document_from_node, window_from_node}; | |||
use dom::nodelist::NodeList; | |||
use dom::validation::Validatable; | |||
use dom::validitystate::ValidityState; | |||
use dom::validitystate::ValidityStatus; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be combined with the previous line: use dom::validitystate::{ValidityState, ValidityStatus};
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing out this. Have made changes everywhere where we added two lines instead of one.
|
||
pub trait Validatable {} | ||
pub trait Validatable { | ||
fn as_element_validatable(&self) -> ∈ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method does not belong in this trait. We want a method on Element like as_maybe_activatable that returns an instance of the Validatable
trait. This will allow us to invoke methods via that trait.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that looks right!
@@ -1,5 +1,10 @@ | |||
/* This Source Code Form is subject to the terms of the Mozilla Public | |||
* License, v. 2.0. If a copy of the MPL was not distributed with this | |||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | |||
use dom::element::Element; | |||
use dom::validitystate::ValidityStatus; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused?
@@ -7,6 +7,7 @@ use dom::bindings::codegen::Bindings::ValidityStateBinding::ValidityStateMethods | |||
use dom::bindings::js::{JS, Root}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file changed mode from 644 to 755, which is weird.
@@ -7,6 +7,7 @@ use dom::bindings::codegen::Bindings::ValidityStateBinding::ValidityStateMethods | |||
use dom::bindings::js::{JS, Root}; | |||
use dom::bindings::reflector::{Reflector, reflect_dom_object}; | |||
use dom::element::Element; | |||
use dom::validation::Validatable; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused?
fn as_element_validatable(&self) -> &Element { | ||
self.upcast() | ||
} | ||
fn is_instance_validatable(&self) -> bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method likely needs to implement the various cases where input elements are barred from constraint validation (click the bold text to see a list of places where the term is used).
@@ -465,6 +466,15 @@ impl HTMLFormElement { | |||
/// https://html.spec.whatwg.org/multipage/#statically-validate-the-constraints | |||
fn static_validation(&self) -> Result<(), Vec<FormSubmittableElement>> { | |||
let node = self.upcast::<Node>(); | |||
for child in node.traverse_preorder() { | |||
match child.downcast::<Element>() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if let Some(el) = child.downcast::<Element>() {
if el.disabled_state() {
continue;
}
let validatable = match el.as_maybe_validatable() {
Some(v) => v,
None => continue,
};
if !validatable.is_instance_validatable() {
continue;
}
// invoke some other method that returns true if the form element validates, false otherwise.
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much for this, got a better idea about the flow now from static validation to individual elements validation. Will soon post the changes.
pub trait Validatable { | ||
fn as_element_validatable(&self) -> ∈ | ||
fn is_instance_validatable(&self) -> bool; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will need a method added here that performs the appropriate validation checks, returns true if the element validates, and false otherwise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello
As described in the initial steps for the project, we have some doubts. Is it possible for you to give your email id so that we can have a discussion there?
Regards
Bhavya
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the right track!
let mut validate_flags: ValidationFlags = ValidationFlags::empty(); | ||
// Need match and insert ValidationFlags later | ||
validatable.validate(validate_flags); | ||
// Invoke validata here that returns true if the form element validates, false otherwise |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment seems unnecessary now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed unnecessary comments as of now.
} | ||
let mut validate_flags: ValidationFlags = ValidationFlags::empty(); | ||
// Need match and insert ValidationFlags later | ||
validatable.validate(validate_flags); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would go ahead and call validatable.validate(ValidationFlags::empty())
for the time being.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change updated.
@@ -1162,6 +1159,11 @@ impl Validatable for HTMLInputElement { | |||
} | |||
true | |||
} | |||
fn validate(&self, validate_flags: ValidationFlags) -> bool { | |||
if validate_flags.is_empty() {} | |||
// Need more flag check for different validation types later |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the stub implementations should just consist of true
, nothing else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stub implementation updated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
returning only true in here, leaves unused variable names warning. Any ideas or we can avoid that for time being?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a _
prefix to variables that don't get used.
const STEP_MISMATCH = 0b0010000000, | ||
const BAD_INPUT = 0b0100000000, | ||
const CUSTOM_ERROR = 0b1000000000, | ||
const VALID = 0b0000000000, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This flag doesn't make sense here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you referring to VALID Flag?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
InputType::InputCheckbox => {}, | ||
InputType::InputRadio => {}, | ||
InputType::InputPassword => {}, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should either return real values for these types or remove the match until we actually use it.
if el.disabled_state() { | ||
continue; | ||
} | ||
let validatable = match el.as_maybe_validatable() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as_maybe_validatable is still missing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as_maybe_validatable method is added and defined in element.rs file. Kindly refer below link:
https://github.com/servo/servo/blob/master/components/script/dom/element.rs#L2296
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, in future linking to the code in question rather than providing screenshots will make that more clear :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Thanks for the suggestion :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, two more requests for changes. I missed some code before that we should integrate with, and there's no need to have all the duplicated stub method implementations right now.
pub trait Validatable {} | ||
pub trait Validatable { | ||
fn is_instance_validatable(&self) -> bool; | ||
fn validate(&self, validate_flags: ValidationFlags) -> bool; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that every implementation is a stub right now, I propose we do:
pub trait Validatable {
fn is_instance_validatable(&self) -> bool { true }
fn validate(&self, _validate_flags: ValidationFlags) -> bool { true }
}
and remove the stub implementations from the other files:
impl Validatable for HTMLTextAreaElement {}
// pass empty flags initially and set appropriate flags according to the type of element called | ||
validatable.validate(ValidationFlags::empty()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I didn't notice previous that there is code below this that we should be integrating into instead. We'll want to return Some(el)
if validate
returns false, and None
otherwise (since it's collecting elements that do not validate).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello Josh,
Trying to integrate the code, but every time it is giving some different kind of error on compilation.
Please correct me if my approach is wrong here:
This section of code
let invalid_controls = node.traverse_preorder().filter_map(|field| { |
will traverse through the page and then the code which we wrote for calling validate method will be written in that section. if validate method returns true then we return None, else return Some(el) which is the element for which validation got failed. And collect will collect those Some(el) {elements}.
Now, I tried writing the code but I am struggling with rust specific errors here.
Please guide me here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please post your code somewhere and the errors it outputs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Manishearth Thanks for helping out. Shall I give you the code file I wrote to integrate the two parts here or do you want me to email you that particular section?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please push the code to a git branch somewhere, and link to it (as well as mentioning the error text) here. It's also fine if you just push it as a new commit on this same pull request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hello
Pushing as a new commit with unsuccessful compilation, that will create issues. If you are ok, I can share the portion of code file here by attaching a document and also mention what kind of error I am getting. Actually error is related to rust syntax and I believe its nothing major.
Also, if you are ok sharing your email ID, I can email you the details.
Regards
Bhavya
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pushing as a new commit with unsuccessful compilation, that will create issues.
It's really fine to push non-compiling code to a PR; I do it all the time. And you can always push to a different branch
git checkout -b temp-branch
(make the commit)
git push origin temp-branch
git checkout <name of original branch>
But if you feel you need to email it, myusername@gmail.com will work.
@Manishearth can you take a look at the most recent response in my absence as well? |
will do |
if validate_flags.is_empty() {} | ||
// Need more flag check for different validation types later | ||
fn validate(&self, _validate_flags: ValidationFlags) -> bool { | ||
// call stub methods defined in validityState.rs file here according to the flags set in validate_flags | ||
true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there's a miscommunication here, the idea is to just have impl Validatable for HTMLTextAreaElement {}
instead of explicitly writing out the fn definitions out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated, thanks for pointing it out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a couple last stylistic nits and we can merge this!
let validatable = match el.as_maybe_validatable() { | ||
Some(v) => v, | ||
None => return { None } | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: indentation here is weird.
let validatable = match el.as_maybe_validatable() {
Some(v) => v,
None => return None,
};
if !validatable.is_instance_validatable() { | ||
None | ||
} | ||
else if validatable.validate(ValidationFlags::empty()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: we use } else if
rather than splitting them on separate lines.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code indentation done as per feedback. Thanks
pub trait Validatable {} | ||
pub trait Validatable { | ||
fn is_instance_validatable(&self) -> bool { true} | ||
fn validate(&self, _validate_flags: ValidationFlags) -> bool { true} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: add a space before }
on these lines.
Looks good! Could you squash the commits into one? |
…thods, added code to handle validations
9bff9f3
to
2a40877
Compare
@bors-servo: r+ |
📌 Commit 2a40877 has been approved by |
html form validation initial steps with test html file <!-- Please describe your changes on the following line: --> Added code for initial steps in html form validation. 1. Added methods for trait validatable 2. implemented stub methods for elements like HTMLInputElement, HTMLButtonElement, etc <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [X ] `./mach build -d` does not report any errors - [ X] `./mach test-tidy` does not report any errors - [ ] These changes fix #__ (github issue number if applicable). <!-- Either: --> - [ ] There are tests for these changes OR - [ ] These changes do not require tests because _____ <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/13969) <!-- Reviewable:end -->
💔 Test failed - mac-rel-wpt1 |
@bors-servo: retry |
⚡ Previous build results for arm32, arm64, linux-dev, linux-rel-css, linux-rel-wpt, mac-dev-unit, mac-rel-css, mac-rel-wpt2, windows-dev are reusable. Rebuilding only mac-rel-wpt1... |
☀️ Test successful - arm32, arm64, linux-dev, linux-rel-css, linux-rel-wpt, mac-dev-unit, mac-rel-css, mac-rel-wpt1, mac-rel-wpt2, windows-dev |
Added code for initial steps in html form validation.
3. Added code to call methods from static validation inHTMLFormElement.rs file
./mach build -d
does not report any errors./mach test-tidy
does not report any errorsThis change is