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

Provide full form data when using form tags in Blade #5892

Merged
merged 10 commits into from
Aug 11, 2022

Conversation

jacksleight
Copy link
Contributor

@jacksleight jacksleight commented Apr 22, 2022

Attempt at a solution to #5534 based on the convention established in #5599.

Covers all form tags:

  • form:create
  • protect:password_form
  • user:login_form
  • user:register_form
  • user:forgot_password_form
  • user:reset_password_form

This:

@php
Statamic::tag('form:contact')->params([
    'js' => 'alpine',
    'files' => true,
    'redirect' => url('/'),
    'id' => 'my-form',
])->fetch();
@endphp

Will now return this instead of a <form> string:

Screenshot 2022-04-24 at 15 22 09

Adds new formAttrs and formParams methods which basically do what formOpen does but without any of the HTML rendering. formOpen could probably be refactored to use these as well, to avoid the duplication, but I didn't want to mess around with it. Can take a look at that if you like.

@jacksleight jacksleight marked this pull request as draft April 22, 2022 15:33
@jacksleight jacksleight marked this pull request as ready for review April 22, 2022 15:38
@jacksleight jacksleight changed the title Provide full form data to blade views Provide full form when using form tag in Blade Apr 22, 2022
@jacksleight jacksleight changed the title Provide full form when using form tag in Blade Provide full form data when using form tag in Blade Apr 22, 2022
@jacksleight jacksleight changed the title Provide full form data when using form tag in Blade Provide full form data when using form tags in Blade Apr 24, 2022
@wautersj
Copy link

Would be very helpful indeed!
Forms are basically unusable now if you're working with Blade.

@DenyEs
Copy link

DenyEs commented May 19, 2022

I came across this issue yesterday trying to render form using Blade as well. This would help a lot.

@jacksleight
Copy link
Contributor Author

If anyone needs an interim solution for this I have this helper that I've been using as a workaround: https://gist.github.com/jacksleight/04fdc8c1f8d827313a68261ac94f34fa

statamic_form('contact') will return similar-ish data to above, although attrs and params are strings taken from the HTML.

@stuartcusackie
Copy link

stuartcusackie commented May 3, 2023

Great stuff. Any advice on how to render $form['attrs'] as a string / html element? I'm currently using this approach but perhaps there is something built in to Laravel or Statamic for this:

@php
$formAttrs = $form['attrs'];

array_walk($formAttrs, function(&$value, $key) {
    $value = "{$key}='{$value}'";
});

$formAttrs = implode(' ', $formAttrs);
@endphp

<form {!! $formAttrs !!}>

@jacksleight
Copy link
Contributor Author

@stuartcusackie You could use Statamic\Support\Html::attributes() (https://github.com/jacksleight/statamic-cms/blob/3.4/src/Support/Html.php#L19)

@stuartcusackie
Copy link

@jacksleight Perfect! I almost got there on my own... Thanks very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants