Yii2 radio button validation is not working - yii2

I have a form with multiple reference for a class(I mean table) in single form like add more details. Main goal of the task is a company have more than one users. each user have some access restrictions. so while adding company we will add multiple company users. company users form have that access radio button. here radio button validation is not working. same issue for checkbox and dropdown also.sample code given below. Please help.
AccessController.php
class AccessController extends Controller
{
public function actionCreate() {
$formDetails = Yii::$app->request->post('ClientAllowAccess', []);
if(!empty($formDetails)){
foreach ($formDetails as $i => $formDetail) {
$modelDetail = new ClientAllowAccess(['scenario' => ClientAllowAccess::SCENARIO_BATCH_UPDATE]);
$modelDetail->setAttributes($formDetail);
$modelClientAccess[] = $modelDetail;
}
} else {
$modelClientAccess[] = new ClientAllowAccess(['scenario' => ClientAllowAccess::SCENARIO_BATCH_UPDATE]);
}
return $this->render('create', [
'client_allow_access_multiple' => $modelClientAccess
]);
}
}
create.php
....
<?php foreach($client_allow_access_multiple as $i => $client_allow_acces): ?>
<?= $form->field($client_allow_access, '[$i]access_type')->radioList([1 => 'Allow access', 2 => 'Can\'t allow access'],[ 'item' => function($index, $label, $name, $checked, $value) {
$return = '<label class="modal-radio">';
$return .= '<input type="radio" name="' . $name . '" value="' . $value . '" id="custom_id_value_'.$index.'" >';
$return .= '<span> ' . ucwords($label) . '</span>';
$return .= '</label>';
return $return;
}]); ?>
<?php endforeach; ?>
....
rendered view
<div class="form-group field-clientcontactdetails-0-gender has-success">
<label class="control-label" for="clientcontactdetails-0-gender">Gender</label>
<select id="clientcontactdetails-0-gender" class="form-control" name="ClientContactDetails[0][gender]">
<option value="">---</option>
<option value="1">Male</option>
<option value="2">Female</option>
<option value="3">Others</option>
</select>
<div class="help-block"></div>
</div>
<div class="form-group field-clientallowaccess-0-access_type required">
<label class="control-label" for="clientallowaccess-0-access_type">Access Type</label>
<input type="hidden" name="ClientAllowAccess[0][access_type]" value=""><div id="clientallowaccess-0-access_type"><label><input type="radio" name="ClientAllowAccess[0][access_type]" value="1"> Allow access</label>
<label><input type="radio" name="ClientAllowAccess[0][access_type]" value="2"> Can't allow access</label></div>
<div class="help-block"></div>
</div>

Related

How to notification show after Form successful submit

