Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from

Conversation

carlosV2
Copy link

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
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>
   ...
   <label for="my_field">Input the result of 3 + 4:</label>
   <input type="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;

@carlosV2 carlosV2 changed the title Attach label to form fields [DomCrawler] Attach label to form fields Mar 26, 2016
*
* @return \DOMNode|null The \DOMElement of the associated label tag or null if none was found
*/
private function findAssociatedLabel(\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's getLabel() 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.

@carlosV2 carlosV2 force-pushed the attach-label-to-form-fields branch from efd3577 to 93f8d8f Compare April 7, 2016 15:38
@carlosV2
Copy link
Author

carlosV2 commented Apr 7, 2016

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

return $labels->item(0);
}

if ($this->node->hasAttribute('id')) {
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

xabbuh commented Apr 11, 2016

What about something like this:

<form>
  <div>
   <label for="my_field">Input the result of 3 + 4:</label>
  </div>
  <div>
   <input type="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 the for 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

xabbuh commented Apr 11, 2016

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

@fabpot
Copy link
Member

fabpot commented Jun 14, 2016

Thank you @carlosV2.

@fabpot fabpot closed this in 5c91f6e Jun 14, 2016
@fabpot fabpot mentioned this pull request Oct 27, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants