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

html form validation initial steps with test html file #13969

Merged
merged 1 commit into from Nov 25, 2016

Conversation

bbansalWolfPack
Copy link
Contributor

@bbansalWolfPack bbansalWolfPack commented Oct 29, 2016

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

3. Added code to call methods from static validation inHTMLFormElement.rs file

  • [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).
  • There are tests for these changes OR
  • These changes do not require tests because _____

This change is Reviewable

@highfive
Copy link

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.

@highfive
Copy link

Heads up! This PR modifies the following files:

  • @fitzgen: components/script/dom/htmltextareaelement.rs, components/script/dom/htmlbuttonelement.rs, components/script/dom/validitystate.rs, components/script/dom/validation.rs, components/script/dom/htmlformelement.rs, components/script/dom/htmlselectelement.rs, components/script/dom/htmlinputelement.rs, components/script/dom/htmlobjectelement.rs
  • @KiChjang: components/script/dom/htmltextareaelement.rs, components/script/dom/htmlbuttonelement.rs, components/script/dom/validitystate.rs, components/script/dom/validation.rs, components/script/dom/htmlformelement.rs, components/script/dom/htmlselectelement.rs, components/script/dom/htmlinputelement.rs, components/script/dom/htmlobjectelement.rs

@highfive highfive added the S-awaiting-review There is new code that needs to be reviewed. label Oct 29, 2016
@highfive
Copy link

warning Warning warning

  • These commits modify script code, but no tests are modified. Please consider adding a test!

Copy link
Member

@jdm jdm left a 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;
Copy link
Member

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};

Copy link
Contributor Author

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) -> ∈
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Method as_maybe_validatable is defined in element.rs file which returns an instance of Validatable trait. Attaching the screenshot for the same. Please verify once.

as_maybe_validatable

Copy link
Member

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;
Copy link
Member

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};
Copy link
Member

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;
Copy link
Member

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 {
Copy link
Member

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>() {
Copy link
Member

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.
}

Copy link
Contributor Author

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) -> &Element;
fn is_instance_validatable(&self) -> bool;
}
Copy link
Member

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.

Copy link
Contributor Author

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

@jdm jdm added S-needs-code-changes Changes have not yet been made that were requested by a reviewer. and removed S-awaiting-review There is new code that needs to be reviewed. labels Oct 31, 2016
@highfive highfive added S-awaiting-review There is new code that needs to be reviewed. and removed S-needs-code-changes Changes have not yet been made that were requested by a reviewer. labels Nov 5, 2016
Copy link
Member

@jdm jdm left a 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
Copy link
Member

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.

Copy link
Contributor Author

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);
Copy link
Member

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.

Copy link
Contributor Author

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
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

stub implementation updated

Copy link
Contributor Author

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?

Copy link
Member

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,
Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

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 => {},
}
Copy link
Member

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() {
Copy link
Member

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.

Copy link
Contributor Author

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

Copy link
Member

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 :)

Copy link
Contributor Author

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 :)

@jdm jdm added S-needs-code-changes Changes have not yet been made that were requested by a reviewer. and removed S-awaiting-review There is new code that needs to be reviewed. labels Nov 7, 2016
@highfive highfive added S-awaiting-review There is new code that needs to be reviewed. and removed S-needs-code-changes Changes have not yet been made that were requested by a reviewer. labels Nov 8, 2016
Copy link
Member

@jdm jdm left a 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;
Copy link
Member

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());
}
}
Copy link
Member

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).

Copy link
Contributor Author

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.

Copy link
Member

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?

Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Contributor Author

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

Copy link
Member

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.

@jdm jdm added S-needs-code-changes Changes have not yet been made that were requested by a reviewer. and removed S-awaiting-review There is new code that needs to be reviewed. labels Nov 8, 2016
@jdm
Copy link
Member

jdm commented Nov 17, 2016

@Manishearth can you take a look at the most recent response in my absence as well?

@Manishearth
Copy link
Member

will do

@highfive highfive added S-awaiting-review There is new code that needs to be reviewed. and removed S-needs-code-changes Changes have not yet been made that were requested by a reviewer. labels Nov 23, 2016
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
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Member

@jdm jdm left a 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 }
};
Copy link
Member

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()) {
Copy link
Member

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.

Copy link
Contributor Author

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}
Copy link
Member

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.

@jdm jdm added S-needs-code-changes Changes have not yet been made that were requested by a reviewer. and removed S-awaiting-review There is new code that needs to be reviewed. labels Nov 23, 2016
@highfive highfive added S-awaiting-review There is new code that needs to be reviewed. and removed S-needs-code-changes Changes have not yet been made that were requested by a reviewer. labels Nov 23, 2016
@jdm
Copy link
Member

jdm commented Nov 23, 2016

Looks good! Could you squash the commits into one?

@jdm jdm added the S-needs-squash Some (or all) of the commits in the PR should be combined. label Nov 23, 2016
@jdm jdm removed the S-awaiting-review There is new code that needs to be reviewed. label Nov 23, 2016
@highfive highfive added the S-awaiting-review There is new code that needs to be reviewed. label Nov 25, 2016
@jdm
Copy link
Member

jdm commented Nov 25, 2016

@bors-servo: r+
Thanks!

@bors-servo
Copy link
Contributor

📌 Commit 2a40877 has been approved by jdm

@highfive highfive added S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. and removed S-awaiting-review There is new code that needs to be reviewed. S-needs-squash Some (or all) of the commits in the PR should be combined. labels Nov 25, 2016
@bors-servo
Copy link
Contributor

⌛ Testing commit 2a40877 with merge beec035...

bors-servo pushed a commit that referenced this pull request Nov 25, 2016
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 -->
@bors-servo
Copy link
Contributor

💔 Test failed - mac-rel-wpt1

@highfive highfive added S-tests-failed The changes caused existing tests to fail. and removed S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. labels Nov 25, 2016
@bors-servo
Copy link
Contributor

⚡ 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...

@bors-servo
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-tests-failed The changes caused existing tests to fail.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants