Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[DomCrawler] Attach label to form fields#18322

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed

Conversation

@carlosV2
Copy link

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed tickets
LicenseMIT
Doc PR

While building a software to extract information from a webpage, I need to fill a form with hints on the fields labels.

Example:

<form>   ...<labelfor="my_field">Input the result of 3 + 4:</label><inputtype="text"id="my_field"name="my_field"/>   ...</form>

I thought it would be handy to have the field labels attached to each field.
So given the previous form you could now do:

$form->get('my_field')->getLabel()->textContent;

DavidGarciaCat reacted with thumbs up emoji
@carlosV2carlosV2 changed the titleAttach label to form fields[DomCrawler] Attach label to form fieldsMar 26, 2016
*
* @return \DOMNode|null The \DOMElement of the associated label tag or null if none was found
*/
privatefunctionfindAssociatedLabel(\DOMElement$node)
Copy link
Contributor

Choose a reason for hiding this comment

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

Have you considered moving this implementation to the FormField'sgetLabel() method? You wouldn't have to add a new argument to FormField's constructor, and the logic wouldn't be called if the label wasn't accessed.ownerDocument property is available on the$node that's already passed to the FormField.

Copy link
Author

Choose a reason for hiding this comment

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

That's a good point. I'll refactor it.

@carlosV2carlosV2force-pushed theattach-label-to-form-fields branch fromefd3577 to93f8d8fCompareApril 7, 2016 15:38
@carlosV2
Copy link
Author

Sorry@stof, I realised while you where commenting it. It should be fixed now.

if ($labels->length >0) {
return$labels->item(0);
}

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 id-based logic should have higher priority

@xabbuh
Copy link
Member

What about something like this:

<form><div><labelfor="my_field">Input the result of 3 + 4:</label></div><div><inputtype="text"id="my_field"name="my_field"/></div></form>

@carlosV2
Copy link
Author

@xabbuh it will work as far as the label is within the form node computed by the crawler. In this case, parenting would fail but the label will still be found by thefor attribute.

If it is suitable, I can add a new test to prove this although it seems to be working on my local.

@xabbuh
Copy link
Member

@carlosV2 You are right. I misread the way the XPath object is created.

@fabpot
Copy link
Member

Thank you@carlosV2.

@fabpotfabpot mentioned this pull requestOct 27, 2016
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

7 participants

@carlosV2@xabbuh@fabpot@jakzal@stof@javiereguiluz@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp