DropDownList won't POST value by using ajax - yii

I created DropDownList and I'm trying to fill div with content and I know how to do it. But problem is that my dropdown list won't POST value.
This is code from view\index.php
<?php
echo CHtml::dropDownList('parovi', '', $model->dropDownListParovi(), array
(
'class'=>'dropDownListLegloIzaberiPar',
'empty'=>array('-'=>Yii::t('default', 'PAR_UZGOJNI_DNEVNIK_LEZENJA_IZABERI_PAR')),
'ajax'=>array
(
'type'=>'POST',
'url'=>Yii::app()->createUrl('/paruzgojni/ajaxIzlistajLegla'),
'update'=>'#legla',
),
)
);
?>
<div id="legla"></div>
And this is from controller
public function actionAjaxIzlistajLegla()
{
echo $_POST['parovi'];
}
this AjaxIzlistaijLegla action is inside accessRules(). And it's working cause When I replace echo $_POST['parovi']; with echo "Hello"; it works, it updates my div. But I don't know why it won't POST that value from drop down list.
jquery is included in head.

Answer is I should wrap my dropDownList with CActiveForm like this
$this->beginWidget('CActiveForm', array(
'id'=>'par-uzgojni-ispis-legla-form',
));
echo CHtml::dropDownList('parovi', '', $model->dropDownListParovi(), array
(
'class'=>'dropDownListLegloIzaberiPar',
'empty'=>array('-'=>Yii::t('default', 'PAR_UZGOJNI_DNEVNIK_LEZENJA_IZABERI_PAR')),
'ajax'=>array
(
'type'=>'POST',
'url'=>Yii::app()->createUrl('/paruzgojni/ajaxIzlistajLegla'),
'update'=>'#legla',
),
)
);
$this->endWidget();
Because I didn't wrap it, it couldn't POST any data to my controller

Related

get value of CHtml::activeDropDownList in yii

I have this code
<?php echo CHtml::activeDropDownList(
$semaineModel,
'libelleSemaine',
CHtml::listData(Semaine::findBySql('SELECT * FROM Semaine')->all(), 'idSemine', 'libelleSemaine')
); ?>
but why that displays just the last element of the table, and me I have 6 items in this table 'Semaine 1' to 'Semaine 6'
and that code display just 'Semaine 6'.
an idea please ?
Then you don't nedd CHtml but active dropDownList
Assiming your Semain model is named Semain
use app\models\Semaine;
use yii\helpers\ArrayHelper;
$semaines=Semaine::find()->all();
$listSemaines = $listData=ArrayHelper::map($semaines,'idSemine', 'libelleSemaine');
echo $form->field($model, 'idSemaine')->dropDownList( $listSemaines,
['prompt'=>'Select Semaine...']);

How can I change the checkboxlist to look similar to on/off switch?

