Jump to content

Inputfield::attr() won't set / general cart submission question


cst989
 Share

Recommended Posts

Hi all, I have a simple issue with inputfield:attr() not setting a value.

I'm doing this in ready.php

$forms->addHook('FormBuilderProcessor::renderReady', function($e) {
    $form = $e->arguments(0);
    if($form->name != 'enquiry_form') return;
    $inputfield = $form->getChildByName('cart');
    if($inputfield) {
        $cart = json_encode(wire('session')->get('cart'));
        $inputfield->attr('value', $cart);
    }
});

This is lifted straight from formbuilder hooks and inputfield method specs.

If I echo $cart it's definitely a simple string of json, but nothing appears in the cart field (which is a plain text field - later to change to hidden)

Is there something obvious I might've overlooked here?

As a side question, can anyone think of a better way to do this - add my cart to a Formbuilder submission (it's not a shop, just an enquiry) - while still storing entries easily in the formbuilder entries list.

Link to comment
Share on other sites

  • cst989 changed the title to Inputfield::attr() won't set / general cart submission question

I think you need to return the modified argument

$forms->addHook('FormBuilderProcessor::renderReady', function($e) {
    $form = $e->arguments(0);
    if($form->name != 'enquiry_form') return;
    $inputfield = $form->getChildByName('cart');
    if($inputfield) {
        $cart = json_encode(wire('session')->get('cart'));
        $inputfield->attr('value', $cart);
        // you can try var_dump($inputfield->attr('value') here to see if the value was set
    }
	$e->arguments(0, $form); // this will return the modified form object to the hooked method
});

 

  • Thanks 1
Link to comment
Share on other sites

Var dump returns a string but still nothing in the field...

It's occurring to me now, maybe I'm misunderstanding this whole thing? Is this actually meant to be populating the html field itself? Will this still work if I'm using embed method C or D on Formbuilder?

I'm trying to avoid populating this with javascript but that would be so much easier... perhaps I'll just abandon it.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...