I change my website template. i change Contact Form also previous contact form work correctly, when i change contact form template than submit form successful notification dont show under submit button. i checked all details but i can not find any error. help me this form.
form code
<form class="contact-form" id="contact-form" method="post" action="sendemail.php">
<div class="form-group tiple">
<input type="text" class="contact-box" name="name" value="" placeholder="Enter Your Name">
</div>
<div class="form-group tiple">
<input type="email" class="contact-box" name="email" value="" placeholder="Enter Your Email">
</div>
<div class="form-group tiple">
<input type="text" pattern="[6|7|8|9][0-9]{9}" class="contact-box" name="phone" value="" placeholder="10 Digit Mobile No.">
</div>
<div class="form-group">
<label>Subject</label>
<select name="subject" class="form-control">
<option value="Demo Registraion">Demo Registraion</option>
<option value="Contact Us">Contact Us</option>
<option value="Feedback">Feedback</option>
<option value="Other">Other</option>
</select>
</div>
<div class="form-group">
<textarea name="message" class="contact-box" placeholder="Your Message" rows="10"></textarea>
</div>
<button type="submit" class="short-line margin-top-10">Submit now</button>
</form>
sendmail.php code
<?php
// Define some constants
define( "RECIPIENT_NAME", "NAME" );
define( "RECIPIENT_EMAIL", "mail#email.com " );
// Read the form values
$success = false;
$senderName = isset( $_POST['name'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['name'] ) : "";
$senderEmail = isset( $_POST['email'] ) ? preg_replace( "/[^\.\-\_\#a-zA-Z0-9]/", "", $_POST['email'] ) : "";
$senderPhone = isset( $_POST['phone'] ) ? preg_replace( "/[6|7|8][0-9][9]/", "", $_POST['phone'] ) : "";
$subject = isset( $_POST['subject'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['subject'] ) : "";
$message = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message'] ) : "";
// If all values exist, send the email
if ( $senderName && $senderEmail && $message ) {
$recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
$headers = "From: " . $senderName . " <" . $senderEmail . ">";
$mailBody = 'Sender Name: ' . $senderName. "\r\n" . 'Sender Email: ' . $senderEmail . "\r\n" .'Sender Phone: '. $senderPhone . "\r\n" . 'Subject: ' . $subject . "\r\n" . 'Message: ' . $message;
$success = mail( $recipient, $subject, $mailBody, $headers );
echo 'Your Request Send Successful';
echo "<p class='success'>Thanks for contacting us. We will contact you ASAP!</p>";
?>
i want an notification under the submit button
msg submit successfull or failed
You can follow PRG pattern and use RedirectAttributes to add flash attributes.
For example:
#RequestMapping(value = "/contractor", method = RequestMethod.POST)
public String handle(#Valid #ModelAttribute Contractor contractor,
BindingResult result,
RedirectAttributes redirectAttributes) {
// Save contactor ...
redirectAttributes.addFlashAttribute("message", "Successful!");
return "redirect:/someGetUrl";
}
And just show this message in the view rendered by /someGetUrl handler.

Show the selected value everytime in the dropdown list

I need to display the selected category value displayed all the time. I have a list of category values and adding it to drop-down list from an array.
<form action="#" method="post">
<select name="dropDown" id="drop_down_id">
<option value=""> Select CATEGORY</option>
<?php
foreach($decoded as $key => $value ){
foreach($value as $key1 => $value1 ){
?> <option value="<?php echo $value1;?>"><?php echo $value1;?></option><?php
}
}
?>
<input type="submit" name="submit" value="Submit"/>
</form>
I see that you use post for form so you can use POST to know last selected:
$dropDown=htmlentities($_POST['dropDown'], ENT_QUOTES, "UTF-8");
And then just simple make if statement and its done:
if($dropDown==$value1){$selected='selected';}else{$selected=null;}
result:
<?php
$dropDown=htmlentities($_POST['dropDown'], ENT_QUOTES, "UTF-8");
?>
<form action="#" method="post">
<select name="dropDown" id="drop_down_id">
<option value=""> Select CATEGORY</option>
<?php
foreach($decoded as $key => $value ){
foreach($value as $key1 => $value1 ){
if($dropDown==$value1){$selected='selected';}else{$selected=null;}
echo '<option value="'.$value1.'" '.$selected.'>'.$value1.'</option>';
}
}
?>
<input type="submit" name="submit" value="Submit"/>
</form>
You can also store it in PHP SESSSION http://php.net/manual/en/function.session-start.php

id attribut is missing in yii2 radio button

I'm facing one problem in yii2 radio buttons while using with yii2 dynamic forms package. yii2 is not generating the radio button with id attribute. Due to missing of id attributes in radio buttons, yii2 dynamic forms radio buttons values are always setting as 1. So Please help me how to overcome this problem.
Edited
<?= $form->field($client_allow_acces, "[$i]access_type")->radioList([1 => 'Allow access', 2 => 'Can\'t allow access'], ['uncheckValue' => null, 'id'=>'custom_id_value']) ?>
I'm getting the html output below
<div class="form-group field-clientallowaccess-0-access_type required">
<label class="control-label" for="custom_id_value">Access Type</label>
<input type="hidden" name="ClientAllowAccess[0][access_type]" value="">
<div id="custom_id_value">
<label><input type="radio" name="ClientAllowAccess[0][access_type]" value="1"> Allow access</label>
<label><input type="radio" name="ClientAllowAccess[0][access_type]" value="2"> Can't allow access</label>
</div>
<div class="help-block"></div>
</div>
My custom id value is coming in div element. but I need it in radio button itself.
Thanks in advance.
You need to use following code because of your given id is assigned to div in your code. that is on <div id="custom_id_value">.
<?= $form->field($client_allow_acces, '[$i]access_type')->radioList([1 => 'Allow access', 2 => 'Can\'t allow access'],[ 'item' => function($index, $label, $name, $checked, $value) {
$return = '<label class="modal-radio">';
$return .= '<input type="radio" name="' . $name . '" value="' . $value . '" id="custom_id_value_'.$index.'" >';
$return .= '<span> ' . ucwords($label) . '</span>';
$return .= '</label>';
return $return;
}]); ?>

Update multiple rows with same id using foreach loop

I echo 2 input-fields (2 db columns) foreach row in the database (where id=$id) with each row's data being inside the input field. Admin-user can change data inside input-field and click update.
Currently, when clicking the update-button, it does update the rows (where id=$id) but it updates each row with the input-data of the first input field instead of updating input-data foreach input field. Thank you in advance for help.
view:
<form action='<?= site_url("admin/do_edit_page"); ?>' method='POST'>
<?php foreach($link_data as $row) : ?>
Link:<br />
<input type='text' name='page_link_title' value='<?= $row->link_title; ?>'>
<input type='text' name='page_link_sub_title' value='<?= $row->link; ?>'><br />
<?php endforeach; ?>
<input type='submit' name='update_site' value='Update'>
</form>
controller:
public function do_edit_page(){
$id = $this->input->post('page_id', TRUE);
$this->content_model->update_links($id);
}
model:
public function update_links($id){
foreach($_POST as $update_rows){
$update_rows = array(
array(
'page_id' => $id,
'link_title' => $this->input->post('page_link_title', TRUE),
'link' => $this->input->post('page_link_sub_title', TRUE)
)
);
$this->db->update_batch('content_links', $update_rows, 'page_id');
}
}
you have to move your form tag between the foreach loop so that it can submit the data of single row. something like this
<?php foreach($link_data as $row) : ?>
<form action='<?= site_url("admin/do_edit_page"); ?>' method='POST'>
Link:<br />
<input type='text' name='page_link_title' value='<?= $row->link_title; ?>'>
<input type='text' name='page_link_sub_title' value='<?= $row->link; ?>'><br />
<input type='submit' name='update_site' value='Update'>
</form>
<?php endforeach; ?>

Checkbox form sends zeros to mysql, whether boxes are checked or not

I've setup a checkbox form (each question has only one checkbox). The form is submitting, but it only sends zeros to mysql--whether the box has been checked or not. How can I get the correct values (1 or 0) sent to mysql?
Built in codeigniter/mysql.
FORM
<?php echo form_open('addFoo'); ?>
<input type="checkbox" name="foo1" value="" /> //I tried this w/values incl; still zeros
<input type="checkbox" name="foo2" value="" />
<input type="checkbox" name="foo3" value="" />
<input type="checkbox" name="foo4" value="" />
<?php echo form_submit('submit', 'Save Changes'); ?>
<?php echo form_close(); ?>
CONTROLLER
function addFoo()
{
if ($this->input->post('submit')) {
$id = $this->input->post('id');
$foo1 = $this->input->post('foo1');
$foo2 = $this->input->post('foo2');
$foo3 = $this->input->post('foo3');
$foo4 = $this->input->post ('foo4');
$this->load->model('foo_model');
$this->foo_model->addFoo($id, $foo1, $foo2, $foo3, $foo4);
}
}
MODEL
function addFoo($id, $foo1, $foo2, $foo3, $foo4) {
$data = array(
'id' => $id,
'foo1' => $foo1,
'foo2' => $foo2,
'foo3' => $foo3,
'foo4' => $foo4
);
$this->db->insert('foo_table', $data);
}
<input type="checkbox" name="foo2" value="" /> <-- needs a value. Your DB inserts a zero if it's empty.
I think the issue is likely on your DB end, not the form-end.