In Woocommerce I'm trying to do a validation check of the Address field in Woocommerce,
to warn customers in case they forgot to place their house or street numbers.
I am using the following snippet, which works for this exact validation and displays in real time a warning message below the field after the user clicks away from it, however there is an issue, as it forces the field to always have a number, and the point is simply to warn users but still allow them to progress with the order.
I am not sure how to proceed with this, as In my search I couldn't find anything related to "Not forcing" or making the validation check but still allowing customers to proceed with the order.
/**
* Add validation for street house adress: Verify if there is a number
*/
add_filter(
'woocommerce_default_address_fields', function( $fields ) {
unset( $fields['postcode']['custom_attributes']['data-parsley-length'] );
$fields['address_1']['custom_attributes']['data-parsley-pattern'] = '/^(?=.*\d).+$/';
$fields['address_1']['custom_attributes']['data-parsley-error-message'] = __( 'It seems your address has no street or door number', 'your-translation-domain' );
return $fields;
},
100001 );
Thank you in advance for the attention and advice.
I need a way to place data of failing record along with count of records that failed because of the same reason. I hope the explanation is enough to get the requirement.
eg:
$rules['inventories.*.activity_id']= [ 'required', 'exists:activities,id ];
$messages['inventories.*.activity_id.required'] = 'Activity id can not be blank.';
$messages['inventories.*.activity_id.exists'] = "Activity id <<< [FAILING RECORD -> ACTIVITY_ID] >>> does not exist in the system. <<< NUMBER OF RECORDS FAILED FOR THIS REASON >>> entries skipped.";
Anyone have an idea about this?
I found I can place :attributes but this does't not what I want.
You could try using the :input attribute to display the value you are checking the existence of in your custom validation message:
'Activity id :input does not exist in the system.'
Though this will not get you to your failure count part.
this shows you number of validation errors:
count($errors)
if you want to show the invalid value that passed among request in the error message, you can do this:
public function messages()
{
return [
'fieldname.numeric' =>
'The :attribute must be numeric. Your value is '.$request->input('fieldname')
];
}
To validate the update of the e-mail of a user already registered I have the next function to exclude the "unique" rule for the current User:
public function updateRules() {
return [
'name' => 'required',
'email' => 'required|unique:users,email,'.$this->id,
];
}
In the Laravel docs https://laravel.com/docs/5.7/validation#rule-unique I found the syntax unique:table,column,except,idColumn with 4 parameters:
table: refers to the table name "users"
column: refers the column name "email"
except: I'm taking it as the id of the Model instance I want to exclude from the "unique" verification
idColumn: I have no idea about this
Someone could clarify what do except and idColumn refers to?
Note for those obsessed with duplicate questions: I'm not asking how to do the rule exclusion for the update, because it seems to work just fine, I've read those questions and answers. I'm making helper functions and I need to know exactly what those two parameters means.
So you are right about the except parameter, it is the id that you want to be excluded from the check.
And idColumn is optional in case your column is not called id but user_id for example. So you will use:
'required|unique:users,email,'.$this->id . ',user_id';
Suppose, in the feedback form we want to ask at least one of two contacts: email and/or telephone number. So, in the validation rules will be required_without for each other:
$this->validate($request, [
'email' => 'required_without:tel|email',
'tel' => 'required_without:email|regex:/(01)[0-9]{9}/'
], $messages);
We need to validate the contact data format only when it has been inputted, but in the code above the format of both fields will be checked. Because we need at least one contact, sometimes rule is no use.
I am trying to do a Laravel validation rules as follow:
"permalink" => "required|unique:posts,permalink,hotel_id,deleted_at,NULL|alpha_dash|max:255",
The explanation to the rules is:
I have a table "Posts" in my system with the following fields (among others): hotel_id, permalink, deleted_at. If MySQL would allow make an unique index with null values, the sql would be:
ALTER TABLE `posts`
ADD UNIQUE `unique_index`(`hotel_id`, `permalink`, `deleted_at`);
So: I just add a new row IF: the combination of hotel_id, permalink and deleted_atfield (witch must be NULL) are unique.
If there is already a row where the permalink and hotel_id field are the same and 'deleted_at' field is NULL, the validation would return FALSE and the row wouldnt be inserted in the database.
Well. I don't know why, but the query Laravel is building looks like:
SELECT count(*) AS AGGREGATE FROM `posts`
WHERE `hotel_id` = the-permalink-value AND `NULL` <> deleted_at)
What the heck...
The query I was hoping Laravel build to validation is:
SELECT count(*) AS AGGREGATE FROM `posts`
WHERE `permalink` = 'the-permalink-value' AND `hotel_id` = ? AND `deleted_at` IS NULL
Could someone explain me how this effectively works? Because everywhere I look it looks like this:
$rules = array(
'field_to_validate' =>
'unique:table_name,field,anotherField,aFieldDifferentThanNull,NULL',
);
Does anyone could help me?
Thank you
all.
Finally, I got a proper understanding of the validation (at least, I think so), and I have a solution that, if it is not beautiful, it can helps someone.
My problem, as I said before, was validate if a certain column (permalink) is unique ONLY IF other columns values had some specific values. The problem is the way Laravel validation string rules works. Lets get to it:
First I wrote this:
"permalink" => "required|unique:posts,permalink,hotel_id,deleted_at,NULL|alpha_dash|max:255",
And it was generating bad queries. Now look at this:
'column_to_validate' => 'unique:table_name,column_to_validate,id_to_ignore,other_column,value,other_column_2,value_2,other_column_N,value_N',
So. The unique string has 3 parameters at first:
1) The table name of the validation
2) The name of the column to validate the unique value
3) The ID of the column you want to avoid (in case you are editing a row, not creating a new one).
After this point, all you have to do is put the other columns in sequence like "key,value" to use in your unique rule.
Oh, easy, an? Not so quickly, paw. If you're using a STATIC array, how the heck you will get your "currently" ID to avoid? Because $rules array in Laravel Model is a static array. So, I had to came up with this:
public static function getPermalinkValidationStr() {
$all = Input::all();
# If you are just building the frozenNode page, just a simple validation string to the permalink field:
if(!array_key_exists('hotel', $all)) {
return 'required|alpha_dash|max:255';
}
/* Now the game got real: are you saving a new record or editing a field?
If it is new, use 'NULL', otherwise, use the current id to edit a row.
*/
$hasId = isset($all['id']) ? $all['id'] : 'NULL';
# Also, check if the new record with the same permalink belongs to the same hotel and the 'deleted_at' field is NULL:
$result = 'required|alpha_dash|max:255|unique:posts,permalink,' . $hasId . ',id,hotel_id,' . $all['hotel'] . ',deleted_at,NULL';
return $result;
}
And, in the FrozenNode rules configuration:
'rules' => array(
'hotel_id' => 'required',
'permalink' => Post::getPermalinkValidationStr()
),
Well. I dont know if there is a easiest way of doing this (or a much better approach). If you know something wrong on this solution, please, make a comment, I will be glad to hear a better solution. I already tried Ardent and Observer but I had some problems with FrozenNode Administrator.
Thank you.