I have installed Laracasts/Flash package and have been using it successfully for a while. I recently made some changes to my code and added another service provider and now, suddenly Laracasts/Flash is not magically creating the flash message pop-up modal. On my local dev environment it does, but not in production :(. In one of my controllers I run this code:
flash()->overlay("Your content may take a few minutes to be processed.", "Thanks for your submission!");
Log::info( $request->session()->all());
and I checked my logs to find the session data:
[2017-05-18 02:07:08] local.INFO: array (
'_token' => 'somestring',
'backUrl' => 'https://example.com/posts',
'_previous' =>
array (
'url' => 'https://example.com/posts/create',
),
'flash' =>
array (
'old' =>
array (
),
'new' =>
array (
0 => 'flash_notification',
),
),
'login_web_59ba36addc2b2f9401580f014c7f58ea4e30989d' => 1,
'flash_notification' =>
Illuminate\Support\Collection::__set_state(array(
'items' =>
array (
0 =>
Laracasts\Flash\OverlayMessage::__set_state(array(
'title' => 'Thanks for your submission!',
'overlay' => true,
'message' => 'Your content may take a few minutes to be processed.',
'level' => 'info',
'important' => false,
)),
),
)),
)
although in the environment where it works it looks like this:
'flash_notification' =>
array (
'message' => 'Your content may take a few minutes to be processed.',
'level' => 'info',
'overlay' => true,
'title' => 'Thanks for your submission!',
),
So Laracasts/Flash is creating the session data, but then in the returned view there is no flash message. Meaning when I check the source of the web page that is returned I do not see <div id="flash-overlay-modal" class="modal fade flash-modal"><div class="modal-dialog"><div class="modal-content"><div class="modal-header">... that I do when Laracasts/Flash normally creates a flash message. Anyone have any ideas about what the problem could be?
I am using Laravel 5.2.45.
Also I am including the flash view by #include('flash::message')
I have been digging around. I published the flash vendor files so that now I can edit the flash::message view and I put some test text at the top of the file and it shows up, however the first if statment in the view #if (Session::has('flash_notification.message')) never passes.
In my local env when I do Log::info($request->session()->get('flash_notification')); I get this output:
[2017-05-18 05:30:00] local.INFO: array (
'message' => 'Your content may take a few minutes to be processed.',
'level' => 'info',
'overlay' => true,
'title' => 'Thanks for your submission!',
)
in my production I get this output:
[2017-05-18 17:42:21] local.INFO: [{"title":"Thanks for your submission!","overlay":true,"message":"Your content may take a few minutes to be processed.","level":"info","important":false}]
Related
I am using tinymce for to add user cover letter related to the application.
This what my post array look like:
Array
(
[cover_letter] => <p>Test Cover Letter</p>
<ol>
<li>Need to save this data</li>
</ol>
<p><strong>Thanks</strong></p>
)
Simply I have used the require validation rule for this.
'candidate_cover_letter' => array(
array(
'field' => 'cover_letter',
'label' => 'Cover Letter',
'rules' => 'required'
)
)
I get the validation error regarding this like Cover Letter require.
I have two main problem:
How to validate HTML post array data
Is this best practice to save data like this? if no then how should i save this data?
First of all, in Codeigniter if we want to do form validations we need to go like this :
$config = array(
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'required'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'required',
'errors' => array(
'required' => 'You must provide a %s.',
),
)
);
$this->form_validation->set_rules($config);
You can refer here
so, your code here should be like this in the controller:
$config =array(
array(
'field' => 'cover_letter',
'label' => 'Cover Letter',
'rules' => 'required'
)
);
$this->form_validation->set_rules($config);
You can add extra fields in the $config like the example above.
Another thing that you asked, "How you should save the data ?"
I would suggest you to use a field in the database table with type "TEXT" and it should be okay for you.
After you hit submit you get redirected back to your controller somewhere. One way to utilize CI form validation is:
//look to see if you have post data
if($this->input->post('submit')){
//points to applications/config/form_validation.php (look at next chucnk to set form_validation.php)
if($this->_validate('cover_letter')){
//rest of your post logic
//get data to upload to database
$data = [
'cover_letter'=>$this->input->post('cover_letter'),
'user_id'=>$this->input->post('user_id')
];
//save data to database ..also this should be moved to a model
$this->db->insert('name of table to insert into', $data);
}
//if you post doesnt not get validated you will fall here and if you have this chucnk of code in the same place were you have the logic to set up the cover letter you will see a pink message that says what ever error message you set up
}
Set up form validation.php
<?php
$config = [
//set up cover letter validation
//$this->_validate('cover_letter') maps to here and checks this array
'cover_letter' => [
[
'field'=>'cover_letter',
'label'=>'Cover Letter Required',//error message to return back to user
'rules'=>'required'//field is required
],
//example to add additional fields to check
[
'field'=>'observations',
'label'=>'Observations',
'rules'=>'required'
],
],
]
have a Magento module that takes an input and creates a new item off that input. Everything works fine, and I wanted to add a field that would input an image with the item. I made sure to add the corresponding database column, and made sure to make the change every where it would be needed, but when i upload an image through the new input the image field does not send in my post data. I still get an object with all the fields and values from my form but the File field is always omitted. Using $_FILE to try to access it yields nothing also.
Here is the form:
$form = new Varien_Data_Form(array(
'id' => 'edit_form',
'action' => $this->getUrl('*/*/edit', array('embroidery_id' => $this->getRequest()->getParam('embroidery_id'))),
'method' => 'post',
'enctype' => 'multipart/form-data'
));
$form->setUseContainer(true);
$this->setForm($form);
$fieldset = $form->addFieldset(
'general',
array(
'legend' => $this->__('Details')
)
);
And here are the fields:
$this->_addFieldsToFieldset($fieldset, array(
'name' => array(
'label' => $this->__('Name'),
'input' => 'text',
'required' => true,
),
'file_t' => array(
'label' => $this->__('File path'),
'required' => false,
'class' => 'disable',
'input' => 'file',
'name' => 'file_t',
'value' => 'file_t',
),
));
Any help or guidance would be appreciated. i already did some googling and looked at all the top results for adding in a file upload field, and I copied some of the code exactly but the post field for the file input is still empty.
For Clarification:
The _addFieldsToFieldset function just loops through the fields array and either fills them in with data or adds the field onto the $fieldset variable. I also tried adding the field straight onto the variable with the following code but it did not work also.
$fieldset->addField('file_t', 'file', array(
'label' => $this->__('File path'),
'required' => false,
'name' => 'file_t',
));
Here is the function that handles the saving. This function does work, it has been tried and tested multiple times when I submitted other forms, and all the fields were successfully saved, and everything was sent through, but whenever I add the file field on, the post data looks exactly the same, and everything still works, it just does not send the file field through the post.
if(isset($_FILES['file_t']['name'])){
echo 'TRUE';
}
var_dump($this->getRequest()->getPost()); exit();
$em->addData($postData);
$em->save();
$this->_getSession()->addSuccess(
$this->__('The embroidery has been saved.')
);
// redirect to remove $_POST data from the request
return $this->_redirect(
'mu_em_admin/embroidery/edit',
array('embroidery_id' => $em->getID())
);
I simplified alot of the code down to only include the important parts. I also have the var_dump function there to troubleshoot the code. The results of var_dump were the whole post array that was sent, except for the file field.
please try to put this form before body end and then submit.
what web browser do you use, and which version of php?
I am running the following code (I've hidden ID's) to add/update a subscriber's interest groups in a MailChimp list:
$mailchimp->patch('lists/1234567/members/' . md5('test#test.com'), [
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => 'Ben',
'LNAME' => 'Sinclair',
),
'interests' => array(
'abcd1234' => true,
'defg6789' => true,
),
]);
The interests key is what I'm having issues with.
I presumed whatever you put in this key will overwrite what currently exists.
It doesn't seem to be the case. It only adds new interests but does not remove any if the ID's are not in the array. I am not getting any errors.
Does anyone know how to overwrite interest groups? Or if that's not possible, is there a way to remove interest groups?
For completion I wanted to add this answer so people stumbling upon this post can find a quick solution.
$mailchimp->patch('lists/1234567/members/' . md5('test#test.com'), [
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => 'Ben',
'LNAME' => 'Sinclair',
),
'interests' => array(
'abcd1234' => true, // Attached
'defg6789' => false, // Detached
)
]);
In this example the interest 'abcd1234' will be attached and the interest 'defg6789' will be detached.
Other interests that are not listed will remain on their original value.
I've built a form in Drupal 7 using form API and ajax calls. A typical form item looks like this:
$form['wrapper']['step1']['currency'] = array(
'#type' => 'radios',
'#options' => array(
'USD' => t('USD'),
'GBP' => t('GBP'),
'EUR' => t('EUR'),
),
'#default_value' => (!empty($form_state['values']['currency'])) ? $form_state['values']['currency'] : 'USD',
'#title' => t('Choose Currency'),
'#required' => TRUE,
'#ajax' => array(
'callback' => 'ajax_step1',
'wrapper' => 'step1-wrapper',
'method' => 'replace',
'effect' => 'fade',
'speed' => 'fast',
),
);
Everything is working as should but even when the ajax call just rebuilds a small part of the form it takes couple of seconds (the throbber is working overtime :).
Is this normal?
Is there a way to speed this (keeping things the Drupal way)?
When the ajax call is made, it literally rebuilds the entire form and returns only an aspect of it. For instance, in your function ajax_step1, you are probably calling to return a certain element from the form, and display it in the step1-wrapper div/wrapper element.
If you want to speed up the form return, you need to optimize the form builder itself. Which means, you might need to rewrite the form.
How to make file uploading as optional with validation?
The code below validates even if i didn't selected any file.
I want to check the extension only if i selected the the file.
If i am not selecting any file it should not return any validation error.
class Catalog extends AppModel{
var $name = 'Catalog';
var $validate = array(
'name' => array(
'rule' => '/^[a-z0-9 ]{0,}$/i',
'allowEmpty' => false,
'message' => 'Invalid Catalog name'
),
'imageupload' => array(
'rule' => array('extension',array('jpeg','jpg','png','gif')),
'required' => false,
'allowEmpty' => true,
'message' => 'Invalid file'
),
);
}
thanks in advance
"I assign $this->data['Catalog']['image'] = $this->data['Catalog']['imageupload']['name'];"
So by the time you save your data array, it looks something like this I assume:
array(
'image' => 'foobar',
'imageupload' => array(
'name' => 'foobar',
'size' => 1234567,
'error' => 0,
...
)
)
Which means, the imageupload validation rule is trying to work on this data:
array(
'name' => 'foobar',
'size' => 1234567,
'error' => 0,
...
)
I.e. the value it's trying to validate is an array of stuff, not just a string. And that is unlikely to pass the specified validation rule. It's also probably never "empty".
Either you create a custom validation rule that can handle this array, or you need to do some more processing in the controller before you try to validate it.
Concept:
In Controller, before validating, or saving (which does validation automatically by default) check if any file is uploaded. If not uploaded, then unset validator for the file field.
Sample code:
Controller
// is any image uploaded?
$isNoFileUploaded = ($this->request->data['Model']['field_name']['error'] == UPLOAD_ERR_NO_FILE) ? true : false ;
if ($isNoFileUploaded) {
$this->Model->validator()->remove('field_name');
}
Notes:
This solution comes under preprocessing as one of the two alternative approaches (preprocessing in controller, custom validation in model) suggested by #deceze's answer