Zend_Form array notation and empty elements names - zend-form

I'want to render:
<input type="text" value="" name="foo[]" />
<input type="text" value="" name="bar[]" />
but Zend_Form_Element require a (string) name, so I need to do:
$this->addElement('text', '1', array(
'belongsTo' => 'foo'
));
$this->addElement('text', '2', array(
'belongsTo' => 'bar'
));
but the output is:
<input id="foo-1" type="text" value="" name="foo[1]" />
<input id="bar-2" type="text" value="" name="bar[2]" />
I can also accept an output like:
<input id="foo-1" type="text" value="" name="foo[1]" />
<input id="bar-1" type="text" value="" name="bar[1]" />
but Zend_Form_Element rewrite elements with the same name
is there a way to do what I need?

For multiple values:
$foo = new Zend_Form_Element_Text('foo');
// Other parameters
$foo->setIsArray(TRUE);
$this->addElement($foo);
Generates: name="foo[]"
--
If you're looking for given keys such as name="foo[bar]", use:
$bar= new Zend_Form_Element_Text('bar');
// Other parameters
$bar->setBelongsTo('foo');
$this->addElement($bar);
--
Tested on ZF 1.11.5

class MyFooForm extends Zend_Form {
public function init() {
$fullNameOpts = array(
'required'=>false,
'label'=>'fullName',
'isArray'=>true,
'validators' => array( array('stringLength', false, array(1, 250) ) )
);
$this->addElement('text' ,'fullName',$fullNameOpts);
// rest of the elements , forms and stuff goes here
}
}
And that does creates
<dd id="fullName-element"><input type="text" class="inputAccesible" value="" id="fullName"name="fullName[]"></dd>
It's on Element.php , in Form , line 512 "isArray" check.
I'm using a regular zend_form, crossValidation with custom validators and i'm pushing subforms to replicate the main form, 'cause the user can add multiple times the same form.
Additionally , I'm too lazy to research custom decorators, i have created one, but it kills subForms and array notation, so i just stick with the regular ones, and that solves it.
I'm at Zf 1.10.

Related

request()->validate() does not work the second time around n laravel 8

I have a method to store and validate data from POST request, the first method works fine and validated successfully.
public function saveClientInfo(){
ClientInfo::create($this->validateClientInfo());
}
public function validateClientInfo(){
return request()->validate([
'code' => 'required',
're' => ['required','unique:client_infos'],
'name' => 'required',
'contact' => 'required',
'email' => 'required',
'property' => 'required',
'status' => 'required'
]);
}
Here in this second method with the same structure for saving and validating the requests, it doesn't work I'm very intrigue as to why.
public function loanStatus(){
PaymentInfo::create($this->validatePay());
}
public function validatePay(){
return request()->validate([
'payment_type' => 'required',
'terms' => 'required',
'amount_due' => 'required',
'from' => 'required',
'to' => 'required'
]);
}
By the way they're in the same controller so I dont get nauseous trying to figure out what is the difference of both as to where I went wrong.
NOTE:
the error it gives is Route GET is not Supported suggested Method
POSTS some sort of like this,
that's why it's confusing enough already I checked the form and routes and I used post so why this error is showing so I figure it must be the request validate part.
Blade File
<form action="/proceed/loan-status" method="POST">
#csrf
<input type="hidden" name="re" value="{{ $re }}">
<div class="row">
<div class="p-2">
<input type="text" class="form-control" name="payment-type" placeholder="Payment Type...">
#if($errors->has('payment_type'))
      <p class="alert text-danger">{{ $errors->first('payment_type') }}</p>
