This is how the registration form of FOSUserBundle looks like:
<form action="/Symfony/web/signup/" method="POST" class="fos_user_registration_register">
<div id="fos_user_registration_form">
<input type="hidden" id="fos_user_registration_form__token" name="fos_user_registration_form[_token]" value="c248f3ef17b082803ae9948c03d137c380f0dc24"/>
<div>
<label for="fos_user_registration_form_username">Username:</label><input type="text" id="fos_user_registration_form_username" name="fos_user_registration_form[username]" required="required" maxlength="255" pattern=".{2,255}"/>
</div>
<div>
<label for="fos_user_registration_form_email">Email:</label><input type="email" id="fos_user_registration_form_email" name="fos_user_registration_form[email]" required="required"/>
</div>
<div>
<label for="fos_user_registration_form_plainPassword_first">Password:</label><input type="password" id="fos_user_registration_form_plainPassword_first" name="fos_user_registration_form[plainPassword][first]" required="required"/>
</div>
<div>
<label for="fos_user_registration_form_plainPassword_second">Verification:</label><input type="password" id="fos_user_registration_form_plainPassword_second" name="fos_user_registration_form[plainPassword][second]" required="required"/>
</div>
</div>
<div>
<input type="submit" value="Register"/>
</div>
So, as you can see,
<input type="email" id="fos_user_registration_form_email" name="fos_user_registration_form[email]"
MAIN QUESTION: How can I change the id to something like id="email" and also the name to something like name="email"? And it has to work, obviously.
Here you can see: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/views/Registration/register_content.html.twig {{ form_widget(form) }}, but I can't trace this to where it goes. I also presume the RegistrationFormHandler would have to be edited to support these parameters.
Alter buildForm function in RegistrationFormType class:
# FOSUserBundle/Form/Type/RegistrationFormType.php
class RegistrationController extends ContainerAware
{
// ...
public function getName()
{
return 'fos_user_registration';
}
}
Change fos_user_registration to whatever you want.
Related
I'm struggling to understand how livewire works.
I have two text fields. One is where the user enters information like prefixes and the second field with a read-only attribute where data will be displayed based on the first field value.
But for some reason, I can't populate the second field. All examples on the internet are how to take a value and return it back or generate a dropdown menu.
my blade template:
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">prefix</label>
<input wire:change="testing"
type="text"
class="form-control"
id="prefix"
name="prefix"
/>
</div>
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">Code</label>
<input wire:model="part"
type="text"
class="form-control"
id="part"
name="part"
value="{{ $part }}"
/>
</div>
and
Livewire class:
class DoSomethingClass extends Component
{
public $prefix;
public $part;
public function testing()
{
$this->part = $this->prefix;
}
public function render()
{
return view('livewire.blade-template');
}
}
You should use wire:model to both input fields. Than, if you want to update the 2nd field on the input of the first, you can use the Livewire Lifecycle to update it.
public $prefix;
public $part;
public function updatedPrefix()
{
$this->part = $this->prefix;
}
Check this link for info on the updatedPrefix() hook: https://laravel-livewire.com/docs/2.x/lifecycle-hooks
(You can choose to use your testing() method too, but than you still need to use the wire:model directive to your second input.)
You can make what you're doing work just by adding wire:model="prefix" to your first input as suggested above.
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">prefix</label>
<input wire:change="testing"
type="text"
class="form-control"
id="prefix"
name="prefix"
wire:model="prefix"
/>
</div>
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">Code</label>
<input wire:model="part"
type="text"
class="form-control"
id="part"
name="part"
value="{{ $part }}"
/>
</div>
And the component itself can just stay the same. When someone types anything in the first field then tabs or clicks away it will update the second field.
If you want the second field to update as the user types change it to wire:keydown="testing", it will make a call with every keystroke.
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">prefix</label>
<input wire:keydown="testing"
type="text"
class="form-control"
id="prefix"
name="prefix"
wire:model="prefix"
/>
</div>
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">Code</label>
<input wire:model="part"
type="text"
class="form-control"
id="part"
name="part"
value="{{ $part }}"
/>
</div>
As mentioned above, if you use the updatedPrefix() method you won't need the wire:change or wire:keydown as Livewire will update the field as the user types.
My View:
<form action="/AddJuiceForm" method="POST">
#csrf;
<div class="form-group">
<label for="FruitName">Fruit Name : </label>
<input type="text"
class="form-control"
placeholder="Enter the Fruit Name"
name="fruitname">
</div>
<div class="form-group">
<label for="JuiceName">Juice Name : </label>
<input type="text"
class="form-control"
placeholder="Enter the Juice Name"
name="juicename">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
My Routes/web.php:
Route::post('AddJuiceForm','JuiceController#insertjuice ');
My Controller:
<?php
namespace App\Http\Controllers;
use App\JuiceModel;
use Illuminate\Http\Request;
class JuiceController extends Controller
{
public function insertjuice()
{
dd(request()->all);
}
}
Result: I am getting 'null' as output
Help me where I am going wrong?
Inject the Illuminate\Http\Request object into the insertjuice method and then call the all() method on the variable within your JuiceController class:
public function insertjuice(Request $request)
{
dd($request->all());
}
I used method store from a controller and data doesn't store in my database.
I want to insert NOM_ARTICLE, PHOTO_ARTICLE, TYPE, DESCRIPTION_ARTICLE in three tables from a form in native HTML, but I don't know what action to do.
I am using ArticleController where there is a method store and three table models.
public function create()
{
return view('addarticle');
}
public function store(Request $request)
{
$article = new article;
$article->NOM_ARTICLE = $request->NOM_ARTICLE;
$article->LABEL_TYPE = $request->LABEL_TYPE;
$article->PHOTO_ARTICLE = $request->PHOTO_ARTICLE;
$article->DESCRIPTION_ARTICLE = $request->DESCRIPTION_ARTICLE;
$article->save();
return redirect()->route('addarticle');
}
Here are my tables from database:
article ('ID_ARTICLE','NOM_ARTICLE','ID_TYPE,'DESCRIPTION_ARTICLE')
photo_articles('ID_PHOTO','ID_ARTICLE','PHOTO_ARTICLE')
type('ID_TYPE','TYPE')
My HTML form:
<form method="post" action="" class="contact_form text-center" id="contact_form">
<div class="">
<div class="col-lg-4">
<input type="text" class="contact_input" name="NOM_ARTICLE" placeholder="Nom d'article" required="required">
</div>
<br/>
<div class="col-lg-4">
<input type="text" class="contact_input" name="ID_TYPE" placeholder="Type d'article" required="required">
</div>
<div>
<input type="hidden" name="MAX_FILE_SIZE" value="250000" />
<input type="file" class="contact_input" name="PHOTO_ARTICLE" placeholder="Capture de votre article" name="fic" size=50 required="required" />
<!-- <input type="submit" value="Envoyer" /> -->
</div>
</div>
<textarea class="contact_textarea contact_input" name="DESCRIPTION_ARTICLE"placeholder="Description" required="required"></textarea>
<button class="contact_button" type="submit">Valider!</button>
</form>
And I have my route in web.php:
Route::resource('addarticle','ArticleController');
And this is my Article model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class article extends Model {
public $table = 'article';
public $primaryKey ='ID_ARTICLE';
return $this->belongsTo('photo_articles');
}
After I click on the submit button it shows a URL like this: http://localhost/testprojet/public/addarticle?NOM_ARTICLE=test&ID_TYPE=book&MAX_FILE_SIZE=250000&PHOTO_ARTICLE=villle+icon.jpg&DESCRIPTION_ARTICLE=ss
And redirecting to my view addarticle but nothing gets added in the database.
Yaa You clearly missed csrf token to pass with the form. If you want to bubmit any form in Laravel, you should pass csrf token.
use #csrf and blade will pass the unique token to the form submit
https://laravel.com/docs/5.8/csrf
Example:
<form method="post" action="" class="contact_form text-center" id="contact_form">
#csrf
<div class="">
<div class="col-lg-4">
<input type="text" class="contact_input" name="NOM_ARTICLE" placeholder="Nom d'article" required="required">
</div>
<br/>
<div class="col-lg-4">
<input type="text" class="contact_input" name="ID_TYPE" placeholder="Type d'article" required="required">
</div>
<div>
<input type="hidden" name="MAX_FILE_SIZE" value="250000"/>
<input type="file" class="contact_input" name="PHOTO_ARTICLE" placeholder="Capture de votre article" name="fic" size=50 required="required"/>
<!-- <input type="submit" value="Envoyer" /> -->
</div>
</div>
<textarea class="contact_textarea contact_input" name="DESCRIPTION_ARTICLE" placeholder="Description"
required="required"></textarea>
<button class="contact_button" type="submit">Valider!</button>
</form>
What I see from your form is that you didn't put any action in it, you just left it empty:
<form method="post" action="" class="contact_form text-center" id="contact_form">
You should be putting in a route to your store method in your controller, so that when the form is submitted, the data will be passed into your controller.
Also, providing a link to your localhost isn't helping us to understand your problem as we can't access to it. A solid screenshot will do.
You should put the action according to the resource pattern.
Your web.php
Route::resource('articles','ArticleController');
Your form:
<form method="post" action="{{route('articles.store')}}" class="contact_form text-center" id="contact_form">
#csrf
You should include the #csrf blade directive, as well.
In you controller:
public function store(Request $request)
{
$article = new Article();
$article->NOM_ARTICLE = $request->NOM_ARTICLE;
$article->DESCRIPTION_ARTICLE = $request->DESCRIPTION_ARTICLE;
$article->save();
return redirect()->route('addarticle');
}
You're adding the PHOTO_ARTICLE to the article but you dont have this column in your articles table as you said, so it dont make sense to use the line below:
$article->PHOTO_ARTICLE = $request->PHOTO_ARTICLE;
Also, you're not receiving any LABEL_TYPE from your request, and you dont have this column in your articles table too. So you must remove this line:
$article->LABEL_TYPE = $request->LABEL_TYPE;
Try this: I hope it will help you.
Controller
public function store(Request $request)
{
$articleObj = new article;
$articleObj->add($request);
//second table
$employerObj = new Employer();
$employerObj->add($request);
}
Create three model according to your datatables and paste this code for all.
function add($request)
{
$this->NOM_ARTICLE = $request->NOM_ARTICLE;
$this->LABEL_TYPE = $request->LABEL_TYPE;
$this->PHOTO_ARTICLE = $request->PHOTO_ARTICLE;
$this->PHOTO_ARTICLE = $request->PHOTO_ARTICLE;
$this->save();
return $this->id;
}
In Laravel I'm getting a "csrf" issue "Illuminate \ Session \ TokenMismatchException"
Route::group(array('before'=>'guest'),function()
{
Route::get('/user/create',array('uses'=>'UserController#getCreate'));
Route::get('/user/login',array('uses'=>'UserController#getLogin','as'=>'postCreate'));
Route::group(array('before'=>'csrf'),function()
{
Route::get('/user/create','UserController#postCreate');
Route::get('/user/login','UserController#postLogin');
});
});
that is controller
class UserController extends BaseController{
public function getCreate()
{
//return View::make('hello');
return View::make('user.register');
}
that is view
<div class="container">
<h1>Register</h1>
<form role="form" method="post" action="{{ URL::route('postCreate')}}">
<div class="form-gourp">
<label for="username">Username: </label>
<input id="username" name="username" type="text" class="form-control" />
</div>
<div class="form-gourp">
<label for="password">Password: </label>
<input id="password" name="password" type="text" class="form-control" />
</div>
<div class="form-gourp">
<label for="username">confirm Password: </label>
<input id="cpassword" name="cpassword" type="text" class="form-control" />
</div>
{{form::token()}}
<div class="form-gourp">
<input type="submit" value="register" class="btn btn-default"/>
</div>
</form>
When I add {{form::token }}, it shows the error "Illuminate \ Session \ TokenMismatchException"
You are wrong in your routes.
Route::group(array('before'=>'csrf'),function()
{
Route::get('/user/create','UserController#postCreate');
Route::get('/user/login','UserController#postLogin');
});
Those should be post, like the following
Route::group(array('before'=>'csrf'),function()
{
Route::post('/user/create','UserController#postCreate');
Route::post('/user/login','UserController#postLogin');
});
Read more about CSRF on wiki and laravel doc.
Use {{ Form::open() }} and {{ Form::close() }} rather than the <form> tags
In PyroCMS (Version 2.2.3) there is a Contact plugin.
How is one supposed to add a class to one of the text inputs ?
To contact us please fill out the form below.
{{ contact:form name="text|required" email="text|required|valid_email" subject="dropdown|Support|Sales|Feedback|Other" message="textarea" }}
Name:{{ name }}
Email:{{ email }}
Subject:{{ subject }}
Message:{{ message }}
{{ /contact:form }}
To Something like this
To contact us please fill out the form below.
{{ contact:form name="text|required" email="text|required|valid_email" subject="dropdown|Support|Sales|Feedback|Other" message="textarea" }}
Name:{{ name }}
Email:{{ email }}
Subject:{{ subject }}
Message:{{ message: class='css_message' }}
{{ /contact:form }}
I had this problem too. When the form get's printed out onto your page, each field is given an ID prefixed with contact_ and a class name that matches the field name. The form itself has a default class of "contact-form". Here's mine:
This setup...
{{contact:form
name="text|required"
phone="text|required"
email="text|required|valid_email"
message="textarea|required"
attachment="file|doc|pdf|zip|docx" max-size="10000"
template="recruitment"
button="Enquire Now"}}
<div><label for="name">Full Name:</label>{{name}}</div>
<div><label for="phone">Phone:</label>{{phone}}</div>
<div><label for="email">E-mail Address:</label>{{email}}</div>
<div><label for="message">Message:</label>{{message}}</div>
<div><label for="attachment">Attach your CV (Formats accepted: PDF, DOC, DOCX, ZIP): </label>{{attachment}}</div>
{{/contact:form}}
Will generate this form...
<form action="http://www.companysite.dev/recruitment" class="contact-form" enctype="multipart/form-data" method="post" accept-charset="utf-8">
<input type="text" name="d0ntf1llth1s1n" value=" " class="default-form" style="display:none" />
<div>
<label for="name">Full Name:</label><input type="text" name="name" value="" id="contact_name" class="name" />
</div>
<div>
<label for="phone">Phone:</label><input type="text" name="phone" value="" id="contact_phone" class="phone" />
</div>
<div>
<label for="email">E-mail Address:</label><input type="text" name="email" value="" id="contact_email" class="email" />
</div>
<div>
<label for="message">Message:</label><textarea name="message" cols="40" rows="10" id="contact_message" class="message"></textarea>
</div>
<div>
<label for="attachment">Attach your CV (Formats accepted: PDF, DOC, DOCX, ZIP):</label><input type="file" name="attachment" value="" id="contact_attachment" class="attachment" />
</div>
</form>
You can then style your message field using any of following CSS methods
.contact-form .message {
// Styles go here
}
.message {
// Styles go here
}
.contact-form #contact_message {
// Styles go here
}
#contact_message {
// Styles go here
}
Hope that helps!