submitting form to controller#store - forms

laravel 5.5
voyager 1.0
i'm trying to submit a form to a controller with :
<form class="" action="FeedController#store" method="post">
<div class="mui-textfield mui-textfield--float-label">
<input id="title" type="text" name="title" value="" required autofocus>
<label for="title" class="col-md-4 control-label">title</label>
</div>
<div class="mui-textfield mui-textfield--float-label">
<textarea id="description" type="textarea" name="description" value="" required autofocus></textarea>
<label for="description" class="col-md-4 control-label">description</label>
</div>
<input class="mui-btn btn-primary pull-right " type="submit" name="" value="submit">
</form>
within FeedController:
/**
*#param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request){
$data = ReportDatum::create([
'owner_id' => auth()->id(),
'title' => request('title'),
'description' => request('description')
]);
return test;
}
and in the routes:
Route::resource('feed', 'FeedController');
(and/or depending on what I'm trying)
Route::post('store', 'FeedController#store');
what I'm i missing to correctly submit the form?
I'm not using laravelCollective forms

You're missing the csrf_field(). Try adding it after the <form> tag.
Ref: https://laravel.com/docs/5.5/csrf

You should define route at first then it will works:
Resource controller routing
Route::resource('feed', 'FeedController');
or specific simple routing
Route::post('feed', 'FeedController#store');

Same error here. I've commented the line \Greazy\Http\Middleware\VerifyCsrfToken::class which stays in app/Http/Kernel.php and the post method worked in Postman, however it does not work when invoked on the form.

Related

TYPO3 Extbase form referrer wrong casing

I made a plugin which allows frontend users to write news articles. This works fine on the Mac locally. However the New action gives a wrong casing in the __referrer of the form, resulting on the Linux server (which is sensitive for casing) in:
Class Pen\Pennews\Controller\newsController does not exist. Reflection failed.
My controller
class NewsController extends NewsBaseController
...
/**
* New action
*
* #return void
*/
public function newAction()
{
$feUserUid = $GLOBALS['TSFE']->fe_user->user['uid'];
/** #var User $user */
$user = $this->frontendUserRepository->findByUid($feUserUid);
$this->view->assignMultiple([
'user' => $user
]);
}
The form
<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"
data-namespace-typo3-fluid="true">
<f:layout name="Default"/>
<f:section name="content">
<f:form action="create" name="newNews" controller="News" object="{newNews}">
<label for="pennews-title" class="pennews-title">Titel</label>
<f:form.textfield property="title" id="pennews-title" placeholder="Onderwep" required="required"/>
<label for="pennews-teaser" class="pennews-teaser">Teaser</label>
<f:form.textarea id="pennews-teaser" class="pennews-teaser-newPost" name="teaser" rows="3" placeholder="Korte teaser - optioneel"/>
<label for="pennews-bodytext" class="pennews-bodytext">Content</label>
<f:form.textarea id="pennews-bodytext" class="pennews-bodytext-newPost pen-form-textarea" name="bodytext" placeholder="Bericht"/>
<div class="pen-button pennews-new-button text-align-right">
<button class="button pennews-submit-news icon-left" type="submit"><i class="fas fa-pen-square"></i>Maak nieuw bericht</button>
</div>
</f:form>
</f:section>
</html>
Part of the source of the form (taken from local dev) :
<form name="newNews"
action="/vlaggensite?tx_pennews_entry%5Baction%5D=create&tx_pennews_entry%5Bcontroller%5D=News&cHash=4cc2809be1ea9f2f2d3e07a8e396981f"
method="post">
<div>
<input type="hidden" name="tx_pennews_entry[__referrer][#extension]" value="Pennews"/>
<input type="hidden" name="tx_pennews_entry[__referrer][#vendor]" value="Pen"/>
<input type="hidden" name="tx_pennews_entry[__referrer][#controller]" value="news"/>
<input type="hidden" name="tx_pennews_entry[__referrer][#action]" value="new"/>
<input type="hidden" name="tx_pennews_entry[__referrer][arguments]"
value="YTowOnt9dd9a70668d7db0e58fe5097a80fa26ab79028541"/>
<input type="hidden" name="tx_pennews_entry[__referrer][#request]"
value="a:4:{s:10:"#extension";s:7:"Pennews";s:11:"#controller";s:4:"news";s:7:"#action";s:3:"new";s:7:"#vendor";s:3:"Pen";}316417f03bf11f3d860053d7f0c1286795c3db22"/>
<input type="hidden" name="tx_pennews_entry[__trustedProperties]"
value="a:3:{s:7:"newNews";a:1:{s:5:"title";i:1;}s:6:"teaser";i:1;s:8:"bodytext";i:1;}b6e10b48e7d3acab8926ba3318ce3ce1fed6ea2d"/>
</div>
As can be visible in the action, the controller is there News with a capital. However in the refererer the controller has the value "news" - lowercase. What could cause this or how can I fix it with a capital as first letter?
Solved. On ext_localconf.php I had the controllername lowercase.