#endif
</div>
<div class="p-2">
<input type="text" class="form-control" name="terms" placeholder="Terms...">
</div>
<div class="p-2">
<input type="number" min="2" max="" step="any" class="form-control" name="amount" placeholder="Amount Due...">
</div>
<div class="p-2">
<input type="text" class="form-control" name="from" placeholder="From...">
</div>
<div class="p-2">
<input type="text" class="form-control" name="to" placeholder="To...">
</div>
</div>
</div>
<div class="card-footer d-flex justify-content-between">
<button type="submit" class="btn btn-success btn-sm">Save</button>
Cancel
</div>
</form>
web.php
Route::post('/proceed/loan-status', 'ClientInfoController#loanStatus');
If your Laravel version is 8, change the route sintax
Route::post('/proceed/loan-status', ClientInfoController::class,'loanStatus')->name('proceed.loan-status');
and in the blade form change the action to
<form action="{{ route('proceed.loan-status') }}" method="POST">
Also, I suggest you use validation request like Laravel doc, it's a better coded controller and adjusted to patterns. Changing that, you must have something like
public function loanStatus(StorePaymentInfoRequest $request){
PaymentInfo::create($request->validated());
}
and in the StorePaymentInfoRequest
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'payment_type' => 'required',
'terms' => 'required',
'amount_due' => 'required',
'from' => 'required',
'to' => 'required'
];
}
Please use the laravel's request validation.
php artisan make:request FormRequest
It will generate a file that you can use.
https://medium.com/#kamerk22/the-smart-way-to-handle-request-validation-in-laravel-5e8886279271
This Article is a good example.
The error look like you have defined post route in web.php but you are requesting form method is GET or vice versa .So it throwing error
So you need to change form method to post to support that
<form method="POST" >
Or if you still need get request in form method then you need to update routes form post to get
in your Route put your code like this
ROUTE::POST('/VARRIBALE NAME', [CONTROLLER NAME::CLASS, 'FUNCTIONNAME']->NAME('VARRIBALE NAME.FUNCTIONNAME);

Web Component Input radio not in a group

I have the WC <m-form> witch is the wraper for my form and the input fields.
In the renderHTML() of m-form i do this:
renderHTML() {
this.loadChildComponents().then(children => {
Array.from(this.root.querySelectorAll('input'))
.filter(i => i.getAttribute('type') != "hidden").forEach(input => {
const label = this.root.querySelector(`label[for=${input.getAttribute("name")}]`)
const aInput = new children[0][1](input, label, { namespace: this.getAttribute('namespace') || ''})
aInput.setAttribute('type', input.getAttribute('type'))
input.replaceWith(aInput)
})
})
}
This wraps <input> and <label> in an <a-input> WC.
But when I want to do the same with
<input type="radio" id="gender1" name="gender" value="herr">
<label for="gender1">Herr</label>
<input type="radio" id="gender2" name="gender" value="frau">
<label for="gender2">Frau</label>
they are not in a group.
What can i do to get them grouped together but also in the <a-input>?
Here is the Code on the site and what got rendered out.
shadowDOM encapsulates CSS, HTML and behavior
Both you inputs are in shadowDOM. That is like putting them into 2 different IFRAMEs.
They have no clue another input exists
Remove shadowDOM
or, loads more work, add Events to make the inputs communicate with each other

ASP.NET Core Razor: Conditional attribute without value

I want to conditionally render a readonly attribute in an <input>. In the ASP.NET Core dialect of Razor the interpretation of attributes is a little different opposed to System.Web.Mvc.
In System.Web.Mvc I used to do:
<input name="Name" class="form-control" #(Model.MyCondition ? "readonly" : "") />
In ASP.NET Core, the same does not work: no error, but the attribute isn't rendered either.
<input asp-for="Name" class="form-control" #(Model.MyCondition ? "readonly" : "" ) />
Once the attribute is present in the HTML it is enforced, so I cannot do something with the value of the attribute. This snippet would cause the input to always be read-only:
<input asp-for="Name" class="form-control" readonly="#(Model.MyCondition ? "readonly" : "" )" />
Now, I can work around by creating a tag helper that would respond to the is-readonly tag, but that seems very convoluted for something so simple.
How do I conditionally render a no-value attribute without resorting to a custom tag helper?
If you set the value to null when your condition isn't matched (instead of an empty string) then the attribute will not be rendered, as follows:
<input asp-for="Name" class="form-control" readonly="#(Model.MyCondition ? "readonly" : null )" />
#{
var htmlAttributes = (MyModel.MyCondition == true) ? (object)new
{
##readonly = "readonly"
} : null;
}
#Html.TextBoxFor(m => m.Nom, htmlAttributes)
What I do is create an #if statement. I find it more transparent.
#if (Model.MyCondition)
{
<input asp-for="Name" class="form-control" readonly="readonly" />
}
else
{
<input asp-for="Name" class="form-control" />
}

VeeValidate Confirmed Rule Fails With Custom Validator

Using Vee-Validate, when using a custom validator along with the confirmed rule, the confirmed rule always fails validation. The custom validator is specified on the input field being confirmed like so:
<input type="password" placeholder="Password" data-vv-as="password" v-model="password" name="password" v-validate="'required|min:8|has_upper'" />
<input type="password" placeholder="Password" data-vv-as="confirm" v-model="confirmPassword" name="confirmPassword" v-validate="'required|confirmed:password'" />
Here is my Vue instance:
(function (Vue, VeeValidate) {
VeeValidate.Validator.extend('has_upper', {
getMessage: function (field) {
return 'The ' + field + ' must contain an upper case letter';
},
validate: function (value) {
return /^(?=.*[A-Z]).+$/.test(value);
}
});
Vue.use(VeeValidate);
var enroll = {
el: "#app",
data: {
password:'',
confirmPassword: ''
}
}
var app = new Vue(enroll);
})(Vue, VeeValidate)
The custom validator for the password field is triggering as expected, however, as mentioned the confirmed rule is always failing for the confirm password model.
Recently in Vee-Validate (2.1.0 and greater), they've changed how parameters work for these. Now, the target of confirmed has to be a ref, so this will work:
<input type="password" ref="password" name="password" v-validate="'required'" />
<input type="password" v-model="confirmPassword" name="confirmPassword" v-validate="'confirmed:password'" />
The only change you really need to make is to add ref="password" to your password input.
See here for the author talking about this change: https://github.com/baianat/vee-validate/issues/1415
So far I don't see any changes in the documentation, but I assume it will come.
Working example: https://codesandbox.io/s/km4lw12823

Insert Data Into Wordpress Database Custom Table From Custom Form

Insert data into Wordpress database table from a custom form
I have worked with your example but I have a problem. I have error on the start. I get this message:
WordPress database error: [Cannot add or update a child row: a foreign
key constraint fails (prowebex_barbara.barbara_schedule, CONSTRAINT
barbara_schedule_ibfk_1 FOREIGN KEY (day_id) REFERENCES barbara_days
(id_day))]
INSERT INTO barbara_schedule (id, challenger, challenged,
date_match, day_id, term_start, term_end) VALUES ('', 'gfdgdfgdfg', 'gdfgfdgdf', '2015-11-30', '', '23:00', '00:59');
Data Submitted
Can you tell me how can I change this. I have already, truncate tables,delete both tables. I don't know what to do.
<?php
if(isset($_POST['submit'])) {
global $wpdb;
$id = sanitize_text_field($_POST['id']);
$challenger = sanitize_text_field($_POST['challenger']);
$challenged = sanitize_text_field($_POST['challenged']);
$date_match = sanitize_text_field($_POST['date_match']);
$day = sanitize_text_field($_POST['day_id']);
$term_start = sanitize_text_field($_POST['term_start']);
$term_end = sanitize_text_field($_POST['term_end']);
$table_name = $wpdb->prowebex_barbara.barbara_schedule;
$wpdb->insert($table_name, array(
'id' => $id,
'challenger' => $challenger,
'challenged' => $challenged,
'date_match' => $date_match,
'day_id' => $day,
'term_start' => $term_start,
'term_end' => $term_end,
),
array( '%s', '%s', '%s', '%s', '%s', '%s', '%s')
);
$msg = "Data Submited";
echo $msg;
}?>
<form action="" method="post" id="subForm">
<div>
<input type="hidden" name="id" id="iiihuu-iiihuu" placeholder="" />
<br>
<input type="text" name="challenger" id="iiihuu-iiihuu" placeholder="Izazivač" />
<br>
<input type="text" name="challenged" id="iiihuu-iiihuu" placeholder="Izazvani" />
<br>
<input type="date" name="date_match" id="iiihuu-iiihuu" placeholder="Datum" />
<br>
<select>
<option>I don't know how to do this</option>
</select>
<br>
<input type="time" name="term_start" id="iiihuu-iiihuu" placeholder="Termin od" />
<br>
<input type="time" name="term_end" id="iiihuu-iiihuu" placeholder="Termin do" />
<br>
<input type="submit" name="submit" value="Spasi" class ="submit_button" id="formSubmit" />
</div>
</form>
</div>
The errors look like that the custom tables actually have relationship with each other, not sure how the database structure is, but it looks like in order to create a record in barbara_schedule you will have to also create a record in barbara_days.
So in order to just check if this is the case you should pass a fake day_id. So replace $_POST['day_id'] with an existing day_id that already exists in the barbara_days table.
But if thats not the case, then here is a quick thought, do you have a require statement on the top of this file?
Usually this is needed in order to make an insert on custom tables besides the meta table.
require_once('LINK-TO/wp-load.php');
Secondly i am not sure, what this outputs
$table_name = $wpdb->prowebex_barbara.barbara_schedule;
but just in case, try to hardcode the table name like this
$table_name = $wpdb->prefix."table-name"