Laravel 9 Prevent trims on Old value - trim

I have an old value containing the person's name, but when I put it in the input value it automatically trims (only the first word), I've tried adding exceptions in the AppServiceProvider and Trim middleware but it's not working, does anyone have a solution?
Top : The Old Value, Bottom : The Input Value
EDIT 1 (ADD CODE)
// File : input component (form-field.blade.php)
#props([
'label' => 'undefined',
'placeholder' => '',
'type' => 'text',
'disabled' => false,
'required' => true,
'value' => old($name) ?? '',
'min' => '',
'max' => '',
'name' => '',
'items' => [],
])
<label class="text-sm font-semibold" for={{ $name }}>{{ $label }} {!! $required ? '<span class="text-primary">*</span>' : '' !!}</label>
<div class="bg-gray-300 rounded-md #error($name) border-primary border-1 #enderror">
<input
class="flex-grow bg-transparent border-gray-300 border-2 rounded-md w-full py-2 px-2 focus:border-black focus:outline-none"
name={{ $name }}
type={{ $type }}
id={{ $name }}
min={{ $min }}
max={{ $max }}
value={{ $value ?? old($name) }}
{{ $disabled ? "disabled" : "" }}
>
</div>
// File : View Form Page
<x-form-field
:name="'name'"
:label="'Nama'"
:value="session()->get('data')->employee_name ?? null"
/>

If you view the source, you'll see <input ... value=Prof Smiling ...>.
value={{ $value ?? old($name) }} without quotation marks is your problem, because Smiling gets treated as a different HTML attribute, as if you wrote <input value="Prof" Smiling="">.
value="{{ $value ?? old($name) }}" will avoid breaking when there are spaces.
(You should fix this everywhere, not just for this particular occurrence.)

Related

How to add custom fields to WooCommerce product data panel

