How to print a string into a array - php

I tried to print a string into a array.
The form will be submitted trough a array. The amount of forms is unknow because user is allowed to add extra inputs if needed (Order system).
To run my validation of the form it needs to be like this:
$toxValidate = new toxValidate();
$toxValidation = $toxValidate->check('$_POST', array(
'part1' => array('required' => TRUE),
'part2' => array('required' => TRUE),
'part3' => array('required' => TRUE),
'part4' => array('required' => TRUE),
'part5' => array('required' => TRUE)
));
But because we dont know how many inputs it will be, there needs for every input one line of: 'part1' => array('required' => TRUE),.
This is what i tried:
for($i=0; $i<count($part); $i++){
$parts .= "part".$i." => array('required' => true)";
$t0x = $t0x + 1;
if($t0x < count($part)){
$parts .= ', ';
}
}
This would be the var_dump output of $parts when submitting 3 forms
''part0' => array('required' => true), 'part1' => array('required' => true), 'part2' => array('required' => true)'
That looks exactly what needs to be printed in:
$toxValidation = $toxValidate->check($_POST, array(
#HERE#
));
i tried with print and echo which i allready tought wouldt work.
How can i print $parts into the array.
Update
HTML Form:
<div id="form">
<p class="order">
<label style="margin-left:20px;">Partnumber:</label><br />
<input type="text" style="margin-left:20px;" class="text medium" id="part" name="part[]" placeholder="SP partnumber" />
</p>
<p class="order" >
<label style="margin-left:20px">Qty</label><br>
<input type="text" id="qty" name="qty[]" style="margin-left:20px;" class="text small" placeholder="Qty" />
<p class="order">
<label style="margin-left:20px">Price</label><br>
<input type="text" style="margin-left:20px;" class="text small" id="price" placeholder="Price" tabindex="-1" readonly /><br>
</p>
</div><p>
jQuery to add a other line of inputs:
$('#addpart').click(function(){
var loop = $('#loop').val();
var html;
html = '<p>';
html += '<input type="text" name="part[]" style="margin-left:20px;" class="text medium" id="part" placeholder="SP partnumber" />';
html += '<input type="text" name="qty[]" style="margin-left:20px;" class="text small" placeholder="Qty" />';
html += '<input type="text" style="margin-left:20px;" class="text small" id="price" placeholder="Price" tabindex="-1" readonly />';
html += '</p>';
for (i = 0; i < loop; i++) {
$("#form").append(html);
}
});
Update 2
But there also needs to be other 'inputs' to be check. Like Purchase order, backorder, date. See below:
$toxValidation = $toxValidate->check('$_POST', array(
'po' => array('required' => TRUE),
'date' => array('required' => TRUE),
'bo' => array('required' => TRUE),
'remarks' => array('required' => TRUE),
#PARTS HERE#
));

Related

Changing Radio button Input template using CakePHP 3

