Hi waqar,
i have two field, price-sale, price-rental.
i need to detect the both filled NOT filled at the same time, only one of the field allowed.
i created very simple fn like this. but it doesnt work. could you advice pls. thanks.
add_action( 'save_post', 'error_sale_rent_filled', 99 );
function error_sale_rent_filled( $post_id )
{
if( get_post_type( $post_id ) == 'office' or get_post_type() == 'industrial'
) {
$field_rent = get_post_meta($post_id, 'wpcf-property-price-rental', true);
$field_sale = get_post_meta($post_id, 'wpcf-property-price-sale', true);
if( $field_rental != '' and $field_sale != '') ;
{
echo ('Both Sale and Rental Price is filled, remove one !');
}
}
}
Hi Dee,
Thank you for contacting us and I'll be happy to assist.
To include form validations rules like these, you can use front-end conditional blocks:
https://toolset.com/documentation/user-guides/conditional-display-for-form-inputs/
For example, to show a message that both fields should not be filled at the same time, you can add:
[cred_show_group if="( $(property-price-rental) ne '' ) AND ( $(property-price-sale) ne '' )" mode="fade"]
<p class="wpt-form-error">Both Sale and Rental Price is filled, remove one!</p>
[/cred_show_group]
Likewise, you can wrap your form's submit button, inside another conditional block, so that it only shows, when at least one of these two fields is empty:
[cred_show_group if="( $(property-price-rental) eq '' ) OR ( $(property-price-sale) eq '' )" mode="fade"]
[cred_field field="form_submit" output="bootstrap" value="Submit" class="btn btn-primary btn-lg"]
[/cred_show_group]
Important note: In a case when a certain form validation rule(s) can't be applied through front-end conditional blocks, you can use "cred_form_validate" ( ref: https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate ) to connect a custom PHP function.
I hope this helps.
regards,
Waqar
Hi waqar, thanks for the reply.
i need this for view for now, haven't go into cred .soon i will. thanks.
Hi Dee,
I'm sorry, but I couldn't fully understand how you're saving a post from the front-end, without a CRED form.
Can you please share some more details around what and where exactly you're planning to use this function?
regards,
Waqar
Hi, sorry its not view either, its pure custom post type edit section.
same as what you shared: https://toolset.com/forums/topic/calculation-and-storing-in-a-new-field/
thanks.
Hi Dee,
Thank you for clarifying that.
The "save_post" hook ( ref: https://toolset.com/forums/topic/calculation-and-storing-in-a-new-field/#post-1185754 ) can be used to add/edit/remove a custom field value, programmatically, but it can't show a validation message and/or stop the post's record from saving.
A neater way of achieving that can be through a new custom field. This new custom field can be of type "radio" or "select" and can have two values to select from: "Sale" or "Rental".
Based on the value that is selected in this field, you can hide/show the "Sale Price" and "Rental Price", without any additional custom code:
https://toolset.com/documentation/user-guides/types-custom-fields-conditional-display/
regards,
Waqar
Yes, i did think of that too but i have to update all my listing manually.
i guess its now or never, thank you for the reminder. cheers