I have created a custom product type for events in WooCommerce. Now I want to add some custom fields to it.
This can be achieved with functions like woocommerce_wp_select(), woocommerce_wp_text_input() etc. However, as far as I know, whith these function you can only add text input fields, textarea's, select boxes and select dropdowns.
I want to add a date picker field, a file upload field and an url input field.
I have created these fields and they do render on the admin panel as they should, but the values are not saved (although I'm using the action hook 'woocommerce_process_product_meta'). Only the first 2 fields (event_type and event_location), which are created with the WooCommerce function, are stored properly.
What am I doing wrong here?
My code:
add_action('woocommerce_product_data_panels', 'okappi_add_custom_fields');
function okappi_add_custom_fields()
{ ?>
<div id="event_details" class="panel woocommerce_options_panel hidden">
<div class="options_group" class="show_if_event">
<? woocommerce_wp_select([
'id' => 'event_type',
'label' => __('Event type', 'custom'),
'wrapper_class' => 'show_if_event',
'value' => get_post_meta(get_the_ID(), 'event_type', true),
'options' => array('online' => 'Online', 'international' => 'International', 'internal' => 'Internal'),
]); ?>
<? woocommerce_wp_text_input([
'id' => 'event_location',
'label' => __('Event location', 'custom'),
'wrapper_class' => 'show_if_event',
'value' => get_post_meta(get_the_ID(), 'event_location', true),
]); ?>
<p class="show_if_event form-field event_start_date_field">
<label for="event_start_date">Start date</label>
<input type="date" id="event_start_date" name="event_start_date" class="date short">
</p>
<p class="show_if_event form-field event_end_date_field">
<label for="event_end_date">End date</label>
<input type="date" id="event_end_date" name="event_end_date" class="date short">
</p>
<p class="show_if_event form-field event_pdf_field">
<label for="event_pdf">PDF upload</label>
<input type="file" id="event_pdf" name="event_pdf" class="date short">
</p>
<p class="show_if_event form-field event_link_field">
<label for="event_link">Event link</label>
<input type="url" id="event_link" name="event_link" class="date short" placeholder="https://www.my-event.com/">
</p>
</div>
</div>'
<? }
add_action('woocommerce_process_product_meta', 'save_custom_fields');
function save_custom_fields($post_id)
{
$product = wc_get_product($post_id);
$event_type = isset($_POST['event_type']) ? $_POST['event_type'] : '';
$product->update_meta_data('event_type', sanitize_text_field($event_type));
$event_location = isset($_POST['event_location']) ? $_POST['event_location'] : '';
$product->update_meta_data('event_location', sanitize_text_field($event_location));
$event_start_date = isset($_POST['event_start_date']) ? $_POST['event_start_date'] : '';
$product->update_meta_data('event_start_date', sanitize_text_field($event_start_date));
$event_end_date = isset($_POST['event_end_date']) ? $_POST['event_end_date'] : '';
$product->update_meta_data('event_end_date', sanitize_text_field($event_end_date));
$event_pdf = isset($_POST['event_pdf']) ? $_POST['event_pdf'] : '';
$product->update_meta_data('event_pdf', sanitize_text_field($event_pdf));
$event_link = isset($_POST['event_link']) ? $_POST['event_link'] : '';
$product->update_meta_data('event_link', sanitize_text_field($event_link));
$product->save();
}

Change Symfony Form Type element's form group CSS class

Briefly: I want to edit my form elements parent div class. In this code don't add or edit the css class to my target div (which is form-group) and the follow Form Type is an element of another Form Type.
I have an Form Type like the follow:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('id', IntegerType::class, [
'empty_data' => '',
'label' => 'ID',
'attr' => [
'class' => 'col-sm-3'
],
]);
$builder->add('companyName', TextType::class, [
'empty_data' => '',
'label' => 'Şirket Adı',
'attr' => [
'class' => 'col-sm-3'
],
]);
}
So this form type renderin this HTML:
<div class="form-group field-member_earning_filter"><label class="control-label"> </label>
<div id="form_filters_member" css="col-sm-11">
<div class="form-group field-integer"><label class="control-label"
for="form_filters_member_id">ID</label><input type="number"
id="form_filters_member_id"
name="form_filters[member][id]"
class="form-control col-sm-3">
</div>
<div class="form-group field-text"><label class="control-label" for="form_filters_member_companyName">Ticari
Ünvan</label><input type="text" id="form_filters_member_companyName"
name="form_filters[member][companyName]" class="form-control col-sm-3"></div>
As you can see the CSS class which I want to add for each elements group are added to form-control element. But actually I want to do this:
<div class="form-group col-sm-3 field-integer"><label class="control-label"
for="form_filters_member_id">ID</label><input type="number"
id="form_filters_member_id"
name="form_filters[member][id]"
class="form-control">
I tried to refactor this challenge at view layer (on twig side) but actually doesn't have any real block to edit that "form group" section. Additionally that it's a form type in another form type for that reason the block don't affect my code. I mean this :
{% block form_row %}
<div class="form-group col-sm-3"> <--! but this form group actually my form type's form group -->
{{ form_label(field) }}
{{ form_errors(field) }}
{{ form_widget(field) }}
</div>
{% endblock %}
Do you have any idea?
I found a solution for my problem. It's little bit workaround or hacking means but solved my problem:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('id', IntegerType::class, [
'empty_data' => '',
'label' => 'ID',
'block_prefix' => ' col-sm-3'
]);
$builder->add('companyName', TextType::class, [
'empty_data' => '',
'label' => 'Şirket Adı',
'block_prefix' => ' col-sm-3'
]);
}
This form-group's div is just affecting from block_ options. As you guess the block_prefix is a prefix of class but just adding a whitespace () then it's solve my problem :)

Display all rows in Symfony 3