I would like change structure but it does not work, it's always the same structure. How to solve my problem?
<?php
$default = 'wepay';
$options = array('wepay' => 'WePay', 'paypal' => 'Paypal', 'donorbank' => 'Donor Bank');
echo $this->Form->input('payment_method', [
'templates' => [
'radioWrapper' => '<label class="radio-inline">{{label}}</label>'],
'type' => 'radio',
'options' => $options,
'default' => $default
]);
?>
But this code does not generate output properly. I want to generate output like below:
<label class="radio-inline">
<input type="radio" name="payment_method" value="wepay" id="payment-method-wepay" checked="checked">WePay
</label>
<label class="radio-inline">
<input type="radio" name="payment_method" value="paypal" id="payment-method-paypal">Paypal
</label>
<label class="radio-inline">
<input type="radio" name="payment_method" value="donorbank" id="payment-method-donorbank">Donor Bank
</label>
My Code generate output like below:
<label class="radio-inline">
<label for="payment-method-wepay">
<input type="radio" name="payment_method" value="wepay" id="payment-method-wepay" checked="checked">WePay
</label>
</label>
<label class="radio-inline">
<label for="payment-method-paypal">
<input type="radio" name="payment_method" value="paypal" id="payment-method-paypal">Paypal
</label>
</label>
<label class="radio-inline">
<label for="payment-method-donorbank"><input type="radio" name="payment_method" value="donorbank" id="payment-method-donorbank">Donor Bank
</label>
</label>
Here one extra label field added. How can I remove this extra-label?
What you are showing there is pretty much the default output, except for the class attribute and the missing for attribute on the label. So what you want to modify most probably is the nestingLabel template, surely not the radioWrapper one:
'nestingLabel' =>
'{{hidden}}<label class="radio-label"{{attrs}}>{{input}}{{text}}</label>'
If you want to remove the for attribute, then you'll have to remove the {{attrs}} placeholder, that way no further attributes can be added to the label element.
Finally, solved my problem using #ndm hints. Final solution like this below
<?php
$default = 'wepay';
$options = array('wepay' => 'WePay', 'paypal' => 'Paypal', 'donorbank' => 'Donor Bank');
echo $this->Form->input('payment_method', [
'nestingLabel' =>
'{{hidden}}<label class="radio-inline"{{attrs}}>{{input}}{{text}}</label>',
'type' => 'radio',
'options' => $options,
'default' => $default
]);
?>
Hope this solution help others in future.

Inserting photo and get photo