How to get the post vars from a form via user function

On a Typo3 website a form is integrated. The action should be routed to a typoscript user function.
This is what I tried so far:
The fluid form code (excerpt):
<form action="{f:cObject(typoscriptObjectPath: 'lib.mynlreg')}" method="post">
<input type="text" name="email" placeholder="Ihre E-Mail-Adresse">
<input type="submit" name="send" value="Jetzt registrieren" class="submit" />
</form>
The typoscript lib:
lib.mynlreg = USER_INT
lib.mynlreg {
userFunc = Vendor\Extension\myClass->myFunction
}
And the class:
class myClass {
public function myFunction($content, $conf) {
$arguments = $this->request->getArguments();
$formEmail = $arguments['email'];
return '<div>' . $formEmail . '</div>';
}
}
I expect to get the content of the form field "email", but after submitting the page throws an error. The question is, how do I get the post vars into the user function? Thank you for any help!
$this->request is not available in a userFunc. As gautamsinh mori says, you should use \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('email');, however I'm not sure you understand what the f:cObject ViewHelper does.
With this code, your HTML before submitting the form will be:
<form action="<div></div>" method="post">
<input type="text" name="email" placeholder="Ihre E-Mail-Adresse">
<input type="submit" name="send" value="Jetzt registrieren" class="submit" />
</form>
Your HTML after submitting will be:
<form action="<div>filledInEmail</div>" method="post">
<input type="text" name="email" placeholder="Ihre E-Mail-Adresse">
<input type="submit" name="send" value="Jetzt registrieren" class="submit" />
</form>
I'd recommend making an extension for this, but if you really want/need to do it like this, I think what you're looking for is something like:
<f:cObject typoscriptObjectPath="lib.mynlreg" />
<form action="{uri.page(addQueryString: 1)}" method="post">
<input type="text" name="email" placeholder="Ihre E-Mail-Adresse">
<input type="submit" name="send" value="Jetzt registrieren" class="submit" />
</form>
This will create the form with action to the current page (including any query string). You then have to change the userFunc to return an empty string if the form hasn't been submitted. Something like:
class myClass {
public function myFunction($content, $conf) {
$formEmail = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('email');
if (empty($formEmail)) {
return '';
}
return '<div>' . $formEmail . '</div>';
}
}

Laravel 5: Form fields not in model

I have a model with a field called map_zips and inside that field, I would like to store up to 5 zip codes, comma separated.
I would like to have five different form fields that before getting stored in the database model, I concatenate the values together and store them in the DB in the single column.
How can I create multiple form fields in a view, validate said form fields, perform what needs to be done on said form fields, store them? Beyond that, I think I will also need to split them up when loading up the edit/update page as well.
I guess the simplest way would be to create 5 different columns, one for each zip, but I'd like to learn how to do this to help extend my limited knowledge of Laravel.
In my ReportsController.php my current store method looks like this:
public function store(Request $request) {
$this->validate($request, $this->rules);
$user = Auth::user();
$report = $request->all();
$report['user_id'] = $user->id;
Report::create($report);
return redirect('/reports')->with('status', 'Report created');
}
I don't know if i undertand you question but you can try this:
if you want 5 input fields in your view you can try this, you will see error messages from you validation and a returned message to confirm that the value was stored propperly
#if (count($errors) > 0)
<div class="alert alert-danger">
#foreach ($errors->all() as $error)
<div>{{ $error }}</div>
#endforeach
</div>
#endif
#if(session()->has('message'))
<div class="alert alert-success">
{{ session()->get('message') }}
</div>
#endif
<form action="{{ route("route_to_store_function") }}" method="post">
<input type="text" name="zip1">
<input type="text" name="zip2">
<input type="text" name="zip3">
<input type="text" name="zip4">
<input type="text" name="zip5">
<button type="submit"></button>
</form>
then in your store function, concatenate the fields, save the report and return with a message to the previous page:
public function store(Request $request) {
$this->validate($request, [
'zip1' => 'required',// you can add more validations here
'zip2' => 'required',
'zip3' => 'required',
'zip4' => 'required',
'zip5' => 'required',
]);
$report = new Report();
$report->map_zips = $request->zip1.",".$request->zip2.",".$request->zip3.",".$request->zip4.",".$request->zip5;
$report->save();
return redirect()->back()->with('message', 'The report has been stored succesfully');
}
Then when you want to edit the report you can try this:
public function edit($id) {
$report = Report::find($id)
$zipCodes = explode(",", $report->map_zips);
return view('edit_report_view', compact("report", "zipCodes"));
}
And in you edit view:
<form action="{{ route("route_to_update") }}" method="post">
<input type="hidden" name="id" value="{{ $report->id }}">
<input type="text" name="zip1" value="{{ $zipCodes[0] }}">
<input type="text" name="zip2" value="{{ $zipCodes[1] }}">
<input type="text" name="zip3" value="{{ $zipCodes[2] }}">
<input type="text" name="zip4" value="{{ $zipCodes[3] }}">
<input type="text" name="zip5" value="{{ $zipCodes[4] }}">
<button type="submit"></button>
</form>