I have a form which has EntityType:class below in Symfony 3 or SF3, as you can see I am calling the function from method findAllActiveBuyingCurrencies from BranchCurrencyRepository.
->add('currency', EntityType::class, [
'class' => BranchCurrency::class,
'choice_value' => 'rate',
'choice_label' => function($currency){
return $currency->getName() . ' (' . $currency->getCode() .') - '.$currency->getRate().'';
},
'placeholder' => 'Choose currency',
'query_builder' => function (BranchCurrencyRepository $er) {
return $er->findAllActiveBuyingCurrencies();
},
])
The functions does it's thing and displays all currencies inside the form (Option)
But, I wanted to display all currencies and my problem is, only 25 currencies are displayed.
Please see my code displaying all currencies
// branchCurrenciesRepository
public function findAllActiveBuyingCurrencies() {
return $this->createQueryBuilder('currency')
->where('currency.type = :type')
->setParameter('type', BranchCurrencyConstant::Buying);
}
View
<div class="col-3">
<label for="" class="required">
<strong> Currency <span class="highlight-red"></span></strong>
</label>
{{ form_widget(form.currency) }}
{% if not form.currency.vars.valid %}
<p class="mt10" style="color: #DC2B1B;">{{ form.currency.vars.errors[0].message }}</p>
{% endif %}
</div>

WordPress Radio Boxes - selects only the last one