Hi i want to make it possible when user sign up default photo upload in my DB right now i'm upload it but i don't know if its correct or not here its the code:
<?php
require_once 'includes/header.php';
$profile_pic = basename('files/img/default.jpg');
if (Input::exists()) {
//also here we check our token to see if input
if (Token::check(Input::get('token'))) {
//new instance
$validate = new Validate();
//now here we check our validation with check method
//fist of all we check if its POST then:
$validation = $validate->check($_POST,array(
//contain all rulles we want
//first of all in username as we set in our DB
'username' => array(
//its required
'required' => true,
//minimum character 2
'min' => 2,
//maximum character 20
'max' => 20,
//and we want it unique on users table
'unique' => 'users'
),
'name' => array(
'required' => true,
//minimum character 2
'min' => 2,
//maximum character 20
'max' => 50,
),
'email' => array(
'required' => true,
//minimum character 2
'min' => 10,
//maximum character 20
'max' => 50,
),
'password' => array(
'required' => true,
//minimum character 2
'min' => 6,
),
'SecondPassword' => array(
'required' => true,
//check for be match with password1
'matches' => 'password'
),
));
//is passed
if($validation->passed()){
$user = new User();
$salt = Hash::salt(32);
try {
$user->create(array(
'username' => Input::get('username') ,
'name' => Input::get('name') ,
'email' => Input::get('email') ,
'password' => Hash::make(Input::get('password'),$salt) ,
'salt' => $salt,
'joined' => date('Y-m-d H:i:s') ,
'lastlogin' => date('Y-m-d H:i:s'),
'avatar' => $profile_pic
));
Session::flash('home','<span class="success-sign">Thank you for your
regsiteration Please log in</span> ');
Redirect::to('index.php');
} catch (Exception $e) {
die($e->getMessage());
}
}else {
//if error
foreach ($validation->errors() as $error) {
echo "<span class='error-sign'>$error</span><br>";
}
}
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<label for="username" class='field-name'>Username</label><br>
<!--here in value we declare it to use get method from Input class to get
username for when we see error we dont miss this value and also for name -->
<input type="text" name="username" id="username"
value="<?php echo Input::get('username');?>" autocomplete="off"
onBlur ='checkUser(this)' class='signup'>
<span id='info'></span>
<br>
<label for="name" class='field-name'>Your name</label><br>
<input type="text" name="name"
value="<?php echo Input::get('name');?>" id="name" class='signup' >
<br>
<label for="email" class='field-name'>Your Email</label><br>
<input type='email' name='email' id="email" value="<?php echo
Input::get('email'); ?>"
class='signup'><br>
<label for="password1" class='field-name'>Password</label><br>
<input type="password" name="password" id="password" class='signup'>
<br>
<label for="secondpassword" class='field-name-re'>Retype Password</label>
<br>
<input type="password" name="SecondPassword" id="SecondPassword"
class='signup'>
<br>
<!--this is for token value-->
<input type="hidden" name="token" value="<?php echo Token::generate();?>">
<br>
<span class='fieldname'> </span>
<input type="submit" value="SignUp" class='info-signup'>
</form>
<br>
now as i said i see that in my DB the default.jpg is exists when user sign up but first i don't know if its photo or just name of photo and second i cant show this photo in user profile page
You Never want to actually save an Image's actual data into the database.
With that being said, your code stores the Path name into the database like how it should have been done.
Double check if you are using the correct path.
use
<img src="<?php echo $your_avatar_url; ?> />
to output your image.
look into How to have user upload the image here
http://www.w3schools.com/php/php_file_upload.asp
You need to actually save POSTed photo. Check out move_uploaded_file function.

How to make custom Radio-Button labels in ZF2 forms?

I have a form with Radio-button:
$this->add([
'name' => 'time',
'options' => [
'value_options' => [
'0' => '9:00 - 12:00',
'1' => '12:00 - 16:00',
'2' => '16:00 - 19:00',
],
'label_attributes' => [
'class' => 'WW_OBJ_fm-label',
]
],
'type' => 'Radio'
]);
In the view I make the output like this:
<div>
<?php echo $this->formElement($form->get('time')); ?>
</div>
and get the output (formatted for readability):
<div>
<label class="WW_OBJ_fm-label">
<input type="radio" name="time" value="0"/>
9:00 - 12:00
</label>
<label class="WW_OBJ_fm-label">
<input type="radio" name="time" value="1"/>
12:00 - 16:00
</label>
<label class="WW_OBJ_fm-label">
<input type="radio" name="time" value="2"/>
16:00 - 19:00
</label>
</div>
But I need, that label text ist wrapped by a <span>:
<div>
<label class="WW_OBJ_fm-label">
<input type="radio" name="time" value="0"/>
<span class="WW_label-text">9:00 - 12:00</span>
</label>
<label class="WW_OBJ_fm-label">
<input type="radio" name="time" value="1"/>
<span class="WW_label-text">12:00 - 16:00</span>
</label>
<label class="WW_OBJ_fm-label">
<input type="radio" name="time" value="2"/>
<span class="WW_label-text">16:00 - 19:00</span>
</label>
</div>
What is the best way to achieve it?
I see three possible solutions for your problem.
1) Extend the Zend\Form\View\Helper\FormRadio class, overriding the renderOptions method, replicating almost entirely the one that you can find in Zend\Form\View\Helper\FormMultiCheckbox but maybe adding an option to pass optional attributes to the span element
2) Very subtle, but could save you writing some code: using the translator. Since the radio value options are translated, you could keep your values defined in the configuration but adding the span element in the transation
3) Do not use $this->formElement to display the element, but actually write all the html
A solution is to use labelOption 'disable_html_escape' :
$this->add([
'name' => 'time',
'options' => [
'value_options' => [
'0' => '<span class="WW_label-text">9:00 - 12:00</span>',
'1' => '<span class="WW_label-text">12:00 - 16:00</span>',
'2' => '<span class="WW_label-text">16:00 - 19:00</span>',
],
'label_attributes' => [
'class' => 'WW_OBJ_fm-label',
]
],
'type' => 'Radio'
]);
$element = $this->get('time');
$element->setLabelOptions(['disable_html_escape' => true]);

Create a form which includes radio buttons with Zend Framework 1.12?