Is there any way to change the checkboxlist widget to look similar to the on/off switch presented in Kartik's Switch Input Widget, but in a group?
I'm using this code:
echo $form->field($model, 'blocked_list')->checkboxList($array_list);
Which is very simple to use, but produces "simple" list...
I've tried with Karitk's like this:
foreach ($array_list as $category_id=>$category_name) {
echo '<label class="control-label">' . $category_name . '</label>';
echo SwitchInput::widget([
'name'=>'blocked_list',
'value'=>in_array($category_id, $model->blocked_list),
'pluginOptions' => [
'size' => 'mini',
],
]);
But it does not link with the model, and the Form looses it format
Any ideas, please?
I've got it!
foreach ($category_array as $category_id=>$category_name) {
if ( isset($model->blocked_list[$category_id]) )
$model->blocked_list[$category_id] = true;
else
$model->blocked_list[$category_id] = false;
echo $form->field($model, 'blocked_list[' . $category_id . ']')
->label($category_name)
->widget(SwitchInput::classname(), []);
}
I just added a validate "if" to ask if the variable to display exists, and then set the correct value for the model
Hope it helps someone

Edit woocommerce Login register page

I am using woocommerce for an eCommerce website. I want to add one more field in Login Regiser page. On this page there are three fields in registration (Email, Password and Re-enter password) and I want to add one more field for phone number.
Can anybody help me ? Thanks in Advance
http://wordpress.org/plugins/woocommerce/
If anyone else is curious on how to add additional fields to the login/register/any other account forms. You can achieve this quite easily since WooCommerce has added those to their templates folder. All you have to do is copy-paste the form you need to modify from plugins>woocommerce>templates>myaccount and drop it into your own theme. After which, you can add your extra fields and add any other functionality to make them work using http://docs.woothemes.com/document/hooks/
Hope this is of help to anyone who's been stuck on this.
I Use the "Register Plus Redux" plugin and use the "billing_phone" Database key to map to the correct field in the the User database. You can also have the user add other Information such as "'billing_address_1" and any others.
Best Regards
Marc
Old post, but this answer deals with the question as asked and provides a direct answer form the WordPress Codex
I have modified the code from the pages linked to act inline with the zipcode example that appears on the validation page. Each page within the codes used different examples, I just made them all the same.
Add the custom field
Plugin API/Action Reference/register form
The 'register_form' action hook is used to customize the built-in WordPress registration form.
Use in conjunction with 'registration_errors' (for validation) and 'register_post' (save extra data) when customizing registration.
WordPress MS Note: For Wordpress MS (Multi-Site), use the 'signup_header' action to redirect users away from the signup. (emphasis mine, took me a while to realise this)
Example
This example demonstrates how to add a new field to the registration form. Keep in mind that this won't be saved automatically. You will still need to set up validation rules and manually handle saving of the additional form fields.
add_action( 'register_form', 'myplugin_add_registration_fields' );
function myplugin_add_registration_fields() {
//Get and set any values already sent
$zipcode = ( isset( $_POST['zipcode'] ) ) ? $_POST['zipcode'] : '';
?>
<p>
<label for="user_extra"><?php _e( 'Extra Field', 'myplugin_textdomain' ) ?><br />
<input type="text" name="user_extra" id="user_extra" class="input" value="<?php echo esc_attr( stripslashes( $zipcode ) ); ?>" size="25" /></label>
</p>
<?php
}
link: https://codex.wordpress.org/Plugin_API/Action_Reference/login_form
Validation
You need to validate the user input (obviously) and should run that throughregistration_errors filter hook. Be mindful, the $errors variable may already be populated by other errors from the registration process. As in the examples, you would add your error to errors already contained therein.
Returning an Error
function myplugin_check_fields( $errors, $sanitized_user_login, $user_email ) {
$errors->add( 'demo_error', __( '<strong>ERROR</strong>: This is a demo error.', 'my_textdomain' ) );
return $errors;
}
add_filter( 'registration_errors', 'myplugin_check_fields', 10, 3 );
Validating a Custom Field
(this next section deals specifically with validation of the custom field)
Assuming you wanted to validate a postal code field that you have already created using the register_form hook, you might validate the field like so:
function myplugin_check_fields( $errors, $sanitized_user_login, $user_email ) {
if ( ! preg_match('/[0-9]{5}/', $_POST['zipcode'] ) ) {
$errors->add( 'zipcode_error', __( '<strong>ERROR</strong>: Invalid Zip.', 'my_textdomain' ) );
}
return $errors;
}
add_filter( 'registration_errors', 'myplugin_check_fields', 10, 3 );
link: https://codex.wordpress.org/Plugin_API/Filter_Reference/registration_errors
finally, don't forget to save the custom field's data!
add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
function myplugin_registration_save( $user_id ) {
if ( isset( $_POST['zipcode'] ) )
update_user_meta($user_id, 'zipcode', $_POST['zipcode']);
}
link: https://codex.wordpress.org/Plugin_API/Action_Reference/user_register
A complete example
At the end of all this I ended up coming across a tutorial to do exactly this. In this example they are adding a First Name field:
//1. Add a new form element...
add_action( 'register_form', 'myplugin_register_form' );
function myplugin_register_form() {
$first_name = ( ! empty( $_POST['first_name'] ) ) ? sanitize_text_field( $_POST['first_name'] ) : '';
?>
<p>
<label for="first_name"><?php _e( 'First Name', 'mydomain' ) ?><br />
<input type="text" name="first_name" id="first_name" class="input" value="<?php echo esc_attr( $first_name ); ?>" size="25" /></label>
</p>
<?php
}
//2. Add validation. In this case, we make sure first_name is required.
add_filter( 'registration_errors', 'myplugin_registration_errors', 10, 3 );
function myplugin_registration_errors( $errors, $sanitized_user_login, $user_email ) {
if ( empty( $_POST['first_name'] ) || ! empty( $_POST['first_name'] ) && trim( $_POST['first_name'] ) == '' ) {
$errors->add( 'first_name_error', sprintf('<strong>%s</strong>: %s',__( 'ERROR', 'mydomain' ),__( 'You must include a first name.', 'mydomain' ) ) );
}
return $errors;
}
//3. Finally, save our extra registration user meta.
add_action( 'user_register', 'myplugin_user_register' );
function myplugin_user_register( $user_id ) {
if ( ! empty( $_POST['first_name'] ) ) {
update_user_meta( $user_id, 'first_name', sanitize_text_field( $_POST['first_name'] ) );
}
}
https://codex.wordpress.org/Customizing_the_Registration_Form

YII Call to a member function getErrors() on a non-objec

Following this tutorial; When I try to get the data to display in the form and update
For my error was:
$n = $this->loadModel($id);
I want to make two models with one form, this is my code for update:
Controller:
public function actionUpdate($id)
{
$n = new Noticias;
$m = new Multimedia;
$this->performAjaxValidation(array($n,$m));
$n=$this->loadModel($id);
if(isset($_POST['Noticias'],$_POST['Multimedia']))
{
$n->attributes=$_POST['Noticias'];
$m->attributes=$_POST['Multimedia'];
$m->ID_NOT=$n->ID;
$m->setIsNewRecord(false);
if($n->save())
$this->redirect(array('admin','id'=>$n->ID));
}
$this->render('update',array('n'=>$n,'m'=>$m));
}
update.php
<?php echo $this->renderPartial('_form', array('n'=>$n, 'm'=>$m)); ?>
some view
//get Multimedia FK
<?php if ($n->isNewRecord==false) {
$m=Multimedia::model()->findByPk($n->ID);
} ?>
//Validation
<?php echo $form->errorSummary(array($n,$m)); ?>
//Field FOTO between other
<div class="row">
<?php echo $form->labelEx($m,'FOTO_URL'); ?>
<?php echo $form->textField($m,'FOTO_URL',array('size'=>25,'maxlength'=>100)); ?>
<?php echo $form->error($m,'FOTO'); ?>
</div>
First - do not load models in view
<?php if ($n->isNewRecord==false) {
$m=Multimedia::model()->findByPk($n->ID);
} ?>
This part should be in controller action.
Make sure $m and $n are really models. If findByPk statement above fails, $m will be null, so you will get error from errorSummary which calls $m->getErrors() and $n->getErrors(). In short - make sure $n and $m are properly initialized - both must be instances of model, either empty, or filled from db, but in your case one is null. Most probably $m
this error occur when Noticias (ie $n is newrecord) or $n->ID is not found in Multimedia
you can solve this if you need to work even if its a new record you can use.
if ($n->isNewRecord==false) {
$m=Multimedia::model()->findByPk($n->ID);
}
else
$m=new Multimedia;
You can try to load both models in Controller (ex. in actionUpdate) :
public function actionUpdate($id)
{
$n = new Noticias;
$m = new Multimedia;
$this->performAjaxValidation(array($n,$m));
$n=$this->loadModelNoticias($id);
$m=$this->loadModelMultimedia($n->ID); //just put it here..
if(isset($_POST['Noticias'],$_POST['Multimedia']))
{
$n->attributes=$_POST['Noticias'];
$m->attributes=$_POST['Multimedia'];
$m->ID_NOT=$n->ID;
$m->setIsNewRecord(false);
if($n->save())
$this->redirect(array('admin','id'=>$n->ID));
}
$this->render('update',array('n'=>$n,'m'=>$m));
}
Please be noticed that you have to define both loadModel functions in the same controller too.
function loadModelNoticias($id){
$model = Noticias::model()->findByPk($id);
return $model;
}
function loadModelMultimedia($id){
$model = Multimedia::model()->findByPk($id);
return $model;
}

symfony get data from array

I'm trying to use an SQL query to get data from my database into the template of a symfony project.
my query:
SQL:
SELECT l.loc_id AS l__loc_id, l.naam AS l__naam, l.straat AS l__straat,
l.huisnummer AS l__huisnummer, l.plaats AS l__plaats, l.postcode AS l__postcode,
l.telefoon AS l__telefoon, l.opmerking AS l__opmerking, o.org_id AS o__org_id, o.naam AS o__naam
FROM locatie l
LEFT JOIN organisatie o
ON l.org_id = o.org_id
This is generated by this DQL:
DQL:
$this->q = Doctrine_Query::create()
->select('l.naam, o.naam, l.straat, l.huisnummer, l.plaats, l.postcode, l.telefoon, l.opmerking')
->from('Locatie l')
->leftJoin('l.Organisatie o')
->execute();
But now when i try to acces this data in the template by either doing:
<?php foreach ($q as $locatie): ?>
<?php echo $locatie['o.naam'] ?>
or
<?php foreach ($q as $locatie): ?>
<?php echo $locatie['o__naam'] ?>
i get the error from symfony:
500 | Internal Server Error | Doctrine_Record_UnknownPropertyException
Unknown record property / related component "o__naam" on "Locatie"
Does anyone know what is going wrong here? i dont know how to call the value from the array if the names in both query's dont work.
Doctrine will have hydrated your results into objects corresponding to the models in your query. In your case these will be Locatie and Organisatie. You should therefore be able to access the data as follows:
<?php foreach ($q as $obj): ?>
<?php echo $obj->Locatie->naam; ?>
<?php echo $obj->Organisatie->naam; ?>
<?php endforeach; ?>
If you have the above method in eg the Locatie table class and use self::create("l") to create your method, the object you use in the view won't need the ->Locatie part.
Edit: table method example:
class LocatieTable extends Doctrine_Table
{
public function getLocaties()
{
$q = self::createQuery("l")
->select('l.naam, o.naam, l.straat, l.huisnummer, l.plaats, l.postcode, l.telefoon, l.opmerking')
->leftJoin('l.Organisatie o')
->execute();
return $q;
}
}
You should be able to find this class (probably empty) already auto-generated in lib/model/doctrine/LocatieTable.class.php. Now call it with:
$this->q = Doctrine::getTable("Locatie")->getLocaties();
If you want to know how to get some value from the result DoctrineRecord object I advise to use var_dump($obj->toArray()) method to get clear view of the object structure. After that you can use several types of getters to retrive what you want (e.g. $obj->A->b, $obj->getA()->getB() etc..)