So I have a form as shown bellow. its a bit long. It contains three radio boxes. Every time I select one, doesn't matter which, and then hit submit, the last radio element shows up as selected instead of the one I clicked. I var dump the option (in this case aisis_core['display_rows']) and it will say the value of the radio element i selected instead of the current on selected.
So I select lists, it will show lists but the radio box selected is no_posts. Can some one tell me what I am doing wrong?
<form action="options.php" method="post">
<input type='hidden' name='option_page' value='aisis_options' /><input type="hidden"
name="action" value="update" /><input type="hidden" id="_wpnonce" name="_wpnonce"
value="f0385965c6" /><input type="hidden" name="_wp_http_referer" value=
"/WordPressDev/wp-admin/admin.php?page=aisis-core-options&settings-updated=true" />
<fieldset>
<div class="control-group">
<label class="radio"><input type="radio" id="rows" class="display" name=
"aisis_core[display_rows]" value="display_rows" checked="checked" /> Display
posts as rows. </label>
<div class="control-group">
<label class="radio"><input type="radio" class="display" name=
"aisis_core[display_rows]" value="list" checked="checked" /> Display posts a
list. </label>
</div>
<div class="control-group">
<label class="radio"><input type="radio" id="noDisplay" class="display" name=
"aisis_core[display_rows]" value="no_posts" checked="checked" /> Display no
posts.</label>
<div class="no_posts_section borderBottom">
<div class="well headLine">
<h1>Display No Rows</h1>
<p>If you choose to display no rows please give me a url of the page or
content you would like to display instead.</p>
<p class="text-info"><strong>Note:</strong> Formatting of said content is
up you. All we do is display it.</p>
</div>
<div class="control-group">
<div class="controls">
<input type="url" name="aisis_core[index_page_no_posts]" value=
"http://google.ca" placeholder="Url" />
</div>
</div>
</div>
<div class="control-group">
<div class="form-actions">
<input type="submit" class="btn btn-primary btn-large" />
</div>
</div>
</div>
</div>
</fieldset>
</form>
The function I am using from wordpress is:
checked('radio_box_value', isset($options['display_rows']), false)
Note: radio_box_value is replaced with what ever the value of the radio box is.
In this case only the last radio box has the "checked" in it's tag, when it should be which ever one I chose.
How are the elements being created?
The following is how I create the elements, they print out what you see above in the html for the radio buttons. These are done similar to, but not exactly, zend framework.
Its pretty straight forward what were doing, create the element, add the options to the element and then return it.
I hope this gives a better picture as to how these are being created.
protected function _radio_rows_element(){
$options = get_option('aisis_core');
echo $options['display_rows'];
$radio_element = array(
'name' => 'aisis_core[display_rows]',
'value' => 'display_rows',
'class' => 'display',
'id' => 'rows',
'checked' => checked('display_rows', isset($options['display_rows']) && $options['display_rows'] == 'display_rows', false),
'label' => ' Display posts as rows. <a href="#radioRows" data-toggle="modal">
<i class="icon-info-sign"> </i></a>'
);
$radio = new CoreTheme_Form_Elements_Radio($radio_element, $this->sub_section_rows_array());
return $radio;
}
protected function _radio_list_element(){
$options = get_option('aisis_core');
echo $options['display_rows'];
$radio_element = array(
'name' => 'aisis_core[display_rows]',
'value' => 'list',
'class' => 'display',
'checked' => checked('list', isset($options['display_rows']) && $options['display_rows'] == 'list', false),
'label' => ' Display posts a list. <a href="#radioLists" data-toggle="modal">
<i class="icon-info-sign"> </i></a>'
);
$radio = new CoreTheme_Form_Elements_Radio($radio_element);
return $radio;
}
protected function _radio_no_posts_element(){
$options = get_option('aisis_core');
echo $options['display_rows'];
$radio_element = array(
'name' => 'aisis_core[display_rows]',
'value' => 'no_posts',
'class' => 'display',
'id' => 'noDisplay',
'checked' => checked('no_posts', isset($options['display_rows']) && $options['display_rows'] == 'no_posts', false),
'label' => ' Display no posts.</a>'
);
$radio = new CoreTheme_Form_Elements_Radio($radio_element, $this->_sub_section_now_posts_array());
return $radio;
}
"checked" function seems to need the value as second parameter as is explained here http://codex.wordpress.org/Function_Reference/checked
Try like this:
protected function _radio_rows_element(){
$options = get_option('aisis_core');
echo $options['display_rows'];
$radio_element = array(
'name' => 'aisis_core[display_rows]',
'value' => 'display_rows',
'class' => 'display',
'id' => 'rows',
'checked' => checked('display_rows', (isset($options['display_rows']))?$options['display_rows']:'', false),
'label' => ' Display posts as rows. <a href="#radioRows" data-toggle="modal">
<i class="icon-info-sign"> </i></a>'
);
$radio = new CoreTheme_Form_Elements_Radio($radio_element, $this->sub_section_rows_array());
return $radio;
}
protected function _radio_list_element(){
$options = get_option('aisis_core');
echo $options['display_rows'];
$radio_element = array(
'name' => 'aisis_core[display_rows]',
'value' => 'list',
'class' => 'display',
'checked' => checked('list',(isset($options['display_rows']))?$options['display_rows']:'', false),
'label' => ' Display posts a list. <a href="#radioLists" data-toggle="modal">
<i class="icon-info-sign"> </i></a>'
);
$radio = new CoreTheme_Form_Elements_Radio($radio_element);
return $radio;
}
protected function _radio_no_posts_element(){
$options = get_option('aisis_core');
echo $options['display_rows'];
$radio_element = array(
'name' => 'aisis_core[display_rows]',
'value' => 'no_posts',
'class' => 'display',
'id' => 'noDisplay',
'checked' => checked('no_posts', (isset($options['display_rows']))?$options['display_rows']:'', false),
'label' => ' Display no posts.</a>'
);
$radio = new CoreTheme_Form_Elements_Radio($radio_element, $this->_sub_section_now_posts_array());
return $radio;
}
This will not give warning when isn't declared the variable $options['display_rows'] (that as you said is a possibility in your case) and will pass the value to the WordPress function to compare with.
You'll want to check the value in the checked condition, not you're just checking if any value is being selected, which is always true after a submit
change
checked('display_rows', isset($options['display_rows']), false)
to:
checked('display_rows', isset($options['display_rows']) && $options['display_rows'] == 'display_rows', false),
and for the list to:
checked('display_rows', isset($options['display_rows']) && $options['display_rows'] == 'list', false),
I did manage to write this:
public function set_element_checked($value, $option, $key){
$options = get_option($option);
if(isset($options[$key]) && $options[$key] == $value){
return 'checked';
}
}
which does exactly what I want. compare the element value to that of the $option[$key] and if they match return checked. can be called via:
'checked' => set_element_checked('display_rows', 'aisis_core', 'display_rows');