I use the Zend Framework 1.12, and I am trying to create a form with Zend_Form like this:
<form action="">
<div class="bb-validator-form">
<div class="form-question">
Question 1?
</div>
<div class="form-row" >
<input type="radio" name="form-val-q" id="yes" class="radio radio-yes"/>
<label for="yes"><?php echo $this->tra->_('Yes'); ?></label>
</div>
<div class="form-row">
<input type="radio" name="form-val-q" id="no" class="radio radio-no"/>
<label for="no"><?php echo $this->tra->_('No'); ?></label>
</div>
<input type="submit" class="btn btn-primary btn-block" value="<?php echo $this->tra->_('Validate'); ?>"/>
</div>
My code in Zend Form is:
class Form_BB_Validator extends Zend_Form {
public function init()
{
$this->setMethod(Zend_Form::METHOD_POST)
->setAttribs(array('id' =>'bb-Form'))
->addDecorator('FormElements')
->addDecorator('HtmlTag', array('tag' => 'div','class'=>'bb-validator-form'))
->addDecorator('Form');
$this->addElement('text','question-1',array(
'label' => 'test',
'decorators'=>array(array('Callback',array('callback'=>
create_function('','return "<h4> Question 1 ?</h4>";'))))
));
$this->addElement('radio','checkValidation', array(
'class' => 'radio',
'escape' => false,
'required' => true,
'multioptions' => array('yes'=>'Yes', 'no'=>'No'),
'decorators' => array(
array('FormElements', array('HtmlTag', array('tag'=>'div','class'=>'bb-validator-form'))),
array('ViewHelper', array('Label', array('tag'=>'div','class'=>'bb-validator-form'))),
)
));
$this->addElement(new Zend_Form_Element_Submit('submit', array(
'label' => 'Valider',
'class' => 'btn btn-primary btn-block',
'decorators' => array(array('ViewHelper', array('Label', array('tag'=>'div','class'=>'bb-validator-form'))),
))));
}
}
The final output is:
<form id="bb-Form" enctype="application/x-www-form-urlencoded" action="" method="post">
<div class="bb-validator-form">
<h4>Question 1 ?</h4>
<dt id="checkValidation-label"> </dt>
<label for="checkValidation-yes">
<input type="radio" name="checkValidation" id="checkValidation-yes" value="yes" class="radio">Oui</label>
<br>
<label for="checkValidation-no">
<input type="radio" name="checkValidation" id="checkValidation-no" value="no" class="radio">Non</label>
<input type="submit" name="submit" id="submit" value="Valider" class="btn btn-primary btn-block">
</div>
</form>
This is not correct. How do I create the form like first piece of code?
You could use custom viewScript. Like this:
$this->setDecorators(
array(
array(
'ViewScript',
array(
'viewScript' => $viewsPath . '/form-bb-validator.phtml'
)
)
)
);
In form-bb-validator.phtml you can design your html output like this:
<form>...
<div>
<?php echo $this->element->question-1; ?>
</div>
...
</form>

Cakephp - Checkbox names

Currently, i have succeeded in creating the checkbox.
The array which i have setup is as below:
$emailName = $this->User->find('list', array(
'fields' => array('User.username', 'User.email')
));
The output is as follows:
array
'admin' => string 'asd#asd.asd' (length=11)
'test' => string 'test#test.test' (length=14)
'Floo' => string 'XXXX#gmail.com' (length=16)
I'm trying to make the checkbox shows the username instead of the user email in view.ctp.
I have tried using the following code in view.ctp
<?php echo $this->Form->input('Address_list.['.$emailName['username'].']', array(
'type' => 'select',
'multiple' => 'checkbox',
'options' => $emailName['email']
)); ?>
However, it seems that this doesn't work. Any ideas?
You are not formatting your form field for the checkbox list correctly. Try changing it to this:
echo $this->Form->input('Address_list', array(
'multiple' => 'checkbox',
'options' => $emailName,
));
However, this will return the value of Username based on the selection of Email the user chooses. It creates the form like this:
<label for="Address_list">Address List</label>
<input type="hidden" id="Address_list" value="" name="data[Address_list]"/>
<div class="checkbox"><input type="checkbox" id="AddressListAdmin" value="admin"
name="data[Address_list][]"/><label for="AddressListAdmin">asd#example.com</label></div>
<div class="checkbox"><input type="checkbox" id="AddressListTest" value="test"
name="data[Address_list][]"/><label for="AddressListTest">test#example.com</label></div>
<div class="checkbox"><input type="checkbox" id="AddressListFloo" value="Floo"
name="data[Address_list][]"/><label for="AddressListFloo">XXX#example.com</label></div>

Categories