public function configure()
{
$this->widgetSchema['start_date'] = new sfWidgetFormInput();
$this->widgetSchema['end_date'] = new sfWidgetFormInput();
$this->validatorSchema->setPostValidator( new sfValidatorOr ( array(
new sfValidatorAnd( array
(new sfValidatorSchemaCompare('start_date', sfValidatorSchemaCompare::NOT_EQUAL, null),
new sfValidatorSchemaCompare('end_date', sfValidatorSchemaCompare::EQUAL, null)
)),
new sfValidatorSchemaCompare('start_date', sfValidatorSchemaCompare::LESS_THAN_EQUAL, 'end_date',
array('throw_global_error' => false), array('invalid' => 'The start date ("%left_field%") must be before the end date ("%right_field%")')))));
}
I've got following input dates which I want to check if the end date isn't before the start date:
Input: Start => 31/03/10 End=> 07/03/10
Output: The start date (2010-03-31) must be before the end date (2010-03-07)
Can you in some way change the date output? I need the error message to set the date format the same as the input.
Also my input fields are set with the wrong date format when the error appears.
Tried several things, but no luck at this moment. Didn't find a solution or information on symfony it self.
I'm using symfony version 1.2.11
I found a the solution together with a colleague.
After some tryouts, we found the solution to my initial problem. Instead of using that post validator, we wrote are own validator.
class StartBeforeEndDateValidator extends sfValidatorBase {
public function configure($options = array(), $messages = array()) {
parent::configure($options, $messages);
$this->addMessage('Invalid_daterange', 'Start date (%start%) must be before End date (%end%)!');
}
public function doClean($values) {
sfContext::getInstance()->getConfiguration()->loadHelpers('Date');
$timestampStart = sfContext::getInstance()->getI18N()->getTimestampForCulture($values['start_date'], sfContext::getInstance()->getUser()->getCulture());
$timestampEnd = sfContext::getInstance()->getI18N()->getTimestampForCulture($values['end_date'], sfContext::getInstance()->getUser()->getCulture());
if(format_date($timestampStart) > format_date($timestampEnd)){
throw new sfValidatorError($this, 'Invalid_daterange', array('start' => $values['start_date'], 'end' => $values['end_date']));
}
}
}
Usage of the validator in the symfony form:
public function configure() {
$this->widgetSchema['start_date'] = new sfWidgetFormInput();
$this->widgetSchema['end_date'] = clone $this->widgetSchema['start_date'];
$this->validatorSchema['start_date'] = new DateValidator();
$this->validatorSchema['end_date'] = clone $this->validatorSchema['start_date'];
$this->validatorSchema->setPostValidator(new StartBeforeEndDateValidator());
}
This was the solution to have always the same date format when it's being validated and when the validation is trigger with an error, the format of the date is correctly returned in the same date format as it was set.
Now one other "problem" that we encountered was when the save happened, the date format wasn't right for mysql. So we override the save function in our symfony form and apply the right date format.
example:
public function save($con = null){
$var = new Object();
if($this->taintedValues['id']!= ""){
$var->setId($this->taintedValues['id']);
}
$var->setStartDate(DateUtils::getIsoDateForCulture($this->taintedValues['start_date']));
$var->setEndDate(DateUtils::getIsoDateForCulture($this->taintedValues['end_date']));
$var->setIcpm($this->taintedValues['icpm']);
$var->save($con);
}
So once the validation is valid, it will perform the save function and set the right date format before actually save it into the database.
Hope that this is helpful for other people who had this problem.
Related
Hi guys i am working with laravel 5.7 while inserting data im getting issue of "Data Missing" but the data is inserting as well but still getting error here im sharing part of my code
Model
public function setApplicationDateAttribute($input)
{
if($input != '') {
$this->attributes['application_date'] = Carbon::createFromFormat(config('quickadmin.date_format'), $input)->format('Y-m-d');
}else{
$this->attributes['application_date'] = '';
}
}
this is the funtion which is checking the date format for input data Now this is a file in my config folder named quickadmin i will show you the code of it
return [
/**
* Datepicker configuration:
*/
'date_format' => 'Y-m-d',
'date_format_jquery' => 'yy-mm-dd',
'time_format' => 'H:i:s',
'time_format_jquery' => 'HH:mm:ss',
/**
* Quickadmin settings
*/
'route' => 'admin',
'homeRoute' => 'admin',
'defaultRole' => 1];
Now here is the code of Controller
$locumApplications = ModelName::create([
'user_id' => $request->user_id,
'locum_id' => $request->locum_id,
'application_date' => $request->application_date
]);
it insert the data but give me error and if i remove the line 'application_date' => $request->application_date it still show me the error
here i attached the error image
Your config: config('quickadmin.date_format') is Y-m-d
You are trying to set attributes
Carbon::createFromFormat(config('quickadmin.date_format'), $input)->format('Y-m-d');
Your $input ($request->application_date) must be on Y-m-d format
Should be work
\Carbon\Carbon::createFromFormat('Y-m-d', '2019-03-14')->format('Y-m-d') //OP: 2019-03-14
Your date input and set attributes value look like the same format
Can you try overriding the default format in YourModel like below:
class YourModel extends Model {
protected $dateFormat = 'Y-m-d'; // add your date format
}
I'm trying for a couple of days now to limit the amount of comments that can be posted by a user.
So after the user submits a comment he should wait for example 5 minutes, to be able to post the next comment.
I know that this can be accomplished by the laravel validator with the 'after' rule. But I've tried a couple of things now and nothing worked out yet.
Here's the Code of the method:
public function storeComment($id, Request $request)
{
$user = User::with('newsComment')->find(Auth::user()->id);
if ($user->NewsComment->count() != 0)
{
$last_comment = NewsComment::where('author_id', Auth::user()->id)->orderBy('created_at', 'desc')->first();
$date_after = new \DateTime($last_comment->created_at);
$date_after->modify('+5 minutes');
} else {
$date_after = date("Y-m-d H:i:s", mktime(0, 0, 0, 00, 00, 0000));
}
$request->current_date = date("Y-m-d H:i:s");
$this->validate($request, [
'comment' => 'required|max:255|min:5',
'current_date' => 'after:'.$date_after->format("Y-m-d H:i:s"),
]);
$comment = new NewsComment;
$comment->news_article_id = $id;
$comment->author_id = Auth::user()->id;
$comment->comment = $request->comment;
$comment->save();
return $this->showArticle($id);
}
So, I know, that this code reads the correct time out of the database and adds the 5 minutes to the given date. In the template I added an empty, hidden form field named 'current_date'. In the method above I fill this variable with the current date and try to validate it with the 'after' validation rule.
But for some reason it won't work.
I hope some of you can help me with this.
I'm btw new to laravel so please bear with me.
Instead of $comment->save(); you can check if the comment is saved and flash in the Session the current timestamp then compare with "Now" after the validation.
if($comment->save()){
$request->session()->put('post_time', Carbon::now());
}
and after validation:
if ($request->session()->has('post_time')) {
if(Carbon::now()->diffInMinutes($request->session()->get('post_time')) < 5){
//stop execution
}
}
Here's the code i want to work with
<?php echo $form->textField($model,'email_time_created',array('readonly'=>true)); ?>
i want to automatically add the current time to my database
I recommend it to add it at the database, asuming you're using MySQL you can create a trigger before saving and doing something like this:
SET NEW.email_time_created=NOW()
If not, you can do it at Yii/PHP level by adding the following function at the model class:
public function beforeSave(){
$this->email_time_created = CDbExpression('NOW()'); //Only in MYSQL
return parent::beforeSave();
}
It will set the column to the current value before saving the model. Notice that it won't be shown at the form, but you can add it by JS or using php's date() at the form's view.
also you can set the value in controller.
$model->email_time_created= now('Y-m-d H:i:s');
The simplest way is to add CTimestampBehavior to your model:
public function behaviors(){
return array(
'CTimestampBehavior' => array(
'class' => 'zii.behaviors.CTimestampBehavior',
'createAttribute' => 'create_time_attribute',
'updateAttribute' => 'update_time_attribute',
)
);
}
See API.
I'd like to validate form input that is datetime for article to be posted.
$model = new Post('update');
$model->attributes = $_POST['Post'];
if($model->validate()){
//But, validation fails...
}
This is the rule that I got to check if the input is datetime format or not.
I used this page(http://chris-backhouse.com/date-validation-in-yii/528) as reference.
But I get validation error for 'created'input.
public function rules()
{
return array(
//datetime validation
array('created', 'date', 'message' => '{attribute}: is not a datetime!', 'format' => 'YYYY-MM-DD HH:MM:SS'),
);
}
This is what I have in $models-attribute.
array(1) { ["created"]=> string(19) "2013-08-01 00:00:01" }
Could anyone knows how to make this work?
Thanks a lot in advance!!!
I would advice to use input formats in rule, since sometimes you want custom formats.
array('created', 'date', 'format'=>'yyyy-MM-dd hh:mm:ss', 'message'=>'{attribute} have wrong format'),
More about date formats here - http://www.yiiframework.com/doc/api/1.1/CDateTimeParser
On Yii 2.0.6 (and maybe Yii 1.x ?), the correct format is :
['creation_date', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss']
It's not working if hours are not in CAPS.
Is this creation time of the post? Then you don't need to validate it at all and you should set it in beforeSave instead of sending it with POST
add this to your model:
public function beforeSave()
{
if($this->isNewRecord)
{
$this->created=new CDbExpression('NOW()');
}
return parent::beforeSave();
}
I am using CJuiDatePicker in my form and I need to use two different formats: mm/dd/yy to be shown and dd/mm/yy to be sent in $_POST.
$questionario->widget('zii.widgets.jui.CJuiDatePicker', array(
'model' => $modeloDoQuestionario,
'attribute' => 'data_preenchimento',
'language' => 'en',
'options' => array(
'showAnim' => 'fold',
'showButtonPanel' => true,
'showOn' => 'both',
'dateFormat' => 'dd/mm/yy',
'altField' => '#Questionarios_data_preenchimento',
'altFormat' => 'mm/dd/yy',
),
'htmlOptions' => array(
'style' => 'height:14px;'
),
));
This is the field in HTML:
<input style="height:14px;" id="Questionarios_data_preenchimento" name="Questionarios[data_preenchimento]" type="text" />
But I still get the mm/dd/yy format in $_POST... What can be wrong?
You can put in the configuration file: 'main.php' something like this:
'widgetFactory' => array(
'widgets' => array(
'CJuiDatePicker' => array(
'scriptFile' => 'jquery-ui.min.js',
'language' => 'en',
'options' => array(
'dateFormat' => 'dd/mm/yy',
'showAnim' => 'fold',
'fontSize' => '0.85em',
),
),
),
),
With this ever that you call a 'CJuiDatePicker' widget will inherit this format.
I was looking for the same thing and have a slightly different solution.
I was mainly looking for on form pages to display the date as DD-MM-YYYY and save in the database as YYYY-MM-DD.
Regardless of changing the dateFormat option on CJuiDatePicker and even trying to use altFormat (using altField as well as apparently this is necessary) option, the date did not show in the desired format, granted if I were to change the date, the format would be shown correctly.
So I created a Custom class in components and created two methods, one to reformat the date to the DD-MM-YYYY format and one to format the date to the correct YYYY-MM-DD to be saved in the database.
Here is the Custom class (the file is called Custom.php and kept in components.)
class Custom
{
/*
**This method formats the date to be human readble ie 21-06-2013
*/
public function reformatDate($date)
{
if($date == '0000-00-00')
{
$newDate = '00-00-0000';
return $newDate;
}
elseif($date == '')
{
return $date;
}
else
{
$newDate = date("d-m-Y", strtotime($date));
return $newDate;
}
}
/*
**This method formats the date to the correct format according to the database
*/
public function formatDate($date)
{
if($date == '00-00-0000')
{
$newDate = '0000-00-00';
return $newDate;
}
elseif($date == '')
{
return $date;
}
else
{
$newDate = date("Y-m-d", strtotime($date));
return $newDate;
}
}
}
Now that I had methods to change the dates to the correct formats. On the _form page I declared at the top, under the initiation of the form.
$model->date_required = Custom::reformatDate($model->date_required);
$model->date_created = Custom::reformatDate($model->date_created);
$model->date_completed = Custom::reformatDate($model->date_completed);
This now results in the date being shown correctly, both when the _form page is viewed and the date is changed via the CJuiDatePicker as i still use the dateFormat option to show the correct format when the date is changed.
Lastly I added to both the create and update actions of the controller the following.
if(isset($_POST['Tickets']))
{
$model->attributes=$_POST['Tickets'];
$model->date_created = Custom::formatDate($model->date_created);
$model->date_required = Custom::formatDate($_POST['Tickets']['date_required']);
$model->date_completed = Custom::formatDate($_POST['Tickets']['date_completed']);
if($model->save())
So before the data is then saved back to the database I format the date to the correct format and pop it into the database.
I'm not expert and only a few month into Yii and about 9 months into PHP. I know there are all sorts of before and after save methods and this may not be the best way to do it, in fact if there is a better way I am all ears. But for now this is working for me. hope ths helps someone.
/* Small Update */
After speaking with a colleague who has been doing this stuff for about a decade. he said it would be much cleaner to remove the code I placed on the form page under the form initiation and move it rather into the controller, so for example my update method appears like so
public function actionUpdate($id)
{
$model=$this->loadModel($id);
$model->date_required = Custom::reformatDate($model->date_required);
$model->date_created = Custom::reformatDate($model->date_created);
$model->date_completed = Custom::reformatDate($model->date_completed);
So rather than reformatting the dates on the form page, it is done directly after loadModel, keeping the view cleaner.