Passing Text into URL from a Form

I'm trying to insert a variable collected from a form into a URL, but I don't want the "?variable=value" part of the URL.
<form action="http://www.example.com/<?php echo htmlspecialchars($_GET['entry']);?>/" method="GET">
<input type="text" value="" name="entry" id="entry">
<input type='submit'>
</form>
Is there any easy way to do this? I want the browser to go to the following URL when the user types "whatever"
http://www.example.com/whatever/
Edit:
I've changed the code to the following, which seems to work, but have I now introduced a script vulnerability?
<form onSubmit=" location.href = 'https://www.example.com/' + document.getElementById('entry').value + '/' ; return false; ">
<input type="text" value="" name="entry" id="entry" placeholder="Your Promo Code">
<input name="promoSubmit" type="submit" value="Buy Now">
</form>
you could use javascript for this kind of tasks, i don't see why would you involve server side for such thing
but the easiest answer will be like:
<script>
function go(){
window.location='http://www.example.com/'+document.getElementById('url').value;
}
</script>
<input type='text' id='url'>
<button id='btn_go' onclick='javascript:go();'>Go</button>

Disable submit button when form invalid with AngularJS

I have my form like this:
<form name="myForm">
<input name="myText" type="text" ng-model="mytext" required />
<button disabled="{{ myForm.$invalid }}">Save</button>
</form>
As you may see, the button is disabled if the input is empty but it doesn't change back to enabled when it contains text. How can I make it work?
You need to use the name of your form, as well as ng-disabled: Here's a demo on Plunker
<form name="myForm">
<input name="myText" type="text" ng-model="mytext" required />
<button ng-disabled="myForm.$invalid">Save</button>
</form>
To add to this answer. I just found out that it will also break down if you use a hyphen in your form name (Angular 1.3):
So this will not work:
<form name="my-form">
<input name="myText" type="text" ng-model="mytext" required />
<button ng-disabled="my-form.$invalid">Save</button>
</form>
Selected response is correct, but someone like me, may have issues with async validation with sending request to the server-side - button will be not disabled during given request processing, so button will blink, which looks pretty strange for the users.
To void this, you just need to handle $pending state of the form:
<form name="myForm">
<input name="myText" type="text" ng-model="mytext" required />
<button ng-disabled="myForm.$invalid || myForm.$pending">Save</button>
</form>
If you are using Reactive Forms you can use this:
<button [disabled]="!contactForm.valid" type="submit" class="btn btn-lg btn primary" (click)="printSomething()">Submit</button>
We can create a simple directive and disable the button until all the mandatory fields are filled.
angular.module('sampleapp').directive('disableBtn',
function() {
return {
restrict : 'A',
link : function(scope, element, attrs) {
var $el = $(element);
var submitBtn = $el.find('button[type="submit"]');
var _name = attrs.name;
scope.$watch(_name + '.$valid', function(val) {
if (val) {
submitBtn.removeAttr('disabled');
} else {
submitBtn.attr('disabled', 'disabled');
}
});
}
};
}
);
For More Info click here
<form name="myForm">
<input name="myText" type="text" ng-model="mytext" required/>
<button ng-disabled="myForm.$pristine|| myForm.$invalid">Save</button>
</form>
If you want to be a bit more strict