symfony 2.5 form validation error - symfony-2.1

I have a form type A with some widgets and mapped to class C1. I need a new widget in this form which is not mapped to this class( i.e mapped to another class C2). So i have created a new form type B. This new form type B is included in form type A.
Form Type B:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('join_event', 'date', array(
'label' => 'employee.form.join_event.label',
'required' => true,
'widget' => 'single_text',
'attr' => array(
'data-date-format' => 'MM-DD-YYYY'
)
));
}
FormType A including FormType B:
$builder->add('event', new FormTypeB(), array(
'label' => false,
'mapped' => false
));
The form display is well as expected but there is a validation error on submission with message "This value is not valid."

I think the problem is related to the date-field itself.
See http://symfony.com/doc/current/reference/forms/types/date.html#format how to define the format.
Maybe sth like 'format' => 'MM-dd-yyyy' will work.

Related

CGridView filter by year of date

Sorry for long text but my problem is long :-) I made grid like this
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'name'=>'title',
'type'=>'raw',
'value'=>'CHtml::link(CHtml::encode($data->title), $data->url)'
),
array(
'name'=>'create_time',
'type'=>'raw',
'value' => 'Yii::app()->dateFormatter->format("yyyy", $data->create_time)',
),
),
));
and then i want to filtering by date like title filtering, by textfield.
I made controller with code
$model=new Post('search');
if(isset($_GET['Post']))
$model->attributes=$_GET['Post'];
$this->render('admin',array(
'model'=>$model,
));
and search methode in model
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('title',$this->title,$partialMatch=true);
$criteria->compare('create_time',$this->create_time,$partialMatch=true);
return new CActiveDataProvider('Post', array(
'criteria'=>$criteria,
'sort'=>array(
'defaultOrder'=>'status, update_time DESC',
),
));
}
i don't forget abount rules()
array('title, create_time', 'safe', 'on'=>'search')
I have got two records wiht today's date. Title filtering works fine but when i type '2015' in 'create_time' column filter textfield and press enter i get "no results found".
I tried this:
array(
'name'=>'create_time',
'type'=>'raw',
'value' => 'Yii::app()->dateFormatter->format("dd MMMM yyyy", $data->create_time)',
'filter' => CHtml::listData(Post::model()->findAll(array('order'=>'create_time')), 'create_time', 'create_time'),
),
and it's seems to work but in my database, create_time is integer(11) (required) and listdata values is number not date and represents full date, not only year.
I controller i made array with unique year values from create_time record's values. Outputs array like $rok['2015'] => '2015'
$model_post = new Post;
$wyniki = $model_post::model() -> findAllBySql('SELECT create_time FROM tbl_post');
for($i=0;$i<count($wyniki);$i++)
{
$rok_temp[$i] = Yii::app()->dateFormatter->format("yyyy", $wyniki[$i]->create_time);
$rok[$rok_temp[$i]] = $rok_temp[$i];
}
$rok = array_unique($rok);
I send $rok to view and
array(
'name'=>'create_time',
'type'=>'raw',
'value' => 'Yii::app()->dateFormatter->format("dd MMMM yyyy", $data->create_time)',
'filter' => $rok,
),
and
$criteria->compare('create_time',Yii::app()->dateFormatter->format("yyyy", $this->create_time));
but then i get "no results found" when filtering.
What i made wrong?
Your time is stored as a UNIX timestamp. You therefore have to do some conversion on the input from the filter to change it to UNIX also. First, create a variable to hold the year in your model
class MyModel extends ... {
public $create_year;
}
Then add this variable to your rules:
array('create_year', 'safe', 'on' => 'search')
array('create_year', 'numerical', 'integerOnly' => true)
Next add the conversion rules for the year to your search() i.e
public function search() {
...
if ($this->create_year) {
$criteria->addBetweenCondition('create_time',
strtotime($this->create_year.'-01-01 00:00:00'),
strtotime($this->create_year.'-12-31 23:59:59')
);
}
...
}
Lastly change your filter to a dropdown to reduce invalid entries:
'filter' => CHtml::activeDropDownList(
$model,
'create_year',
array_combine(range(1900, 2099), range(1900, 2099),
array('prompt' => 'Any year')
)

How do I add an Excerpt box to custom post types in functions?

I just want the standard Excerpt box - not a metabox of my own creation, added to a Custom Post. The box shows up in Posts but not in Custom Posts. I've tried both of these older solutions but neither of them worked (maybe it's a WP 3.9 problem):
The custom post type name is "Scoop"
I added this to the register_post_type_scoop() $labels = array
'supports' => array('title','thumbnail','excerpt')
but it didn't work - neither did this:
add_post_type_support('Scoop', 'title');
add_post_type_support('Scoop', array('title', 'thumbnail', 'excerpt') );
Add a index value excerpt to the supports object. Below the example is:
add_action( 'init', 'create_testimonial_posttype' );
function create_testimonial_posttype(){
register_post_type( 'testimonials',
array(
'labels' => array(
'name' => __( 'Testimonials' ),
'singular_name' => __( 'Testimonial' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'clients'),
'supports' => array('title','thumbnail','editor','page-attributes','excerpt'),
)
);
}

Ajax request in magento module

I developing magneto module, In this module i want to make a ajax request in admin panel.I don't understand were to add script and controllers.please help me.
My requirement:
When change the select box(On change) i want add some field in the form.
Mycode:
/app/code/local/<Namespace>/<Module>/Block/Adminhtml/<Module>/Edit/Tab/Form.php
$fieldset->addField('type', 'select', array(
'label' => Mage::helper('<Module>')->__('Article Type'),
'name' => 'status',
'values' => array(
array(
'value' => '',
'label' => Mage::helper('<Module>')->__('Choose'),
),
array(
'value' => 1,
'label' => Mage::helper('<Module>')->__('Normal'),
),
array(
'value' => 2,
'label' => Mage::helper('<Module>')->__('video'),
),
),
'required' => true
));
I am creating fields using this.
How can i add another field on change.
Where should i add script
Where should i add controller
I created this module using this instructions
Let this field always be on the form. But make it invisible and show when you need it.
In the same template where the form is.
Place controller in your module. On stackoverflow you can find many questions about creating your own magento module and controller.

CakePHP Search Plugin, search for a value equal or higher than the given in the search

I'm using the CakePHP Search Plugin, and there's something I can't get to work. I would like to have a search field (let's call it age) where a user can input a value (like 24, for example) and get as a result all the elements with a value in the age field of the table who have age 24 or higher. How can I do that?
So far I can perform normal searches, like by name or by an exact value (if you want age 24 exactly then it works fine).
Here's the code related with the Search Plugin that I have so far:
- model:
class Person extends AppModel{
var $name = 'Person';
public $actsAs = array('Searchable');
public $filterArgs = array(
array('name' => 'name', 'type' => 'like'),
array('name' => 'age', 'type' => 'value'),
);
}
- controller:
class PersonsController extends AppController {
var $name = 'Persons';
var $components = array('Search.Prg');
public $presetVars = array(
array('field' => 'name', 'type' => 'value'),
array('field' => 'age', 'type' => 'value'),
);
function index() {
$this->set('persons', $this->Person->find('all'));
}
/* ---------------- SEARCH PLUGIN --------------*/
public function find() {
$this->Prg->commonProcess();
$this->paginate['conditions'] = this->Game->parseCriteria($this->passedArgs);
$this->set('persons', $this->paginate());
}
}
- find.ctp:
//apart from the code below, I have all the code to show the results
echo $form->create('Game', array(
'url' => array_merge(array('action' => 'find'), $this->params['pass'])
));
echo $form->input('name', array('div' => false));
echo $form->input('age', array('div' => false));
echo $form->submit(__('Search', true), array('div' => false));
echo $form->end();
To sum up: with the code above, I can perfomr a search of an exact value for the age field. How do I change that to perform a search of 'age >= value entered'?
THank you so much in advance!
So this is how I solved it (or at least it seems to work with all the test I've done so far):
Changed the 'age' line by the following line in the filterArgs array in the model:
array('name' => 'age', 'type' => 'query', 'method' => 'findByAge', 'field' => 'Person.age'),
Added the following method in the same model:
public function findByAge($data = array()) {
$query = array(
'Person.age >=' => $data['age'],
);
return $query;
}
That's it! With the 'query' type, any complex query can be added to the main query using a method. Thanks to satyrwilder for pointing in the right direction!
From your presetVars -
array('field' => 'age', 'type' => 'value'),
-- filters down to your find() method from your commonProcess() method, which I presume constructs a generalized find like conditions=>array($key=>$val)?
Cake sees array key=>val pair assignments as an indication to generate a "WHERE $key=$val" condition.
To do a fuzzier pull, wherever you construct that generic find() - add a method or something just to roll those two into
'conditions'=>array("name LIKE '%{$name}%'", "{$age}>={$val}")
or whatever you've got going on. String the arguments together for the desired effect on your query and I'm sure you can extrapolate further ways to abstract that query, if needed.
If you need a more complex solution, Cake provides elegant built-in, extremely powerful subquery generation. It seems like you're working awfully hard to achieve a lot of built-in functionality, but then again I don't know how complicated your situation is.
http://book.cakephp.org/view/1030/Complex-Find-Conditions
HTH
I know this has been answered but there is another method that may be more suitable to this situation.
From the documentation
'expression' type: useful if you want to add condition that will generate by some method, and condition field contain several parameter like in previos sample used for 'range'. Field here contains 'Article.views BETWEEN ? AND ?' and Article::makeRangeCondition returns array of two values.
Full Article Here
I had to accomplish the same thing you did but with 5 different fields. I did not want to have to write a query for each one. Here is how I accomplished the same thing with the expression type. This is in the filterArgs array.
array(
'name'=>'bathrooms',
'type'=>'expression',
'method'=>'returnNumber',
'field'=>'House.bathrooms >= ?'
)
Then I created this function in the model that simply returns the number the user submitted. When called, returnNumber will be passed two arrays.
Looks something like this.
Array
(
[0] => Array
(
[bathrooms] => 6
[bedrooms] => 5
[acres] => 12
[city] => Greenville
[year] => 2009
)
[1] => Array
(
[name] => bathrooms
[type] => expression
[method] => returnNumber
[field] => House.bathrooms >= ?
)
)
Now for the actual function. You can use the debug statement to see what you are actually passing the function.
function returnNumber($arg, $name){
// debug(func_get_args());
// exit;
return $arg[$name['name']];
}

add custom order attribute to the order from in magento admin

I've added a custom order attribute and updated the onepage checkout page.
Now I'm trying to add this attribute to the new order form in the admin.
I'm trying to extend Mage_Adminhtml_Block_Sales_Order_Create_Form_Account and add a new field in the _prepareForm() method similar to the way the Group and Email fields are added.
How do I get the order attribute?
I've tried several ways but nothing works.
This is how I'm creating the custom order attribute in the mysql-install file:
$attribute = array(
'type' => 'int',
'label' => 'myattr',
'visible' => false,
'required' => false,
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
);
$installer->addAttribute('order', 'myattr', $attribute);
and this is how I'm trying to get the attribute:
$res = Mage::getSingleton('core/resource');
$eav = Mage::getModel('eav/config');
$attr = $eav->getAttribute('sales/order', 'myattr');
or with this:
$entityType = Mage::getModel('eav/config')->getEntityType('order');
$entityTypeId = $entityType->getEntityTypeId();
$attribute = Mage::getResourceModel('eav/entity_attribute_collection')
->setCodeFilter('myattr')
->setEntityTypeFilter($entityTypeId)
->getFirstItem();
or this:
$order = Mage::getResourceSingleton('sales/order');
$myAttr = $order->getAttribute('myattr');
None of them work.
Have you verified that the attribute is being added to eav_attribute table in the database with the correct entity_type_id? (I think sales_order is 11 by default, but don't assume that)
At first glance, it looks like you should be using
$installer->addAttribute('sales/order', 'myattr', $attribute);
HTH,
JD
Sales/Order used to use the EAV Model which supports attributes, that was before 1.4.0 or so not sure.
I think now you should do:
$installer->getConnection()->addColumn($installer->getTable('sales/order'), 'my_column', 'decimal(12,4)');
you can still add the attribute as static field
$installer->addAttribute('order', 'my_column', array('type'=>'static'));
What I noticed after much trial-and-error was that the new attribute must have a default (non-null) value in order to work. Somehow the attribute is not writable if it has a 'NULL' value in the database. So using this attribute options array worked for me:
$attribute = array(
'type' => 'int',
'label' => 'myattr',
'default' => 0,
'visible' => false,
'required' => false,
'user_defined' => true,
'searchable' => false,
'filterable' => false,
'comparable' => false );

Resources