Symfony/Silex: Form submission does nothing (no errors, nothing returned)

I'm using the most recent version of Silex (without the .phar) with Doctrine DBAL installed, on this signup form page.
If I enter invalid details, it returns to that form as excepted. But if the details are valid, instead of redirecting to the /success/ page, it returns the same form again like nothing happened. The database has no entry received and Apache error log doesn't report any problems.
<?php
// ...
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Yaml\Parser;
use Silex\Provider\FormServiceProvider;
use Symfony\Component\Validator\Constraints as Assert;
// ...
$app->match('/signup/', function(Request $request) use($app, $page) {
$data = array('name' => 'John','surname' => 'Smith','telephone' => '00011112222');
$form = $app['form.factory']->createBuilder('form', $data)
->add('name', 'text', array(
'constraints' => array(
new Assert\NotBlank(),
new Assert\MinLength(2),
),
'invalid_message' => 'First name is too short, It should have 2 characters or more',
))
->add('surname', 'text', array(
'constraints' => array(
new Assert\NotBlank(),
new Assert\MinLength(2),
),
'invalid_message' => 'Surname is too short, It should have 2 characters or more',
))
->add('telephone', 'text', array(
'constraints' => array(
new Assert\NotBlank(),
new Assert\Regex("/[\d\-\ ]+/"),
new Assert\MinLength(11),
),
'invalid_message' => 'Please enter a valid phone number. Must have 11 digits and may contain dashes (-) or spaces.',
))
->getForm();
if ('POST' == $request->getMethod()) {
$form->bindRequest($request);
if ($form->isValid()) {
$data = $form->getData();
$app['db']->insert('signups', array(
'forename' => $data['name'],
'surname' => $data['surname'],
'telephone' => $data['telephone']
));
return $app->redirect('/success/');
}
}
$page['form'] = $form->createView();
return $app['twig']->render('signup.html.twig', $page);
}, 'POST|GET');
$app->match('/success/', function() use($app, $page) {
return $app['twig']->render('success.html.twig', $page);
}, 'POST|GET');
And the twig form
<form class="well" action="/signup/" method="post">
<fieldset>
<div class="control-group">
{% if (form_errors(form.name)) or (form_errors(form.surname)) or (form_errors(form.telephone)) %}
<div class="error-in-form">
<h5 style="color:#c00;">Please review the following errors:</h5>
<br />
<div>
<p class="help-msg"><span>First Name: </span></p>
<div class="error-msg">{{ form_errors(form.name) }}</div>
<div class="clearfix"></div>
</div>
<div>
<p class="help-msg"><span>Surname: </span></p>
<div class="error-msg">{{ form_errors(form.surname) }}</div>
<div class="clearfix"></div>
</div>
<div>
<p class="help-msg"><span>Telephone: </span></p>
<div class="error-msg">{{ form_errors(form.telephone) }}</div>
<div class="clearfix"></div>
</div>
</div>
{% endif %}
{{ form_label(form.name) }}
<div class="controls">
{{ form_widget(form.name, { 'attr': { 'class': 'input-medium' } } ) }}
{{ form_widget(form.surname, { 'attr': { 'class': 'input-medium' } } ) }}
</div>
</div>
<div class="control-group">
{{ form_label(form.telephone) }}
<div class="controls">
{{ form_widget(form.telephone, { 'attr': { 'class': 'input-fullwidth' } } ) }}
</div>
</div>
<p class="tnc">If you accepts the terms and conditions below, please proceed.</p>
<button id="big-red-button" type="submit" class="btn btn-danger btn-fullwidth">Submit ></button>
</fieldset>
</form>
Well, looks like I forgot to add {{ form_rest }} to the Twig form template.
Since I hadn't also included {{ form_errors(form) }} either, I couldn't see the error regarding the missing CSFP token, a hidden field that gets added to the form.

Resources