I try to override the RegistrationBundle with adding a postal code.
But in the twig register form, when it loads, the postalcode field is not empty and there is written "user" in the
<input type="text">
And in the password field, it's not empty too
<input type="password">
This error occure when I am using "http://localhost:8000/app_dev.php/en/register/" but if I run another server like "http://localhost:8001/app_dev.php/en/register/" with port 8001, this error don't occure ! What is the matter ?
Could you explain me why ?
Hi Thanks for your comment. So, I overrided my FOSUserBundle Controller too ! Here is my Form :
private $em;
public function buildForm(FormBuilderInterface $builder, array $options)
{
$this->em = $options['em'];
$builder
->add('firstname', TextType::class)
->add('lastname', TextType::class)
->add('postalcode', TextType::class, array(
'attr'=> array('class'=>'postalcode form-control',
'maxlength'=>4,
'value'=>''))
)
->add('city', ChoiceType::class, array(
'attr'=>array('class'=>'city form-control')))
->add('conditions', CheckboxType::class, array('required' => true))
;
$city = function(FormInterface $form, $codepostal){
$localitestofind = $this->em->findBy(array('codepostal'=>$codepostal));
$localites = array();
if($localitestofind)
{
foreach($localitestofind as $localitetofind)
{
$localites[$localitetofind->getNom()] = $localitetofind->getNom();
}
}
$form->add('city', ChoiceType::class, array(
'attr'=>array('class'=>'city form-control'),
'choices'=> $localites));
};
$builder->get('postalcode')->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) use ($city){
$city($event->getForm()->getParent(), $event->getForm()->getData());
});
}
Your right, but I don't know why my browser autocomplete both fields. I removed the cache of the browser, I removed cookies, ... And I don't have this problem on an other port (8000 vs 8001)
Thanks a lot for helping
Related
I am working on Yii2. I am using mysql and mssql databases. The mssql is on a remote site and I am able to access it. Now I am trying to add a dropdown list.
Controller
public function actionCreate()
{
$model = new AllowArea();
$sds = Yii::$app->sds->createCommand("Select * from Area")->queryAll();// mssql Database
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
'sds' => $sds
]);
}
View
<?= $form->field($model, 'salesman_code')->dropDownList(\common\models\AllowArea::toArrayList(), ['prompt' => 'Select a Booker']) ?>
Model
In my model, I have a function
public static function toArrayList(){
$sds = Yii::$app->sds->createCommand("Select * from Salesmen")->queryAll();
return ArrayHelper::map($sds::find()->all(),'SalesmanCode',function($sds, $defaultValue){
return $sds['SalesmanCode'].' - '.$sds['SalesmanNameFull'];
});
}
Previously I was using self in place of $sds. With $sds I am getting error
Class name must be a valid object or a string
Any help would be highly appreciated
You are not using Model/Class. So, in ArrayHelper
return ArrayHelper::map($sds, 'SalesmanCode', function($sds) {
return $sds['SalesmanCode'].' - '.$sds['SalesmanNameFull'];
});
I'm working on CakePHP 3.4
I have a contact_messages table to save message via form on website.
I want to send user an email whenever a new message is saved.
For that, I have created mailer class like
<?php
namespace App\Mailer;
use Cake\Mailer\Mailer;
use Cake\Event\Event;
use Cake\Datasource\EntityInterface;
class ContactMessageMailer extends Mailer
{
public function newMessage($message)
{
$this
->setProfile('no-reply')
->setTemplate('new_message')
->setLayout('message')
->setEmailFormat('html')
->setTo($user->email) // user email
->setSubject('Verify Account')
->setViewVars(['name' => $user->first_name, 'email' => $user->email, 'message' => $message->body]);
}
public function implementedEvents()
{
return [
'Model.afterSave' => 'alertMessage'
];
}
public function alertMessage(Event $event, EntityInterface $entity, \ArrayObject $options)
{
if ($entity->isNew()) {
$this->send('newMessage', [$entity]);
}
}
}
and registering event in ContactMessagesTable.php
$mailer = new UserMailer(); //use App\Mailer\UserMailer;
$this->eventManager()->on($mailer);
ContactMessages belongsTo Users and Users is having email of user whom to send the email.
How can I get users information in Mailer?
Will probably do this;
In the User table;
public function processUser($user)
{
if($this->save($user)){
$event = new Event('Model.afterSave', $this, [$entity = $user])
$this->eventManager()->dispatch($event);
return true;
}else{
return false;
}
}
In ContactMessage Table ;
public function initialize()
{
parent::intialize();
$mailer = new UserMailer(); //use App\Mailer\UserMailer;
$this->Users->eventManager()->on($mailer); //ContactMessage has to be related to User table
}
Hope I was able to communicate.
I am using laravel 5.3 and angularjs
I submit json from my angularjs like below
{"grc":{"id":1},"floatingGrcs":[{"days":"10","units":"100"},{"days":"20","units":"200"}]}
I accept this array from my laravel controller like below
public function store(Request $request)
{
//how to extract $request object in here
}
I don't know how to extract submitted json array in laravel controller
You can use standard json_decode():
public function store(Request $request)
{
$data = json_decode($request->someJson);
}
You can look at all available data with dd($request->all());
The common-case is that there is no need to post json-encoded data through Angular.
To submit something with Angular, you should use, for example:
$http.post('api/something', {"grc":{"id":1},"floatingGrcs": [..]})
And then there is no need to decode json on the Laravel side:
public function store(Request $request)
{
$request->all(); // to get all fields
$request->grc->id; // to get a specific field
}
I got answer with below code. But i am not sure
$params = json_decode(file_get_contents('php://input'), TRUE);
is this above line of code secure or not.
public function store()
{
$params = json_decode(file_get_contents('php://input'), TRUE);
foreach ($params as $key => $value)
{
if($key == "grc")
{
$grc_id = $value["id"];
}
elseif($key == "floatingGrcs")
{
foreach ($value as $floating)
{
$days = $floating["days"];
}
}
}
I have used a component for uploading image,there is no problem in controller after add component.Here the code
class OesUsersController extends AppController {
var $helpers = array('Html', 'Form');
var $components = array('upload');
public function index() {
}
public function upload()
{
if (empty($this->data))
{
$this->render();
}
else
{
$this->cleanUpFields();
// set the upload destination folder
$destination = realpath('../../app/webroot/img/uploads/') . '/';
// grab the file
$file = $this->data['Image']['filedata'];
// upload the image using the upload component
$result = $this->Upload->upload($file, $destination, null, array('type' => 'resizecrop', 'size' => array('400', '300'), 'output' => 'jpg'));
if (!$result){
$this->data['Image']['filedata'] = $this->Upload->result;
} else {
// display error
$errors = $this->Upload->errors;
// piece together errors
if(is_array($errors)){ $errors = implode("<br />",$errors); }
$this->Session->setFlash($errors);
$this->redirect('/images/upload');
exit();
}
if ($this->Image->save($this->data)) {
$this->Session->setFlash('Image has been added.');
$this->redirect('/images/index');
} else {
$this->Session->setFlash('Please correct errors below.');
unlink($destination.$this->Upload->result);
}
}
}
The problem is image doesn't come from add.ctp
here the add.ctp code
<label for="Image">Image:</label>
<input type="file" name="data[Image][filedata]" id="ImageFiledata" />
add function code
public function add() {
$clint_ip=$this->request->clientIp();
if ($this->request->is('post')) {
$this->OesUser->create();
pr($this->request->data);
$this->request->data['OesUser']['user_otpkey']=String::uuid();
$this->request->data['OesUser']['user_regdate']=date("Y-m-d H:i:s");
$this->request->data['OesUser']['user_ip']=$clint_ip;
$this->barcode($this->request->data['OesUser']['user_otpkey']);
if ($this->OesUser->save($this->request->data)) {
$this->Session->setFlash(__('The oes user has been saved'), 'flash_success');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The oes user could not be saved. Please, try again.'), 'flash_fail');
}
}
$this->set('ip',$clint_ip);
}
here, database field name: image
controller name :OesUsers
Model name :OesUser
for full work I have taken help from this link
http://labs.iamkoa.net/2007/10/23/image-upload-component-cakephp/
How is your entire form looks like in add.ctp?
It sounds to me that you did not add
enctype="multipart/form-data"
to the form. That will cause the form not to post the file.
And also, it is recommended to use Form helper to create form.
When using Form helper, specify the form type to file
$this->Form->create('model',array('type'=>'file'));
I've been runing on my issue all night long tried many method to update an entry of User model and I didn't figure it out.
I tried with saveField, updateAll and findBy then save but all i've got is new entry on my DB or no update.
Here is the code, Hope you'll help
<?php
public function email_verification($token) {
$this->User->create();
$user = $this->User->findByEmailToken($token);
$this->User->set(array('email_token' => 'valid'), $user);
$this->User->save($user, false);
}
?>
Thanks in advance
Ahum:
$this->User->set(array('email_token' => 'valid'), $user);
$this->User->save($user, false);
The save() call saves your record with your original data..... since you pass in $user!
Replace the call to $this->user->set() with:
$user['User']['email_token'] => 'valid';
Try code below:
<?php
public function email_verification($token) {
$user = $this->User->findByEmailToken($token);
$user['User']['email_token'] = 'valid';
if ($this->User->save($user)) {
....
....
} else {
debug($this->User->validationErrors);
}
}
?>